【问题标题】:Getting current cursor pos within notepad++ plugin在记事本++插件中获取当前光标位置
【发布时间】:2014-01-25 07:47:06
【问题描述】:

我正在尝试获取光标所在的行号。但是我没有找到直接的方法来获得这条线。相反,我试图获取当前位置,然后使用 SCI_LINEFROMPOSITION 将其转换为行。

::SendMessage(nppData._nppHandle,SCI_GETCURRENTPOS,0,(LPARAM)&first);
::SendMessage(nppData._scintillaMainHandle,SCI_GETCURRENTPOS,0,(LPARAM)&second);
::SendMessage(nppData._scintillaSecondHandle,SCI_GETCURRENTPOS,0,(LPARAM)&third);

这些调用中的每一个都不会更改最后一个参数的值。不幸的是,我没有找到SCI_GETCURRENTPOS 的示例。我可以将文本插入文件,这样我就可以通过这种方式检查值:

std::wstringstream wss;
wss << "First value read" << first << std::endl;
wss << "Second value read" << second << std::endl;
wss << "Third value read" << third << std::endl;
insertTextIntoCurrentFile(wss.str().c_str());

我应该如何获得当前行?在这种情况下,为SendMessage 发送的预期HWND 是什么?

【问题讨论】:

标签: c++ notepad++


【解决方案1】:

以下答案是您最初的问题和您自己的答案(这就是我找到这个完整解决方案的原因)的组合的澄清版本。

// Position of the cursor in the entire buffer
int cursorPosition = ::SendMessage(nppData._scintillaMainHandle, SCI_GETCURRENTPOS, 0, 0);

// Line position of the cursor in the editor
int currentLine = ::SendMessage(nppData._nppHandle, NPPM_GETCURRENTLINE, 0, 0);

// Column position of the cursor in the editor
int currentColumn = ::SendMessage(nppData._nppHandle, NPPM_GETCURRENTCOLUMN, 0, 0);

【讨论】:

    【解决方案2】:

    我能够通过搜索记事本加讨论来解决这个问题。 答案是从SendMessage读取返回值。

    获取 Scintilla HWND:

    int currentEdit;
    ::SendMessage(nppData._nppHandle, NPPM_GETCURRENTSCINTILLA, 0, (LPARAM)&currentEdit);
    HWND curScint = (currentEdit == 0 ) ?
    nppData._scintillaMainHandle:nppData._scintillaSecondHandle;
    

    获取当前光标位置:

    int cursorPosition = ::SendMessage(curScint,SCI_GETCURRENTPOS,0,0);
    

    【讨论】:

      【解决方案3】:

      这是一种无需调用SendMessage(),只需使用methods from standard Scintilla API 的方法——伪代码如下:

      currentLineNumber = editor.SCI_LINEFROMPOSITION(editor.SCI_GETCURRENTPOS())
      

      这对于将代码保持在更高级别或使用其他语言(如在 N++ Python 脚本插件中)编写脚本很有用,在这些语言中调用文档化 API 很容易,但 SendMessage() 方式可能更困难。 (经过测试 - 有效。)

      【讨论】:

        猜你喜欢
        • 2011-09-19
        • 1970-01-01
        • 1970-01-01
        • 2011-12-24
        • 2013-10-26
        • 2022-10-05
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多