Jump to content
  • Advertisement
Sign in to follow this  
Paul-K

Win32 Multiple Buttons

This topic is 3609 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello all, first my current code: #include <windows.h> 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

Share this post


Link to post
Share on other sites
Advertisement
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;

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!