Reputation: 443
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.
Upvotes: 0
Views: 4405
Reputation: 4888
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 !
Upvotes: 3