Click to See Complete Forum and Search --> : Button ID


veigo18
January 17th, 2006, 07:41 AM
Hello

I made two buttons and want to recognise which button is clicked by user.
Found at MSDN that "For a child window, hMenu specifies the child-window identifierin" in CreateWindow function. But compiler gives error "invalid converison from int to HMENU_".

#define ID_BTN1 401

hwndButton = CreateWindow(
"BUTTON", // predefined class
"OK", // button text
WS_VISIBLE | WS_CHILD | BS_PUSHBUTTON | BS_FLAT, // styles
250, // starting x position
250, // starting y position
40, // button width
40, // button height
hwnd, // parent window
ID_BTN1, //
(HINSTANCE) GetWindowLong(hwnd, GWL_HWNDPARENT),
NULL); // pointer not needed

what's wrong here?


Is it correct to use following code if I want to check which button was clicked?

case WM_COMMAND:
if (HIWORD(wParam) == BN_CLICKED)
{
switch (lParam)
{
case ID_BTN1:
...

case ID_BTN2:
...

Thanks in advance!

bilm_ks
January 17th, 2006, 08:05 AM
case WM_COMMAND:
if (HIWORD(wParam) == BN_CLICKED)
{
switch (LOWORD(wParam))
{
case ID_BTN1:
...

case ID_BTN2:

kkez
January 17th, 2006, 08:20 AM
First problem: you must cast the int to a HMENU variable:
CreateWindow(...., hwnd, (HMENU)ID_BTN1, ...);
//or
CreateWindow(...., hwnd, HMENU(ID_BTN1), ...);
It's the same.

Second problem:
case WM_COMMAND:
{
switch(LOWORD(wParam))
{
case ID_BTN1:
//...
break;
case ID_BTN2:
//..
break;
}
}
break;

veigo18
January 17th, 2006, 09:02 AM
It works :) Thank you very much!