How to use multiple checkboxes

I'm a beginner and maybe my question is stupid...

I would like to create more CheckBoxes in a dialog window by using CreateWindow, sg like this:

CreateWindow(TEXT("BUTTON"), "Box1", WS_CHILD | WS_VISIBLE | SS_LEFT | ES_CENTER | BS_CHECKBOX, 10, 10, 100, 20,hWnd, (HMENU) 10,0, NULL);
CreateWindow(TEXT("BUTTON"), "Box2", WS_CHILD | WS_VISIBLE | SS_LEFT | ES_CENTER | BS_CHECKBOX, 100, 10, 100, 20,hWnd, (HMENU) 11,0, NULL);

This case just the "Box1" works. Could you advise how can I do that properly?

Thanks in advance.
Last edited on
Give each CheckBox a different location. In the above code you put the second one on top of the first.
Sorry, the problem was the copy/paste. It does not work when the buttons positions are correct.

It can generate a button push event only with Box1 button.
Make sure that the buttons don't overlap and also include BS_AUTOCHECKBOX
Here is a simple demo.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include <windows.h>
#include <tchar.h>
#include <crtdbg.h>

const int IDC_CHKBOX1 = 10;
const int IDC_CHKBOX2 = 11;

LRESULT CALLBACK WndProc (HWND hWnd, unsigned int msg, WPARAM wParam, LPARAM lParam)
{
  switch (msg)
  {
    case WM_CREATE:
    {
      HINSTANCE hInstance = ((LPCREATESTRUCT)lParam)->hInstance;
      CreateWindow (TEXT ("BUTTON"), "Box1", WS_VISIBLE | WS_CHILD | BS_CHECKBOX | BS_AUTOCHECKBOX, 
10, 10, 100, 20, hWnd, (HMENU)IDC_CHKBOX1, hInstance, NULL);
      CreateWindow (TEXT ("BUTTON"), "Box2", WS_VISIBLE | WS_CHILD | BS_CHECKBOX | BS_AUTOCHECKBOX,
 200, 10, 100, 20, hWnd, (HMENU)IDC_CHKBOX2, hInstance, NULL);
      return 0;
    }
    case WM_COMMAND:
    {
      return 0;
    }
    case WM_DESTROY:
    {
      PostQuitMessage (0);
      return 0;
    }
  }

  return (DefWindowProc (hWnd, msg, wParam, lParam));
}


int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevIns, LPSTR lpszArgument, int iShow)
{
  TCHAR szClassName[] = _T("Template");
  TCHAR szWindowName[] = _T("Template");
  WNDCLASSEX wc = { 0 };
  MSG messages;
  HWND hWnd;

  wc.lpszClassName = szClassName;
  wc.lpfnWndProc = WndProc;
  wc.cbSize = sizeof (WNDCLASSEX);
  wc.hbrBackground = (HBRUSH)COLOR_BTNSHADOW;
  wc.hInstance = hInstance;
  
  _ASSERTE(RegisterClassEx (&wc) !=0);
  
  hWnd = CreateWindowEx (0, szClassName, szWindowName, WS_OVERLAPPEDWINDOW, 
      CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, 
      HWND_DESKTOP, 0, hInstance, 0);
      
  _ASSERTE(::IsWindow(hWnd));
  
  ShowWindow (hWnd, iShow);
  while (GetMessage (&messages, NULL, 0, 0))
  {
    TranslateMessage (&messages);
    DispatchMessage (&messages);
  }

  return static_cast<int>(messages.wParam);
}
Thanks Thomas1965, it works!
Topic archived. No new replies allowed.