How to Create a Button Control in Win32 Program
Under case WM_CREATE: type
hwndButton = CreateWindow(
L"BUTTON", // Predefined class; Unicode assumed.
L"Code", // Button text.
WS_TABSTOP | WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON |
BS_CENTER, // Styles.
2, // x position.
5, // y position.
45, // Button width.
30, // Button height.
hwnd, // Parent window.
NULL, // No menu.
hInst, // program instance
NULL); // Pointer not needed.
NULL); // Pointer not needed.
Before that you should declare HWND hwndButton; in the start of the callback function or in global scope if you need the instance in other functions too.
How to do something when the button is clicked
You can handle the click even easily. When the button is clicked the parent window gets a notification. It’s a constant value called BN_CLICKED. It is sent to the callback function’s wParam parameter.
What you need to do extra is to check if the click is on the button control. As handle is also passed through the parameter lParam you can compare it with hwndButton and if matches then perform actions. Here’s a code snippet.
What you need to do extra is to check if the click is on the button control. As handle is also passed through the parameter lParam you can compare it with hwndButton and if matches then perform actions. Here’s a code snippet.
case WM_COMMAND:
switch (LOWORD(wParam)) {
case BN_CLICKED:
// check if click is in Button control
if (lParam == (LPARAM) hwndButton) {
wsprintf(str, L"Button is clicked!");
//SendMessage(hWnd, WM_PAINT, NULL, NULL);
InvalidateRect(hWnd, NULL, TRUE);
}
break;
}
break;
str is a globar variable. Without keeping it global change will not be in effect.
Enabling Visual Style in Controls and Dialog Boxes
By default Visual Style for controls are not enabled. We have to enable it using manifest file.
Create a text file with these contents.
Create a text file with these contents.
<xml version="1.0" encoding="UTF-8" standalone="yes"?>
<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
<assemblyIdentity
version="1.0.0.0"
processorArchitecture="X86"
name="ButtonWithVisualStyle"
type="win32"
/>
<description>Demonstrates a button control.description>
<dependency>
<dependentAssembly>
<assemblyIdentity
type="win32"
name="Microsoft.Windows.Common-Controls"
version="6.0.0.0"
processorArchitecture="X86"
publicKeyToken="6595b64144ccf1df"
language="*"
/>
</dependentAssembly>
</dependency>
</assembly>
If your program is button.cpp then save this text file with the name button.exe.manifest in the same directory (for advantage).
Now go to solution explorer. Under your current solution right click “Resource Files”. Click add and then “Existing Item”.
Now go to solution explorer. Under your current solution right click “Resource Files”. Click add and then “Existing Item”.
Select your manifest file and click add.
Build the solution again and then run. You’ll see effects like this.
Files are attached with this post. Click to download.
No comments:
Post a Comment