在记事本++插件中获取当前光标位置



我正在尝试获取光标所在的行号。但是我没有找到直接的方法就可以获得线路。相反,我正在尝试获取当前位置,然后使用 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());

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

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

// 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);

我能够通过搜索记事本加讨论来解决这个问题。答案是从 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);

这是一种无需调用 SendMessage() 的方法,只需使用标准 Scintilla API 中的方法 – 伪代码如下:

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

这对于将代码保持在更高级别或用于其他语言(如 N++ Python 脚本插件)的脚本非常有用,在这些语言中,调用记录的 API 很容易,但SendMessage()方法可能更困难。(经过测试 - 它有效。

最新更新