通过单击Ctrl + A选择编辑控件中的所有文本

CEO*_*ico 9 winapi visual-c++

如何通过按Ctrl+ 选择编辑控件中的所有文本A?我可以 在WndProc中捕获父窗口的Ctrl+ A.但我不知道如何捕获ctrl+ a应用于编辑控件.我也尝试使用加速器,但它同样适用于父窗口.谢谢.编辑:1-st最简单的方法这个方法基于@phord在这个问题中的答案: win32 select all edit on edit ctrl(textbox)

while( (bRet = GetMessage( &msg, NULL, 0, 0 )) != 0)
{ 
    if (bRet == -1)
    {
        // handle the error and possibly exit
    }
    else
    {
        if (msg.message == WM_KEYDOWN && msg.wParam == 'A' && GetKeyState(VK_CONTROL) < 0)
    {
        HWND hFocused = GetFocus();
        wchar_t className[6];
        GetClassName(hFocused, className, 6);
        if (hFocused && !wcsicmp(className, L"edit"))
            SendMessage(hFocused, EM_SETSEL, 0, -1);
    }
    TranslateMessage(&msg);
    DispatchMessage(&msg);
    }
}
Run Code Online (Sandbox Code Playgroud)

编辑:第二种方法需要使用CreateAcceleratorTable + TranslateAccelerator函数:

//全局变量:

enum {ID_CTRL_A = 1};
HACCEL accel;
Run Code Online (Sandbox Code Playgroud)

//主程序

ACCEL ctrl_a;
ctrl_a.cmd = ID_CTRL_A; // Hotkey ID
ctrl_a.fVirt = FCONTROL | FVIRTKEY;
ctrl_a.key = 0x41; //'A' key
accel = CreateAcceleratorTable(&ctrl_a, 1); //we have only one hotkey
Run Code Online (Sandbox Code Playgroud)

// GetMessage循环的外观

while( (bRet = GetMessage( &msg, NULL, 0, 0 )) != 0)
{ 
    if (bRet == -1)
    {
        // handle the error and possibly exit
    }
    else
    {
        if (!TranslateAccelerator(hWnd, accel,  &msg))
        {
            TranslateMessage(&msg); 
            DispatchMessage(&msg); 
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

//在WndProc中我们必须添加下一个案例

case WM_COMMAND:
{
    if (LOWORD(wParam) == ID_CTRL_A && HIWORD(wParam) == 1)
    {
        //on which control there was pressed Ctrl+A
        //there is no way of getting HWND through wParam and lParam
        //so we get HWND which currently has focus.
        HWND hFocused = GetFocus();
        wchar_t className[6];
        GetClassName(hFocused, className, 6);
        if (hFocudsed && !wcsicmp(className, L"edit"))
            SendMessage(hFocused, EM_SETSEL, 0, -1);
    }
}
break;
case WM_DESTROY:
{
   DestroyAcceleratorTable(accel);
   PostQuitMessage(0);
}
break;
Run Code Online (Sandbox Code Playgroud)

如你所见,这很简单.

小智 9

无需处理WM_KEYDOWN!我知道这里的大多数例子(以及CodeProject和许多其他地方)都说有,但它无法治愈每当WM_CHAR出现时未处理的蜂鸣声.

相反,试试这个:

LRESULT CALLBACK Edit_Prc(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam){
  if(msg==WM_CHAR&&wParam==1){SendMessage(hwnd,EM_SETSEL,0,-1); return 1;}
  else return CallWindowProc((void*)WPA,hwnd,msg,wParam,lParam);
}
Run Code Online (Sandbox Code Playgroud)

记得使用WPA = SetWindowLong(...)将EDIT控件子类化为此Edit_Prc(),其中WPA是CallWindowProc(...)的窗口过程地址

  • 你们都太年轻了;)以前,Control- &lt;c&gt;是键的代码,映射到ASCII的较低范围(^ C = 3,^ G = bell,^ H = backspace等)。 (2认同)

Jic*_*hao 6

首先更改WindowProc以进行编辑控件:

if (!(pEditProc = (WNDPROC)SetWindowLong(hEdit, GWL_WNDPROC, (LONG)&EditProc)))
{
    assert(false);
    return false;
}
Run Code Online (Sandbox Code Playgroud)

然后在新窗口proc中,处理ctrl + a:

LRESULT CALLBACK EditProc( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam )
{
    if (msg == WM_KEYDOWN) {
        if (GetKeyState(VK_CONTROL) & 0x8000 && wParam == 'A') {
            SendMessage(hwnd, EM_SETSEL, 0, -1);
        }
    }
    return CallWindowProc(pEditProc, hwnd, msg, wParam, lParam);
}
Run Code Online (Sandbox Code Playgroud)