got a simply question, about createwindow() in creating label.

1
2
3
4
5
6
7
8
9
10
11
12
HWND hwndLabel = CreateWindow(
                        TEXT("STATIC"),                   /*The name of the static control's class*/
                        TEXT("Label 1"),                  /*Label's Text*/
                        WS_CHILD | WS_VISIBLE | SS_LEFT,  /*Styles (continued)*/
                        0,                                /*X co-ordinates*/
                        0,                                /*Y co-ordinates*/
                        50,                               /*Width*/
                        25,                               /*Height*/
                        hwnd,                             /*Parent HWND*/
                        (HMENU) 200,              /*The Label's ID*/
                        hlinstance,                        /*The HINSTANCE of your program*/ 
                        NULL);


How and when does the window use the HMENU 200 identifier? Is it like when we click the label text and the identifier will be triggered? And i read many guides about the second last parameter, but i can't really get what does the 'HINSTANCE of ur program' mean. I simply declare a HINSTANCE variable and put it into the createwindow(), the code still be compiled. Or it is just like another identifier?
identifier is an integer value used by a control to notify its parent about events. For example, when you click on a button, it sends a WM_COMMAND message to the parent and its ID as a WPARAM so you can identify which button was clicked.

For example, you can set the label text in two ways
1
2
SetDlgItemText(hwndParent,200,"Test 1");
SetWindowText(hwndLabel,"Test 2");
The HINSTANCE parameter of a CreateWindow call tells Windows (the operating system) the process the call is associated with. You can think of it as an id number if you like. If your CreateWindow calls are in WinMain() you'll get the value from the HINSTANCE parameter of WinMain(). You can get it anywhere else easily enough too with a call to GetModuleHandle(). Its also indirectly found in the WM_CREATE handler.
Thanks freddie, that make hinstance sound a bit clearer to me. But for button, the id is used when clicked, but for a static label how are we going to invoke its id? The static seem to be a non-interactive to user?
Static control usually does nothing but display text but it can be interactive. You can specify SS_NOTIFY style when creating the static control and it will send messages to its parent when clicked or double-clicked

Example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
     HWND hwndLabel = CreateWindow(
                        TEXT("STATIC"),TEXT("Label 1"),
                        WS_CHILD | WS_VISIBLE | SS_LEFT| SS_NOTIFY,
                        0,0,50,25,
                        hwnd, (HMENU) 200, g_hInst,NULL);
// ...

// in window procedure
    case WM_COMMAND:
    {
        switch(HIWORD(wParam)) // notification code
        {
           case STN_CLICKED:
               if(LOWORD(wParam)==200) // loword part of wparam is the control ID
               {
                   MessageBox(hwnd,"You have clicked the static control","Msg",0);
               }
            break;
        }

    }
    break;


http://msdn.microsoft.com/en-us/library/windows/desktop/bb760769(v=vs.85).aspx
http://msdn.microsoft.com/en-us/library/windows/desktop/bb760784(v=vs.85).aspx
I see, thanks guys.
Topic archived. No new replies allowed.