Question: How to display a message box on the click event of a button in Windows Programming (VC++) ? Hi folks!... I have created a window and a button on it using VC++ 6.0. Now, I want to display a messagebox in response of the click event of button. Please guide me what code to write in the Windows Procedure?. Here is the Windows Procedure:\n\n/* Assume that the main window name is Window1 and button name is Button1 */\n\nLRESULT CALLBACK\nWindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)\n{\n    switch (message)                  /* handle the messages */\n    {\n        case WM_DESTROY:\n\n            PostQuitMessage (0);       /* send a WM_QUIT to the message queue */\n            break;\n  \n\n      default:                      /* for messages that we don't deal with */\n\n            return DefWindowProc (hwnd, message, wParam, lParam);\n    }\n\n    return 0;\n}

Answer: Hello,\n\nYou have to check for the event WM_COMMAND in your switch instruction. And inside that case, check the wParam value (High order byte) of the event to make sure it is a BN_CLICKED event. At that stage, you need to further identify your button using the wParam (Low order byte) of the wParam.\n\nExample:\n\n        case WM_COMMAND: \n          if (HIWORD(wParam) == BN_CLICKED) \n          { \n                switch (LOWORD(wParam)) \n                { \n                    case ID_OF_YOUR_BUTON: \n                        MessageBox(hwnd, "Hello", "Testing", MB_OK);\n \n                        break; \n                } \n          } \n          break; \n\n\nI suggest that you take a look to the doc of VC++ on these events, mainly in the Windows SDK doc about Buttons. It is well done with code samples.\n\nAdditional note: as you created your button directly on a window instead of within a dialog box, it is very likely that you used a call to the CreateWindow function to create it. In this case, you give the ID number in parameter HMENU hMenu of the function. You may create a Resource ID (it is just an value globally defined with #define) to avoid playing with numbers when you have several controls.\n\nRegards,\nChris.\nhttp://www.beinsoft.com
This question from the Yahoo Q&A forum is categorized in the topic: Education & Reference