Search code examples
c++user-interfacewinapiwindows-11

Checkbox control in Win32 API is rounded in a deformed manner in Windows 11


As you can see in the following images:

there are unfilled areas when the checkbox is clicked(mainly in the corners). I have enabled visual styles through

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

And I have also called InitCommonControlsEx

INITCOMMONCONTROLSEX controls = { sizeof(INITCOMMONCONTROLSEX), ICC_STANDARD_CLASSES };
if (!InitCommonControlsEx(&controls)) { MessageBoxA(NULL, "Loading controls failed\n", NULL, 0); }

Here is my code for creating the button in WM_CREATE:

int cx_check_box = GetSystemMetrics(SM_CXMENUCHECK) - GetSystemMetrics(SM_CXEDGE);
int cy_check_box = GetSystemMetrics(SM_CYMENUCHECK) - GetSystemMetrics(SM_CYEDGE);
HWND check_box = CreateWindowW(L"BUTTON", L"", WS_VISIBLE | WS_CHILD | BS_AUTOCHECKBOX, 100, 100,
cx_check_box, cy_check_box, hWnd, NULL, (HINSTANCE)GetWindowLongPtr(hWnd, GWLP_HINSTANCE), NULL);

if (!check_box)
    MessageBoxA(NULL, "check box creation failed :(", NULL, MB_RETRYCANCEL);

How can I go about making the checkbox occupy the entire space? Or make the extra space go away/transparent? Thanks in advance for the help.

Windows SDK version - 10.0.22621.0 (which is windows 11 afaik)


Solution

  • You can handle the WM_CTLCOLORBTN message and set the background to Red.

    enter image description here

    case WM_CTLCOLORSTATIC: 
        {
            static HBRUSH hBrushColor;
            if (!hBrushColor)
            {
                hBrushColor = CreateSolidBrush(RGB(255, 0, 0));
                SetBkColor((HDC)wParam,RGB(255, 0, 0));
            }
            return (LRESULT)hBrushColor;
        }
        break;