win-32 button response

Hello everyone, I am finding it hard to find how you make a button, created with 'CreateWindowEx()' in the 'WinMain()' function, to respond when clicked. For example click a button titled 'okay' and then a popup would appear. My guess is it is to do with the 'WinProc()' message handler and an identifier, but I cannot work it out how or what... Does anyone know how to resolve this issue without the use of a dialog box?
closed account (LTXN8vqX)
in your winproc, make a "case WM_CREATE", and in there make your button as an initialization of a variable like this:

1
2
3
4
5
6
7
8
9

static HWND button;

........

case WM_CREATE:

button = CreateWindow (TEXT("button"),.............);



Then to get messages from the button when it is pressed you can do this in the WM_COMMAND message.:

1
2
3
4
5
6
7
8
9
10
11

case WM_COMMAND:

    switch(wParam)
 {
    case "Your id for your button which should be one of your parameters in CreateWindow":
     whatever you want to happen here.... i.e.: MessageBox function
   default:
    return 0;
   }




Last edited on
1
2
3
4
5
6
7
8
9
case WM_COMMAND:

    switch(wParam)
 {
    case "Your id for your button which should be one of your parameters in CreateWindow":
     whatever you want to happen here.... i.e.: MessageBox function
   default:
    return 0;
   }




That isn't quite right.
The control ID is in the LOWORD of the wParam. The HIWORD of the wParam contains a notification code.


So you should be doing something like:
1
2
3
4
5
6
7
8
9
case WM_COMMAND:

    switch(LOWORD(wParam) ) //check the ID portion of the wParam
 {
    case "Your id for your button which should be one of your parameters in CreateWindow":
     whatever you want to happen here.... i.e.: MessageBox function
   default:
    return 0;
   }
Unfortunately this leaves me with a problem, the hInstance is no longer in scope of the CreateWindow() in WinProc(). Originally I was creating the controls within WinMain(). How would I feed the hInstance to the message handler, and also is there a way to do this from within WinMain without using the 'case WM_CREATE:'?

Thanks for your help!

Edit: I have solved the problem, it was because I was not using the 'LOWORD(wParam)'. However, I am still interested as to how control creation would work within the WM_CREATE... again, thanks for your help!
Last edited on
Regarding the hInstance, you could always declare a global
HINSTANCE hInst;
And inside WinMain(HINSTANCE hInstance,...) assign it:
hInst=hInstance;
Now you'll be able to create windows anywhere in your code, by passing hInst as parameter.
Topic archived. No new replies allowed.