Checkbox

Okay so I am attempting to make a checkbox that the user selects to create a shortcut on his/her desktop. I In my program i have a progress bar and when ever someone hits the checkbox the progress bar starts and does what it's suppost to do. Do you know how i can make it so that this doesn't happen and to check to see if the button is pressed. Here is the code for it:

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
  switch(msg)  
  {
      case WM_CREATE:
           	    CreateWindow(TEXT("button"), TEXT("Create Shortcut"),
                     WS_VISIBLE | WS_CHILD | BS_CHECKBOX,
                     20, 100, 185, 35,        
                     hwnd, (HMENU) 1, ((LPCREATESTRUCT)lParam)->hInstance, NULL);
	    CheckDlgButton(hwnd, 1, BST_CHECKED);
	    
           CreateWindow(TEXT("STATIC"), Title, 
		    WS_CHILD | WS_VISIBLE | SS_LEFT,
		    50, 20, 300, 230,
		    hwnd, (HMENU) 1, NULL, NULL);  
		    
		    
          hwndPrgBar = CreateWindowEx(0, PROGRESS_CLASS, NULL, 
                WS_CHILD | WS_VISIBLE | PBS_SMOOTH,
                50, 100, 190, 25, hwnd, NULL, g_hinst, NULL);   

          CreateWindow(TEXT("button"), TEXT("Install"), 
                WS_CHILD | WS_VISIBLE,
                100, 70, 80, 25, hwnd, (HMENU) 1, g_hinst, NULL);  

          SendMessage(hwndPrgBar, PBM_SETRANGE, 0, MAKELPARAM(0, 150));
          SendMessage(hwndPrgBar, PBM_SETSTEP, 1, 0 );
          break;


      case WM_TIMER:
          SendMessage( hwndPrgBar, PBM_STEPIT, 0, 0 );
          i++;
          if ( i == 150 ) 
              KillTimer(hwnd, ID_TIMER);
          break;
              
      case WM_COMMAND:
	        CheckDlgButton(hwnd, 1, BST_UNCHECKED);
		    CheckDlgButton(hwnd, 1, BST_CHECKED);

	    
          i = 1;
          SendMessage( hwndPrgBar, PBM_SETPOS, 0, 0 );
          SetTimer(hwnd, ID_TIMER, 5, NULL);
          mkdir("C:\\Program Files (x86)\\Database FX");
          //MoveFile("F:\\Computer Programming\\Database\\Database_FX.exe", "C:\\Program Files (x86)\\Database FX\\Database_FX.exe");
          CopyFile("F:\\Computer Programming\\Database\\Database_FX.exe", "C:\\Program Files (x86)\\Database FX\\Database_FX.exe", TRUE);
          break;

      case WM_DESTROY:
          KillTimer(hwnd, ID_TIMER);
          PostQuitMessage(0);
          break; 
      
  }
  return DefWindowProc(hwnd, msg, wParam, lParam);
I think you probably want BS_AUTOCHECKBOX instead
Where exactly should i put that? Im fairly new to WIN32 api.
You have :

1
2
3
4
CreateWindow(TEXT("button"), TEXT("Create Shortcut"),
                     WS_VISIBLE | WS_CHILD | BS_CHECKBOX,
                     20, 100, 185, 35,        
                     hwnd, (HMENU) 1, ((LPCREATESTRUCT)lParam)->hInstance, NULL);


Which won't automatically update the status of the checkbox between clicks unless you make the change mentioned previously.
Also you will want to actually key off the install button instead of just lumping all that code after WM_COMMAND:

And how would i go about fixing the key off?
One of the problems with your application is that you aren't even capturing the HWND of the button control. You are capturing the HWND of the progress bar, so just change the code to save the HWND of the install button as well. You can then get the control ID of the window with GetDlgCtrlID(). Something like this:

1
2
3
4
5
6
7
HWND hInstallButton;
int idInstallButton;
hInstallButton = CreateWindow(TEXT("button"), TEXT("Create Shortcut"),
                     WS_VISIBLE | WS_CHILD | BS_AUTOCHECKBOX,
                     20, 100, 185, 35,        
                     hwnd, (HMENU) 1, ((LPCREATESTRUCT)lParam)->hInstance, NULL);
idInstallButton = GetDlgCtrlID(hInstallButton);


Later on in the code you'd use it like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
  switch(message)
  {
    case WM_COMMAND:
    {
      switch(LOWORD(wParam))
      {
        case idInstallButton:
        {
          // this means the user hit the install button
        }
      }
    } break;
  }
  return DefWindowProc(hWnd, message, wParam, lParam);
}

I tried this and i got the error: `idInstallButton' cannot appear in a constant-expression

this is what i did:
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
      case WM_CREATE:
           HWND hInstallButton;
           int idInstallButton;
           hInstallButton = CreateWindow(TEXT("button"), TEXT("Create Shortcut"),
                     WS_VISIBLE | WS_CHILD | BS_AUTOCHECKBOX,
                     20, 100, 185, 35,        
                     hwnd, (HMENU) 1, ((LPCREATESTRUCT)lParam)->hInstance, NULL);
           idInstallButton = GetDlgCtrlID(hInstallButton);
 	       CheckDlgButton(hwnd, 1, BST_UNCHECKED);
	    
           CreateWindow(TEXT("STATIC"), Title, WS_CHILD | WS_VISIBLE | SS_LEFT, 50, 20, 300, 230, hwnd, (HMENU) 1, NULL, NULL);  
		    
		    
          hwndPrgBar = CreateWindowEx(0, PROGRESS_CLASS, NULL, WS_CHILD | WS_VISIBLE | PBS_SMOOTH, 50, 100, 190, 25, hwnd, NULL, g_hinst, NULL);   

          CreateWindow(TEXT("button"), TEXT("Install"), WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON, 100, 70, 80, 25, hwnd, (HMENU) 1, g_hinst, NULL);  

        SendMessage(hwndPrgBar, PBM_SETRANGE, 0, MAKELPARAM(0, 150));
        SendMessage(hwndPrgBar, PBM_SETSTEP, 1, 0 );
          break;


      case WM_TIMER:
          SendMessage( hwndPrgBar, PBM_STEPIT, 0, 0 );
          i++;
          if ( i == 150 ) 
              KillTimer(hwnd, ID_TIMER);
          break;
              
      case WM_COMMAND:
		    switch (LOWORD(wParam))
            {
                case idInstallButton:
                     {
                    i = 1;
                    SendMessage( hwndPrgBar, PBM_SETPOS, 0, 0 );
                    SetTimer(hwnd, ID_TIMER, 5, NULL);
                    mkdir("C:\\Program Files (x86)\\Database FX");
                    CopyFile("F:\\Computer Programming\\Database\\Database_FX.exe", "C:\\Program Files (x86)\\Database FX\\Database_FX.exe", TRUE);
                    }
                }
          break;
My bad. Make it `const int idInstallbutton` instead.

What line is throwing that error? 33?

I am not used to creating controls dynamically so never really have come up against this problem...
Last edited on
105 is the line the errors on where it says case idInstallButton + the const didn't work either
closed account (DSLq5Di1)
You need to pass a unique identifier to the HMENU parameter of CreateWindow for each child window,

enum { ID_CREATE_SHORTCUT, ID_TITLE, ID_PROGRESS, ID_INSTALL }; // etc..

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
      case WM_CREATE:
            CreateWindow(... , (HMENU) ID_CREATE_SHORTCUT, ...);
            CheckDlgButton(hwnd, ID_CREATE_SHORTCUT, BST_CHECKED);
            ...

      case WM_COMMAND:
            switch (LOWORD(wParam))
            {
            case ID_INSTALL: // install button pressed
                  if (IsDlgButtonChecked(hwnd, ID_CREATE_SHORTCUT) == BST_CHECKED)
                  {
                        // create shortcut is checked...
                  }
                  else
                  {
                        // ...
                  }
                  break;

            }
            break;

Thanks ! it works ! One more question: When ever i run the program it only shows 2 letters of the checkbox title (Create ShortCut) and then if i cilck the check box and then unclick it then hit install it shows the full title? do you no how i can fix this?
closed account (DSLq5Di1)
I think something may be overlapping the checkbox?
Last edited on
This is what i have:

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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
  static HWND hwndPrgBar;
  static int i = 1;
  static char *Title = TEXT("         Database FX Installer \n\
Developed By: MacKenzie Whipp");
  INITCOMMONCONTROLSEX InitCtrlEx;

  InitCtrlEx.dwSize = sizeof(INITCOMMONCONTROLSEX);
  InitCtrlEx.dwICC  = ICC_PROGRESS_CLASS;
  InitCommonControlsEx(&InitCtrlEx);
  enum{ID_CREATE_SHORTCUT, ID_TITLE, ID_PROGRESS, ID_INSTALL };
  switch(msg)  
  {
      case WM_CREATE:
           HWND hInstallButton;
           int idInstallButton;
         hInstallButton = CreateWindow(TEXT("button"), TEXT("Create Shortcut"),
                  WS_VISIBLE | WS_CHILD | BS_AUTOCHECKBOX,
                  20, 125, 185, 35,        
                  hwnd, (HMENU) ID_CREATE_SHORTCUT, ((LPCREATESTRUCT)lParam)->hInstance, NULL);

           idInstallButton = GetDlgCtrlID(hInstallButton);
 	       CheckDlgButton(hwnd, ID_CREATE_SHORTCUT, BST_UNCHECKED);
	    
           CreateWindow(TEXT("STATIC"), Title, WS_CHILD | WS_VISIBLE | SS_LEFT, 50, 20, 300, 230, hwnd, NULL, NULL, NULL);  

 
		    
		    
          hwndPrgBar = CreateWindowEx(0, PROGRESS_CLASS, NULL, WS_CHILD | WS_VISIBLE | PBS_SMOOTH, 50, 100, 190, 25, hwnd, NULL, g_hinst, NULL);   

          CreateWindow(TEXT("button"), TEXT("Install"), WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON, 100, 70, 80, 25, hwnd, (HMENU) ID_INSTALL, g_hinst, NULL);  

        SendMessage(hwndPrgBar, PBM_SETRANGE, 0, MAKELPARAM(0, 150));
        SendMessage(hwndPrgBar, PBM_SETSTEP, 1, 0 );
          break;


      case WM_TIMER:
          SendMessage( hwndPrgBar, PBM_STEPIT, 0, 0 );
          i++;
          if ( i == 150 ) 
              KillTimer(hwnd, ID_TIMER);
          break;
              
      case WM_COMMAND:
		    switch (LOWORD(wParam))
            {
                case ID_CREATE_SHORTCUT:
                     {
                                if(IsDlgButtonChecked(hwnd, ID_CREATE_SHORTCUT) == BST_CHECKED)
                                {
                                                            //CREATE SHORTCUT IF CHECKED
                                                            
                                }
                                else
                                {
                                    //dont
                                }
                                break;
                 }
                    break;
                case ID_INSTALL:
                     {

                         i = 1;
                        SendMessage( hwndPrgBar, PBM_SETPOS, 0, 0 );
                        SetTimer(hwnd, ID_TIMER, 5, NULL);
                        mkdir("C:\\Program Files (x86)\\Database FX");
                        CopyFile("F:\\Computer Programming\\Database\\Database_FX.exe", "C:\\Program Files (x86)\\Database FX\\Database_FX.exe", TRUE);
                        MessageBox(0,"Installation Successful", "Installation Complete", MB_OK );
                        PostQuitMessage(0);
                        
                     }
                        
                     
            }
          break;

      case WM_DESTROY:
          KillTimer(hwnd, ID_TIMER);
          PostQuitMessage(0);
          break; 
      
  }


+ do you know how to make it so that the message box for the instalation completion to come up once the progress bar finish's?
Why don't you just use SHFileOPeration() function ? This is what windows explorer uses when copying a file, so you get thr progress bar and a cancel button for free.
Im not quite sure how to use this function in my program?
Topic archived. No new replies allowed.