0

I've been afraid to ask as I'm sure the answer is very simple, but I've been bashing my head against the wall for several days on this one simple thing now and am making no forward progress.

I have a basic window with a checkbox inside of it like this (ignoring all the standard WndProc boilerplate), and am allowing dialog processing (as in my full application I have input fields that I allow tabbing through):

hwnd := CreateWindowEx(WS_EX_CLIENTEDGE, g_szClassName, "Sample", WS_VISIBLE + WS_SYSMENU, CW_USEDEFAULT, CW_USEDEFAULT, 350, 420, NIL, menu, MyInstance(), NIL);
gfhwnd := CreateWindowEx(WS_EX_CLIENTEDGE, "Button", "", WS_CHILD + BS_CHECKBOX + WS_TABSTOP, 250, 130, 17, 17, hwnd, NIL, MyInstance(), NIL);
ShowWindow(hwnd, SW_SHOWNORMAL);
ShowWindow(gfhwnd, SW_SHOWNORMAL);

 WHILE GetMessage( Msg, NIL, 0, 0) DO
    IF NOT IsDialogMessage(hwnd, Msg) THEN
        TranslateMessage(Msg);
        DispatchMessage(Msg);
    END; (* IF *)
 END; (* WHILE *)

My understanding is that the checkbox should straight out of the gate be enabled and can be checked on/off via click or keyboard. However, it won't. It's always off. If clicked (or keyboard activated), the button flashes briefly then returns to unchecked. If I capture the messages in WM_COMMAND it is always telling me that I clicked to "CHECK" the box. I never get an "UNCHECK" event. So, at least it is internally consistent. Since I'm getting messages it does appear to be enabled.

So far I've tried many variations on capturing the clicks and then forcing the checkbox in/out of the checked state (including assigning it custom IDs as well as not). However, out of all the various methods I've found via Google, none of them work. In all cases the checkbox stays quite firmly "unchecked". What tiny, but critical, piece of knowledge am I missing about how checkboxes work in Windows?

Brian Knoblauch
  • 20,639
  • 15
  • 57
  • 92
  • 1
    Assigning a Control ID is not necessary to make a CheckBox function properly. And 0 is a valid Control ID anyway. – Remy Lebeau Feb 13 '21 at 01:22
  • 1
    Small nitpick - bit flags should be combined with `or`, not `+`. It doesn't make a difference in *this* case, but it [*can* matter if you are not careful](https://softwareengineering.stackexchange.com/questions/23852/). – Remy Lebeau Feb 13 '21 at 01:27
  • @RemyLebeau Yeah, that's a problem I've been kicking down the road... My current compiler doesn't support the BOR operator that one would normally use and since the things I've run across so far are clean with add I've been cheating since I can get away with it... Not really sure on the best way to do it the right way at the moment. I know I can inject an ASM assembly block to do it, but that's kind of clunky. – Brian Knoblauch Feb 13 '21 at 14:21

0 Answers0