Skip to main content
GameDev.net gamedev.net
🔒 Locked

Win32 Multiple Buttons

Started by Paul-K Nov 30, 2008 at 10:40 AM 1 replies 550+ views
Original Post
Paul-K
Paul-K
Hello all, first my current code: #include LRESULT CALLBACK CallBackFunc(HWND Window,UINT Msg,WPARAM wParam,LPARAM lParam){ switch(Msg){ case WM_QUIT:PostQuitMessage(WM_QUIT);break; case WM_DESTROY:PostQuitMessage(WM_QUIT);break; case WM_COMMAND: char NumberClicked[10]; itoa(WM_COMMAND,NumberClicked,10); MessageBox(NULL,NumberClicked,"TypePressed",MB_OK); break; default:return DefWindowProc(Window, Msg, wParam, lParam); } return 0; } int WINAPI WinMain(HINSTANCE HoofdScherm,HINSTANCE PHoofdScherm,LPSTR lpCmdLine,int nCmdShow){ WNDCLASSEX MijnScherm; MijnScherm.cbClsExtra = NULL; MijnScherm.cbSize = sizeof(WNDCLASSEX); MijnScherm.cbWndExtra = NULL; MijnScherm.hbrBackground = (HBRUSH)GetStockObject(GRAY_BRUSH); MijnScherm.hCursor = LoadCursor(NULL,IDC_ARROW); MijnScherm.hIcon = LoadIcon(NULL,IDI_APPLICATION); MijnScherm.hIconSm = LoadIcon(NULL,IDI_APPLICATION); MijnScherm.hInstance = HoofdScherm; MijnScherm.lpfnWndProc = CallBackFunc; MijnScherm.lpszClassName = "MijnScherm"; MijnScherm.lpszMenuName = NULL; MijnScherm.style = CS_HREDRAW | CS_VREDRAW; RegisterClassEx(&MijnScherm); HWND MijnSchermWND = CreateWindowEx(NULL,"MijnScherm","Mijn Eerste Programma",WS_BORDER | WS_MAXIMIZE,CW_USEDEFAULT,CW_USEDEFAULT,680,480,NULL,NULL,NULL,NULL); if(!MijnSchermWND){MessageBox(NULL,"Scherm kon niet worden gemaakt","Fout!",MB_OK | MB_ICONSTOP);}else{ ShowWindow(MijnSchermWND,SW_SHOW); } HWND Button1 = CreateWindowEx(NULL,"BUTTON","Afsluiten",WS_TABSTOP | WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON,10,10,100,25,MijnSchermWND,NULL,(HINSTANCE)GetWindowLong(MijnSchermWND, GWL_HINSTANCE),NULL); HWND Button2 = CreateWindowEx(NULL,"BUTTON","Klik hier",WS_TABSTOP | WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON,10,35,100,25,MijnSchermWND,NULL,(HINSTANCE)GetWindowLong(MijnSchermWND, GWL_HINSTANCE),NULL); MSG Msg; while(GetMessage(&Msg,NULL,NULL,NULL)){ TranslateMessage(&Msg); DispatchMessage(&Msg); } return 0; } As you can see i have 2 Buttons, de problem is dat if the first button is pressed i want the program gets closed, and if the second button is pressed there is a messagebox. But now if you press one of them you get the same action. How can you split this Greets & Thx
Endurion
Endurion
To detect different buttons usually different IDs are used. For controls you use the HMENU param and simply put an integer in there (you may need to cast).

When WM_COMMAND is detected wParam contains the ID (lParam is the HWND of the control). Use either the ID or the HWND to determine the pushed button and act accordingly. You should get in the habit to also check for BN_CLICKED, there are other possible WM_COMMAND events.

case WM_COMMAND:  if ( HIWORD( wParam ) == BN_CLICKED )  {    if ( (HWND)lParam == Button1 )    {      // Action Button 1    }    else if ( (HWND)lParam == Button2 )    {      // Action Button 2    }  }  break;
Fruny: Ftagn! Ia! Ia! std::time_put_byname! Mglui naflftagn std::codecvt eY'ha-nthlei!,char,mbstate_t>
Paul-K
Paul-K
Oooh Yeah,

It works, THX Man!

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.