Home > Back-end >  How do I add Control-A as a Hotkey to all EDITTEXT controls in a dialog box?
How do I add Control-A as a Hotkey to all EDITTEXT controls in a dialog box?

Time:05-25

I have a bunch of dialog boxes created with DialogBox() from definitions in a resource file. The dialog boxes have a bunch of controls created with EDITTEXT statements (and some others).

I have noticed that Ctrl A does not work as a hotkey for selecting all text. How do I add it?

"Select All" appears in the popup context menu, and works correctly if chosen by mouse.

I cannot use a manifest because I am in a DLL, and DLLs do not have manifests.

CodePudding user response:

Edit controls don't support Ctrl A natively (even though they have this command in their context menu).

If you want to add Ctrl A handling to an edit control you can do it through sub-classing.

For example,

LRESULT CALLBACK CtrlASubProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam, UINT_PTR uIdSubclass, DWORD_PTR dwRefData)
{
    if (uMsg == WM_DESTROY)
        RemoveWindowSubclass(hWnd, CtrlASubProc, uIdSubclass);
    else if (uMsg == WM_GETDLGCODE && wParam == 'A')
    {
        if (GetKeyState(VK_CONTROL) < 0)
            return DLGC_WANTALLKEYS | DLGC_WANTMESSAGE;
    }
    else if (uMsg == WM_CHAR && wParam == 1) // ctrl-A
    {
        SendMessage(hWnd, EM_SETSEL, 0, -1);
        return 0;
    }
    return DefSubclassProc(hWnd, uMsg, wParam, lParam);
}


void AddCtrlAHandlingToEditControl(HWND hwndEdit)
{
    SetWindowSubclass(hwndEdit, CtrlASubProc, 0, 0);
}
    
  • Related