Implementing and Using Buttons
First of all, I'd like to say that I'm new... what's up?
Anyhow, I'm trying to figure out how to use a button that I have created with the CreateWindow function.
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow)
{
WNDCLASSEX wc;
WNDCLASSEX wb;
HWND hwnd;
HWND hwndButton;
MSG Msg;
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = 0;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wc.lpszMenuName = NULL;
wc.lpszClassName = g_szClassName;
wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
//wb.lpfnWndProc = ButtonProc;
if(!RegisterClassEx(&wc))
{
MessageBox(NULL, "Window Registration Failed!", "Error!",
MB_ICONEXCLAMATION | MB_OK);
return 0;
}
hwnd = CreateWindowEx(
WS_EX_CLIENTEDGE,
g_szClassName,
"A Bitmap Program",
WS_OVERLAPPEDWINDOW,
GetSystemMetrics(SM_CXFULLSCREEN) / 2 - myWindowWidth / 2, GetSystemMetrics(SM_CYFULLSCREEN) / 2 - myWindowHeighth / 2, myWindowWidth, myWindowHeighth,
NULL, NULL, hInstance, NULL);
// HERE IS MY BUTTON!
hwndButton = CreateWindow(
"BUTTON", // predefined class
"Click Me", // button text
WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON, // styles
// Size and position values are given explicitly, because
// the CW_USEDEFAULT constant gives zero values for buttons.
myWindowWidth / 2 - 50, // starting x position
myWindowHeighth / 2 - 50, // starting y position
100, // button width
100, // button height
hwnd, // parent window
NULL, // No menu
(HINSTANCE) GetWindowLong(hwnd, GWL_HINSTANCE),
NULL); // pointer not needed
if(hwnd == NULL)
{
MessageBox(NULL, "Window Creation Failed!", "Error!",
MB_ICONEXCLAMATION | MB_OK);
return 0;
}
ShowWindow(hwnd, nCmdShow);
UpdateWindow(hwnd);
while(GetMessage(&Msg, NULL, 0, 0) > 0)
{
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
return Msg.wParam;
}
So... how do I get the button to do anything? I need an example of something happening when the button is pressed, like for example, another window pops up.
Thanks.
EDIT: I was wrong =) It really has been a long time. See the post above for more info, he's got good links. =)
Quote:Original post by Colin Jeanne
This is everything you can do with buttons
This shows you how to use it and has example code
Thanks, but I've already gone there... I'm still slightly confused.
Quote:Original post by superqtip182Quote:Original post by Colin Jeanne
This is everything you can do with buttons
This shows you how to use it and has example code
Thanks, but I've already gone there... I'm still slightly confused.
OK, the second link is what you should pay attention too. Every button needs a name (a numeric identifier).
Let's take the Clear Boxes button.
It's name would be IDC_CLEARBOXES.
#define IDC_CLEARBOXES 107 // top push button
Now, to tell whether the button has been pressed or not, you need to check during the WndProc message handling. In this case, clicking a button sends a WM_COMMAND message. Now, you're asking, that's great, but how to distinguish between buttons and other commands? Well, the identifier you gave your button, along with other information is passed into the WPARAM and LPARAM parameters. The HIWORD of the WPARAM will notify you when a BN_CLICKED message has been received. And the LOWORD of the WPARAM will notify you of exactly what identifer was called.
Right now, your button has no identifier. I believe (could be wrong, it's late right now), you want to assign the identifer in the HMENU location.
And for future reference, placing your code in source tags ([source][/source]) will make it easier to read.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement