Select all text in edit contol by clicking Ctrl+A

visual-c++, winapi

Solution

No need to handle WM_KEYDOWN! I know that most examples here (and CodeProject and many other places) all say there is, but it does not cure the beep that results whenever a WM_CHAR arises that is not handled.

Instead, try this:

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

Remember to subclass the EDIT control to this Edit_Prc() using WPA=SetWindowLong(...) where WPA is the window procedure address for CallWindowProc(...)

Problem

How to select all text in edit control by pressing Ctrl+A? I can catch Ctrl+A for parent window in WndProc. But I don't know how to catch ctrl+a which are applied for edit control. Also I tried to use accelerators, but again it applies only for parent window. Thanks. EDIT: 1-st the simplest method This method Based on @phord's answers in this question: win32 select all 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); } } ``` EDIT: 2-nd method Need to use CreateAcceleratorTable + TranslateAccelerator functions: //global variables: ``` enum {ID_CTRL_A = 1}; HACCEL accel; ``` //main procedure ``` 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 ``` //How GetMessage loop looks ``` 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); } } } ``` //in WndProc we must add next cases ``` 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; ``` As you can see this is pretty simple.

Original source

Related problems