c++cwinapieditcontrol

Set Extended Styles on Win32 Edit Control


Is it possible to set extended styles on Edit control (simple Edit, not Rich Edit)? For example, I want to set the extended style to WS_EX_ZOOMABLE | WS_EX_ALLOWEOL_ALL. The creation of the control is as follows:

HWND hEdit = CreateWindowExW(
        ES_EX_ZOOMABLE | ES_EX_ALLOWEOL_ALL,
        L"EDIT",
        L"",
        WS_BORDER | WS_CHILD | WS_VISIBLE | WS_VSCROLL | WS_HSCROLL | ES_LEFT | ES_MULTILINE | ES_AUTOVSCROLL | ES_AUTOHSCROLL,
        0, 0, 100, 100,
        hWndMain, (HMENU)ID_EDIT, hInstance, NULL
    );

The problem is that none of the extended styles work. The EOL is still CR LF and the control isn't zoomable.


Solution

  • As noted in comments, edit control extended styles are set by sending the control an EM_SETEXTENDEDSTYLE message:

    HWND hEdit = ::CreateWindow(
            L"EDIT",
            L"",
            WS_BORDER | WS_CHILD | WS_VISIBLE | WS_VSCROLL | WS_HSCROLL | ES_LEFT | ES_MULTILINE | ES_AUTOVSCROLL | ES_AUTOHSCROLL,
            0, 0, 100, 100,
            hWndMain, (HMENU)ID_EDIT, hInstance, NULL
        );
    
    DWORD exStyles = ES_EX_ZOOMABLE | ES_EX_ALLOWEOL_ALL;
    ::SendMessage( hEdit, EM_SETEXTENDEDSTYLE, exStyles, exStyles );
    

    You pass the style bits you want to modify as the wParam argument (the mask) and you pass the new values of these bits as the lParam argument. This allows you to both set and clear styles using a single call, without having to query for the previous values of these styles. This is a very common pattern used by many APIs.

    If you only want to enable these styles, set wParam and lParam to the same values, as I did in the code sample above.

    If you want to clear style bits, leave out the ones you want to clear from the lParam argument. E. g. to set ES_EX_ZOOMABLE but clear ES_EX_ALLOWEOL_ALL:

    ::SendMessage( hEdit, EM_SETEXTENDEDSTYLE, ES_EX_ZOOMABLE | ES_EX_ALLOWEOL_ALL, ES_EX_ZOOMABLE );
    

    To clear both bits:

    ::SendMessage( hEdit, EM_SETEXTENDEDSTYLE, ES_EX_ZOOMABLE | ES_EX_ALLOWEOL_ALL, 0 );
    

    Manifest requirements

    For the extended styles to actually work, you need to specify version 6.0 of the common controls in the application manifest. One of the easiest ways to do this is to insert the following compiler directive in your code, typically in the precompiled header file:

    #pragma comment(linker,"\"/manifestdependency:type='win32' \
    name='Microsoft.Windows.Common-Controls' version='6.0.0.0' \
    processorArchitecture='*' publicKeyToken='6595b64144ccf1df' language='*'\"")
    

    Background information

    This is likely how the control handles EM_SETEXTENDEDSTYLE in terms of bit-wise operations:

    auto newExStyle = currentExStyle & ~wParam | lParam;