ownerdrawn checkbox

blejzz picture blejzz · Oct 30, 2011 · Viewed 7.4k times · Source

i have a native win32 c++ application which has a checkbox in it. I want to replace the checkbox and create ON/OFF toggle button with 2 states (just like the checkbox). I've added the BS_OWNERDRAW style to the checkbox and drawn it to the window in WM_DRAWITEM. The problem is that when I click on the checkbox I get a WM_COMMAND message (just like without the BS_OWNERDRAW) but the CHECKED state doesn't change automaticly. Do I have to implement this functionality or am I missing something?

The code that handles clicking on the checkbox:

 case WM_COMMAND:
    wmId    = LOWORD(wParam);
    wmEvent = HIWORD(wParam);

    switch (wmId)
    {
        case IDC_CHECKBOX:
            if(wmEvent == BN_CLICKED)
            {
                dwPos = SendMessage(checkBox, BM_GETCHECK, 0, 0);
                if(dwPos == BST_CHECKED ) 
                {
                   // do some stuff

                } else if(dwPos == BST_UNCHECKED) {
                    // do some stuff
                }
           }

The problem is that every time i click on the checkbox BM_GETCHECK returns BST_UNCHECKED. If i remove the BS_OWNERDRAW it works fine.

Code that creates the button/checkbox:

   checkBox = CreateWindowEx(
        0,
        WC_BUTTON,
        szBuffer,
        WS_VISIBLE | WS_CHILD | BS_AUTOCHECKBOX | BS_OWNERDRAW,
        BUTTON_ON_OFF_X, BUTTON_ON_OFF_Y,
        BUTTON_ON_OFF_WIDTH, BUTTON_ON_OFF_HEIGHT,
        hWnd, 
        (HMENU)IDC_CHECKBOX,
        hInst,
        NULL
   );

Answer

cbranch picture cbranch · Oct 30, 2011

Ordinarily, you would set BS_AUTOCHECKBOX to have the checkbox check/uncheck automatically in response to user input. However, according to the docs, you cannot combine other styles (e.g., BS_AUTOCHECKBOX) when using BS_OWNERDRAW.

http://msdn.microsoft.com/en-us/library/windows/desktop/bb775951%28v=vs.85%29.aspx