Buttons are just child windows, but I am sure you already know that. So when you create your child window you assign it an id unique to that window(button) right? That is important so you can distinguish them later on.
Now when something happens to the button it sends the parent window a message. This message is a WM_COMMAND message, so all button messages will be trapped in here:
Code:
case WM_COMMAND:
//All button messages
So with that out of the way, we can now look at the actual parameters sent to the parent window with the WM_COMMAND message.
WPARAM - this contains the child window ID that you assigned to it when you created the window. It is contained in the Lower word of this parameter. So to get the child window ID one would:
Code:
childWindowID = LOWORD(wParam);
Now that we know what button the message is coming from we need to know what the actual message is, right? This is also contained in the WPARAM parameter, in the high word. So to view the actual message one would:
Code:
notificationCode = HIWORD(wParam);
Now these notification codes are defined in the windows header files and are:
Code:
BN_CLICKED 0
BN_PAINT 1
BN_HILITE or BN_PUSHED 2
BN_UNHILITE or BN_UNPUSHED 3
BN_DISABLE 4
BN_DOUBLECLICKED or BN_DBLCLK 5
BN_SETFOCUS 6
BN_KILLFOCUS 7
And finally the LPARAM parameter returns the handle to the child window sending the message. So if we wanted to capture when our button with ID = 1 was clicked we would:
Code:
case WM_COMMAND:
if(LOWORD(wParam) == 1)
if(HIWORD(wParam) == BN_CLICKED)
//Do something because I was clicked
break;
Here is a sample app that demonstrates the above mentioned steps to capture button messages:
Code:
#include <windows.h>
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow){
TCHAR title[] = TEXT("MY Button App");
MSG msg;
HWND hwnd;
WNDCLASS wc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wc.hInstance = hInstance;
wc.lpfnWndProc = WndProc;
wc.lpszClassName = title;
wc.lpszMenuName = NULL;
wc.style = CS_HREDRAW | CS_VREDRAW;
RegisterClass(&wc);
hwnd = CreateWindow(title, TEXT("My Button App"), WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
NULL, NULL, hInstance, NULL);
ShowWindow(hwnd, iCmdShow);
UpdateWindow(hwnd);
while(GetMessage(&msg, NULL, 0, 0)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return (int)msg.wParam;
}
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam){
static HWND hwndButton;
static int cxChar, cyChar;
switch(message){
case WM_CREATE:
cxChar = LOWORD(GetDialogBaseUnits());
cyChar = HIWORD(GetDialogBaseUnits());
hwndButton = CreateWindow(TEXT("button"), //class name
TEXT("MyPushButton"), //text displayed by button
WS_CHILD | WS_VISIBLE | BS_DEFPUSHBUTTON, //button style
0, //xPos
0, //yPos
20*cxChar, //width
7*cyChar/4 , //height
hwnd, //parent window
(HMENU)1, //child ID
((LPCREATESTRUCT)lParam)->hInstance, //instance handle
NULL); //Extra parameters
return 0;
case WM_SIZE:
MoveWindow(hwndButton, (LOWORD(lParam) / 2) - 10*cxChar, HIWORD(lParam) / 2, 20*cxChar, 7*cyChar/4, TRUE);
break;
case WM_COMMAND:
if(LOWORD(wParam)==1)
if(HIWORD(wParam)==BN_CLICKED)
MessageBox(NULL, TEXT("Don't click so hard, that hurts"), TEXT("Button Message"), MB_ICONINFORMATION);
break;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd, message, wParam, lParam);
}