0

My goal is to populate the client area of my window with a bunch of different child controls. Things like buttons check boxes and static text but I'm having a difficult time figuring out how to catch the button clicked message.

My Code:

#include <windows.h> #include <CommCtrl.h> #include "resource.h" HMENU BUTTON1; LRESULT CALLBACK WndProc (HWND hwnd, UINT message, UINT wParam, LONG lParam) ; int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpszCmdParam, int nCmdShow) { static char szAppName[] = "Keyboarding" ; HWND hwnd; MSG msg; WNDCLASS wndclass; HMENU hMenu; hMenu = LoadMenu(hInstance, MAKEINTRESOURCE(IDR_MENU1)); if (!hPrevInstance) { wndclass.style = CS_HREDRAW | CS_VREDRAW; wndclass.lpfnWndProc = WndProc; wndclass.cbClsExtra = 0; wndclass.cbWndExtra = 0; wndclass.hInstance = hInstance ; wndclass.hIcon = LoadIcon (NULL, IDI_APPLICATION) ; wndclass.hCursor = LoadCursor (NULL, IDC_ARROW) ; wndclass.hbrBackground = static_cast<HBRUSH>(GetStockObject (WHITE_BRUSH)); wndclass.lpszMenuName = NULL ; wndclass.lpszClassName = szAppName ; RegisterClass (&wndclass) ; } hwnd = CreateWindow (szAppName, // window class name "Lab 6", // window caption WS_OVERLAPPEDWINDOW, // window style CW_USEDEFAULT, // initial x position CW_USEDEFAULT, // initial y position CW_USEDEFAULT, CW_USEDEFAULT, // hmmmmm??? NULL, // parent window handle hMenu, // window menu handle hInstance, // program instance handle NULL) ; // creation parameters HWND button1 = CreateWindow( WC_BUTTON, "Push Button", WS_TABSTOP|WS_VISIBLE|WS_CHILD|BS_PUSHBUTTON, 50, 50, 100, 24, hwnd, BUTTON1, hInstance, NULL); HWND button2 = CreateWindow( WC_BUTTON, "Auto Check Button?", WS_TABSTOP|WS_VISIBLE|WS_CHILD|BS_PUSHBUTTON, 50, 50, 100, 24, hwnd, NULL, hInstance, NULL); HWND button3 = CreateWindow( WC_BUTTON, "Push Button", WS_TABSTOP|WS_VISIBLE|WS_CHILD|BS_PUSHBUTTON, 50, 50, 100, 24, hwnd, NULL, hInstance, NULL); HWND editControl = CreateWindow( WC_BUTTON, "Push Button", WS_TABSTOP|WS_VISIBLE|WS_CHILD|BS_PUSHBUTTON, 50, 50, 100, 24, hwnd, NULL, hInstance, NULL); HWND StaticControl = CreateWindow( WC_STATIC, "Hello World", WS_TABSTOP|WS_VISIBLE|WS_CHILD|BS_PUSHBUTTON, 50, 100, 100, 24, hwnd, NULL, hInstance, NULL); HWND ListBox = CreateWindow( WC_BUTTON, "Push Button", WS_TABSTOP|WS_VISIBLE|WS_CHILD|BS_PUSHBUTTON, 50, 50, 100, 24, hwnd, NULL, hInstance, NULL); ShowWindow (hwnd, nCmdShow) ; UpdateWindow (hwnd) ; while (GetMessage (&msg, NULL, 0, 0)) { TranslateMessage (&msg) ; DispatchMessage (&msg) ; } return (int) msg.wParam ; } LRESULT CALLBACK WndProc (HWND hwnd, UINT message, UINT wParam, LONG lParam) { HDC hdc ; PAINTSTRUCT ps ; TEXTMETRIC tm; RECT rClientRect; RECT rWindowRect; SIZE size; int cButtons = 0; switch (message) { case WM_COMMAND: switch(LOWORD(wParam)) { case BUTTON1: break; } case WM_PAINT: hdc = BeginPaint (hwnd, &ps) ; EndPaint (hwnd, &ps) ; return 0 ; case WM_DESTROY: PostQuitMessage (0) ; return 0 ; } return DefWindowProc (hwnd, message, wParam, lParam) ; } 

I'm trying to catch my first button labaled button1 and has the "ID" BUTTON1. I have tried just catching them in my WM_COMMAND and it says my BUTTON1 is unidentified.

6
  • I tried catching my BUTTON1 in my WM_COMMAND and switching the LOWORD of the wParam. It just says BUTTON1 isn't identified. Commented May 13, 2013 at 15:52
  • @JesseMoreland, It's in WinMain. Your procedure can't see it. Commented May 13, 2013 at 15:53
  • I have to make a procedure for child controls? Commented May 13, 2013 at 15:53
  • No, you should either create all the buttons in your window procedure, catching WM_CREATE, or declare them globally/statically, although I'd recommend the second. Also, you don't appear to be specifying an ID for any of your buttons, so you won't be able to catch this Commented May 13, 2013 at 15:56
  • I did create an ID for my first button becuase i'm trying to learn. Commented May 13, 2013 at 15:56

1 Answer 1

3

What you are looking for is:

case WM_COMMAND: switch(LOWORD(wParam)) { case BUTTON1: { 

But before it will recognize BUTTON1 you will have to declare it in such a way that it can be recognized by the message handler: for example make it a global variable, instead of declaring it in the WinMain:

HMENU BUTTON1; int WINAPI WinMaietcetc.. 

Let me know if this works !

Sign up to request clarification or add additional context in comments.

7 Comments

It worked kind of. It says BUTTON1 needs to be a modifiable expression. Updating code now.
You need to give BUTTON1 a value, so HMENU BUTTON1 = 5. You'd be better of using a define (#define BUTTON1 5) or a const int instead of just a HMENU
Because it's a switch, I think it has to be const or defined in order for it to be allowed by the compiler
That didn't fix anything either. Very frustrating :(
I got it guys. The #define of the button worked but I needed to make it (HMENU) BUTTON1 inside of my create window for the button. Thank you everyone! =).
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.