Cross initialization of HWND??

I am adding stuff to my program and making it work at the same time, this program is to help me by showing working examples of code i can refrence to, but it gives me an error when i run it, here is the code from main.cpp:

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
85
#define WIN32_LEAN_AND_MEAN
#define _WIN32_WINNT 0x0500
#include <windows.h>
#include "resource.h"
#include <iostream>

HINSTANCE hInst;

BOOL CALLBACK DialogProc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
{

    switch(uMsg)
    {
        case WM_INITDIALOG:
            //Blank
            return TRUE;

        case WM_CLOSE: // If the user clicks the X to close the dialog box
            EndDialog(hwndDlg, 0);
            return TRUE;

        case WM_COMMAND:

            switch(LOWORD(wParam))
            {
                case IDC_BTN_QUIT:
                    EndDialog(hwndDlg, 0);
                    return TRUE;

                ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
                //MB_ICONINFORMATION is the type of icon displayed in the window that pops up. There is also//
                //MB_ICONHAND, MB_ICONSTOP, or MB_ICONERROR - Error Icon                                           //
                //MB_ICONQUESTION - Question mark Icon                                                                          //
                //MB_ICONEXCLAMATION or MB_ICONWARNING - Exclamation Icon                                       //
                //MB_ICONASTERISK or MB_ICONINFORMATION - Asterisk Icon                                            //
                /////////////////////////////////////////////////////////////////////////////////////////////////////////////

                case IDC_BTN_TEST:
                    MessageBox(hwndDlg, "Your computer will now crash because you suck.", "Information", MB_ICONERROR);
                    return TRUE;

                case IDC_BTN_TESTBTN2:
                    MessageBox(hwndDlg, "Rated Argh for pirates fuck you.", "Information", MB_ICONWARNING);
                    return TRUE;

                case Button:
                    MessageBox(hwndDlg, "Test Button 2", "Information", MB_ICONINFORMATION);
                    return TRUE;

               case CheckB:
                    if (BST_CHECKED == IsDlgButtonChecked(hwndDlg, CheckB))
                    {
                    std::cout << "checked" << std::endl;
                    } else {
                    std::cout << "Unchecked" << std::endl;
                    }
                    return TRUE;

                case Greyout:
                    //MessageBox(hwndDlg, "When clicked this box will grey out, causing you to no longer click it", "Information", MB_ICONASTERISK);

                    HWND hGreyout = GetDlgItem (hwndDlg, Greyout);
                    EnableWindow (hGreyout, FALSE);
                    return TRUE;
                    break;

                case hide:
                    HWND hideBtn = GetDlgItem(hwndDlg, Button2);
                    ShowWindow(hideBtn, SW_HIDE);
                    return TRUE;
                    break;

            }
    }

    return FALSE;
}

int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
    hInst = hInstance;

    // The user interface is a modal dialog box
    return DialogBox(hInstance, MAKEINTRESOURCE(DLG_MAIN), NULL, (DLGPROC)DialogProc);
}


Here are the errors:

C:\Users\Chay Hawk\Desktop\Chays Stuff\C++ Stuff\C++ Projects\Win API stuff\main.cpp||In function 'BOOL DialogProc(HWND__*, UINT, WPARAM, LPARAM)':|
C:\Users\Chay Hawk\Desktop\Chays Stuff\C++ Stuff\C++ Projects\Win API stuff\main.cpp|67|error: jump to case label|
C:\Users\Chay Hawk\Desktop\Chays Stuff\C++ Stuff\C++ Projects\Win API stuff\main.cpp|62|error: crosses initialization of 'HWND__* hGreyout'|
||=== Build finished: 2 errors, 0 warnings ===|

I cant have 2 HWNDS in my program???
closed account (DSLq5Di1)
http://stackoverflow.com/questions/92396/why-cant-variables-be-declared-in-a-switch-statement
http://stackoverflow.com/questions/3757445/switch-case-declaration-with-initialization-declaration-and-then-assignment
Ok, but if you take out:

1
2
3
4
5
           case hide:
                    HWND hideBtn = GetDlgItem(hwndDlg, Button2);
                    ShowWindow(hideBtn, SW_HIDE);
                    return TRUE;
                    break;


The other one exactly like it works so why is this one a problem?

This is the one that works, it compiles and everything:

1
2
3
4
HWND hGreyout = GetDlgItem (hwndDlg, Greyout);
                    EnableWindow (hGreyout, FALSE);
                    return TRUE;
                    break;


i followed the same format so why is it a problem now? i dont get it?
This ia all to do with local variables declared in a case block withing a switch statement.

Example;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
 switch (x)
{
      case 3:
      int y = SomeFunction(); //initialisation of a local variable
      //more code
      break;

     case 4:
      //more code
      break;

      default:
      break;
)


The variable y has scope right up to the end of the switch block - which means that it can be used
in the case 4: block - but it (the variable) only gets initialised in the case 3: block
As you can see this will be a problem.

If you want to localise a variable to a case block then do it using { and } to set a local scope.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
 switch (x)
{
      case 3: //use a local scope {}to limit variable y to this case block
     {
       int y = SomeFunction(); //initialisation of a local variable
      //more code
      break;
     }

     case 4:
      //more code
      break;

      default:
      break;
)


So in your original code:
1
2
3
4
5
6
7
8
 case Greyout:
  {
      //MessageBox(hwndDlg, "When clicked this box will grey out, causing you to no longer click it", "Information", MB_ICONASTERISK);
      HWND hGreyout = GetDlgItem (hwndDlg, Greyout);
      EnableWindow (hGreyout, FALSE);
      return TRUE;
  }
  break;




*****************
As for your additional question about why you don't get the error for case hide: - it is bcause it is the last case in the switch block - if you were to add another case you would get
an error for case hide: as well
Last edited on
oooooohhh, i see now! I didnt know you could use Curly brackets in case statements! so i learned 2 nw things. Thanks!

Also is there a way to create tabs with a control word in my Resource.rc file? when i say tabs i mean the ones like in your web browser so you can have multiple pages open.
Last edited on
Topic archived. No new replies allowed.