我需要处理在编辑控件(而不是 richedit 控件)中更改插入符号位置的事件。
我知道如何处理在编辑中更改文本的事件,但我不知道如何处理在编辑控件中更改插入符号位置的事件。
我希望有人能帮助我。感谢大家。
完成此操作后,我通过子类化编辑控件 (SetWindowLongPtr
) 并向父窗口发送可能会移动插入符号的消息来在状态栏上显示行号。
#define CARET_MOVED_COMMAND_ID 50001
WNDPROC OriginalEditProc = 0;
HWND OriginalEditParent = 0;
LRESULT WINAPI MyEditProc( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam )
{
// Handle message by original edit control procedure.
LRESULT result = CallWindowProc( OriginalEditProc, hwnd, msg, wparam, lparam );
switch ( msg )
{
case WM_CHAR:
case WM_KEYDOWN:
case WM_KEYUP:
case WM_LBUTTONDOWN:
case WM_LBUTTONUP:
case WM_LBUTTONDBLCLK:
case WM_RBUTTONDOWN:
case WM_RBUTTONUP:
case WM_RBUTTONDBLCLK:
case WM_MBUTTONDOWN:
case WM_MBUTTONUP:
case WM_MBUTTONDBLCLK:
case EM_SETSEL:
// And maybe others...
// Notify parent of posible caret move.
// You can recreate EN_SELCHANGE behavior, I have been too lazy
// and was just sending command (like from menu).
SendMessage( OriginalEditParent, WM_COMMAND, MAKEWPARAM( CARET_MOVED_COMMAND_ID, 0 ), 0 );
break;
}
return result;
}
// ---- In initialization code ----
// subclass
OriginalEditParent = MyMainWindow;
OriginalEditProc = (WNDPROC) SetWindowLongPtr( EditBoxHwnd, GWLP_WNDPROC, (LONG_PTR)MyEditProc );
// ---- In parent window message handling ----
case WM_COMMAND:
switch ( LOWORD( wparam ) )
{
case CARET_MOVED_COMMAND_ID:
{
// You sould execute this code in EN_CHANGE handler,
// so it can handle cuting, pasting, undo etc.
int line = (int) SendMessage( EditBoxHwnd, EM_LINEFROMCHAR, -1, 0 );
// Update status bar. Could be optimized by remembering
// displayed line number and updating only when it changes.
wchar_t buff[50];
wsprintf( buff, L"%d", line );
SendMessage( StatusBarHwnd, SB_SETTEXT, MAKEWPARAM( 1, SBT_NOBORDERS ), (LPARAM) buff );
}
break;
}
break;
我是一名优秀的程序员,十分优秀!