2014-01-07 80 views
2

我正在尝试获取光标所在的行号。然而,我没有找到直截了当的方法来获得线路。相反,我想获得当前位置,然后使用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

+2

可能最好在Notepad ++论坛话题中搜索/询问插件开发:http://sourceforge.net/p/notepad-plus/discussion/482781/ – DMaguireKane

回答

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

以下的答案必须是你最初的问题的组合和你自己的答案澄清的版本(这是是什么让我找到了这个完整的解决方案)。

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