Trapping Button Presses
Hello All,
I am new to Windows Programming in C++, and need some help on trapping messages. I have a window set up with 3 controls: STATIC, EDIT, and BUTTON, all created via CreateWindow. I also have a WndProc function, that I use for trapping resizing and closing. It is a login window in which the style of the EDIT control is simply ES_PASSWORD | WS_VISIBLE | WS_CHILD, and the static is WS_BORDER | WS_VISIBLE | WS_CHILD. The button is just WS_CHILD | WS_VISIBLE. Anyway, I need to be able to trap the message that windows sends to a button when it is clicked, so then I can make some action occur to log the user in. My first thought was to put another case into my WndProc function telling it to trap the WM_LBUTTONDOWN message, then check if the hwnd parameter being passed was the button's. Unfortunately, I found that the only thing that the WM_LBUTTONDOWN message was being sent to was my window and static control. Please, what message should I trap and where should I trap it to find when a command button has been clicked?
EDIT: My code so far is:
#include <windows.h>
/* Declare Windows procedure */
LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);
/* Make the class name into a global variable */
char szClassName[ ] = "WindowsApp";
int WINAPI WinMain (HINSTANCE hThisInstance,
HINSTANCE hPrevInstance,
LPSTR lpszArgument,
int nFunsterStil)
{
HWND hwnd; /* This is the handle for our window */
MSG messages; /* Here messages to the application are saved */
WNDCLASSEX wincl; /* Data structure for the windowclass */
HWND textbox;
HINSTANCE hwndInstance;
LONG styleOfText;
/* The Window structure */
wincl.hInstance = hThisInstance;
wincl.lpszClassName = szClassName;
wincl.lpfnWndProc = WindowProcedure; /* This function is called by windows */
wincl.style = CS_DBLCLKS; /* Catch double-clicks */
wincl.cbSize = sizeof (WNDCLASSEX);
/* Use default icon and mouse-pointer */
wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
wincl.lpszMenuName = NULL; /* No menu */
wincl.cbClsExtra = 0; /* No extra bytes after the window class */
wincl.cbWndExtra = 0; /* structure or the window instance */
/* Use Windows's default color as the background of the window */
wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND;
/* Register the window class, and if it fails quit the program */
if (!RegisterClassEx (&wincl))
return 0;
/* The class is registered, let's create the program*/
hwnd = CreateWindowEx (
0, /* Extended possibilites for variation */
szClassName, /* Classname */
"Password Varification", /* Title Text */
WS_OVERLAPPEDWINDOW, /* default window */
CW_USEDEFAULT, /* Windows decides the position */
CW_USEDEFAULT, /* where the window ends up on the screen */
150, /* The programs width */
300, /* and height in pixels */
HWND_DESKTOP, /* The window is a child-window to desktop */
NULL, /* No menu */
hThisInstance, /* Program Instance handler */
NULL /* No Window Creation data */
);
long wndInstance = GetWindowLong(hwnd, GWL_HINSTANCE);
hwndInstance = HINSTANCE(wndInstance);
styleOfText = WS_VISIBLE | WS_CHILDWINDOW | WS_BORDER | ES_PASSWORD;
HWND hstaticpass = CreateWindow("STATIC", "Password:",
styleOfText,
20, 30, 100, 20, hwnd, 0, hwndInstance, NULL);
HWND hEdit = CreateWindow("EDIT", "Type here.",
styleOfText,
20, 50, 100, 30, hwnd, 0, hwndInstance, NULL);
HWND hButton = CreateWindow("Button", "Log In",
WS_VISIBLE | WS_CHILD,
20, 200, 100, 30, hwnd, 0, hwndInstance, NULL);
/* Make the window visible on the screen */
ShowWindow (hwnd, SW_NORMAL);
/* Run the message loop. It will run until GetMessage() returns 0 */
while (GetMessage (&messages, NULL, 0, 0))
{
/* Translate virtual-key messages into character messages */
TranslateMessage(&messages);
/* Send message to WindowProcedure */
DispatchMessage(&messages);
}
/* The program return-value is 0 - The value that PostQuitMessage() gave */
return messages.wParam;
}
/* This function is called by the Windows function DispatchMessage() */
LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message) /* handle the messages */
{
case WM_DESTROY:
PostQuitMessage (0); /* send a WM_QUIT to the message queue */
break;
case WM_SIZE:
MessageBox(hwnd, "Please Don't Resize.", "Error", MB_OK | MB_ICONERROR);
break;
default:
/* for messages that we don't deal with */
return DefWindowProc (hwnd, message, wParam, lParam);
}
return 0;
}
Thanks in advance,
Alex
|