disable win32 button NON MFC

i have built a simple window with a button in it and when i click the button i want to disable it, i have tried to use EnableWindow(GetDlgItem(hwnd, button1), 0); but this does not work and also gives me an error when compiling :

error: invalid conversion from `HWND__*' to `int'
error: initializing argument 2 of `HWND__* GetDlgItem(HWND__*, int)'

here's the code for my buuton is this correct?:

HWND button1;
switch (message) /* handle the messages */
{
case WM_CREATE:
{
button1 = CreateWindow("BUTTON", "Test", WS_BORDER | WS_CHILD | WS_VISIBLE, 200,230,100,40, hwnd, (HMENU) 1, NULL, NULL );
}
break;

here the button command:

case WM_COMMAND:
if (LOWORD(wParam) == 1 )
{
EnableWindow(GetDlgItem(hwnd, button1), false);
break;
}

any ideas or advice will be appreciated
Thanks in advance

GetDlgItem gets the HWND from a resource ID.

Since you did not create the button from a resource, you do not need to use it. You already have the HWND for the button.

Therefore you can just call EnableWindow directly:

 
EnableWindow(button1, false);

The GetDlgItem function takes a handle to the dialog window and the second parameter is the ID of the dialog item and return s the HWND of the item.
You are passing a handle to a window as the second item.

So it should be:
EnableWindow(GetDlgItem(hwnd, 1), false); //ID of button, NOT the handle

Anyway, you don't need to use GetDlgItem function, because in the WM_COMMAND, if the
message is generated by a child window, then the ID is LOWORD(wParam), and lParam is
the child window handle.

1
2
3
4
5
6
    case WM_COMMAND:
        if (LOWORD(wParam) == 1 ) //check wParam against child window ID
        {
            EnableWindow( (HWND)lParam,  false);
        }
        break;   


Last edited on
Thanks guestgulkan it worked great :-)

Thanks for your answer aswell Disch i did try your way but although the compiler gave me no errors it just never done anything when i clicked the button but that could be my fault as you havent seen the rest of the code.

Thanks again for your help guys very much appreciate it
glenc70 wrote:
Thanks for your answer aswell Disch i did try your way but although the compiler gave me no errors it just never done anything when i clicked the button but that could be my fault as you havent seen the rest of the code.


For that particular solution:

HWND button1; declaration would would have to be changed to either
a. global variable
b. static local variable in the windows procedure.
Topic archived. No new replies allowed.