Windows图形界面(GUI)-SDK-C/C++ - 按钮(button)
公开视频 -> 链接点击跳转公开课程博客首页 -> 链接点击跳转博客主页
按钮(button)
按钮样式
推按钮(Push Button)
推按钮是最常见的按钮控件,用于触发某个动作。常见的推按钮类型有普通按钮、默认按钮、关闭按钮等。复选框(Check Box)
复选框允许用户在多个选项中进行选择,每个选项可以独立地被选中或取消选中。单选按钮(Radio Button)
单选按钮用于在一组互斥选项中进行选择,用户只能选择其中一个选项。
创建控件
使用CreateWindow或CreateWindowEx函数创建推按钮控件。
初始控件
设置控件属性,初始状态。
消息处理
在窗口过程函数中处理WM_COMMAND消息。
示例代码
#include <windows.h>
#include "resource.h"
#define ID_BUTTON_PUSH1 1
#define ID_BUTTON_PUSH2 11
#define ID_BUTTON_PUSH3 111
#define ID_BUTTON_PUSH4 222
#define ID_BUTTON_PUSH5 333
#define ID_BUTTON_CHECKBOX1 2
#define ID_BUTTON_RADIOBUTTON1 3
#define ID_BUTTON_RADIOBUTTON2 4
HINSTANCE ghInstance = 0;
LRESULT CALLBACK MainWindowProc (HWND, UINT, WPARAM, LPARAM);
INT WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
ghInstance = hInstance;
// 注册窗口
WNDCLASSEX wndclass = { 0 };
wndclass.cbSize = sizeof(WNDCLASSEX);
wndclass.style = NULL;
wndclass.lpfnWndProc = MainWindowProc;
wndclass.cbClsExtra = NULL;
wndclass.cbWndExtra = NULL;
wndclass.hInstance = hInstance;
wndclass.hIcon = NULL;
wndclass.hCursor = NULL;
wndclass.hbrBackground = (HBRUSH)COLOR_WINDOW;
wndclass.lpszMenuName = NULL;
wndclass.lpszClassName = TEXT("0xCC");
wndclass.hIconSm = NULL;
if (!RegisterClassEx(&wndclass))
{
MessageBox(NULL, TEXT("RegisterClassEx Failed"), TEXT("Error"), MB_OK);
return 1;
}
// 创建窗口
HWND hwnd = CreateWindowEx(
WS_EX_CLIENTEDGE,
TEXT("0xCC"),
TEXT("CreateWindowEx"),
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
960,
540,
NULL,
NULL,
hInstance,
NULL
);
if (hwnd == NULL)
{
MessageBox(NULL, TEXT("CreateWindowEx Failed"), TEXT("Error"), MB_OK);
return 1;
}
// 显示窗口
ShowWindow(hwnd, SW_SHOWDEFAULT);
// 更新窗口
UpdateWindow(hwnd);
// 消息处理
MSG msg = { 0 };
while (GetMessage(&msg, 0, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return 0;
}
// 窗口过程
LRESULT CALLBACK MainWindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
static HWND hButton1, hButton2, hButton3, hButton4, hButton5, hCheck, hRADIOBUTTON1, hRADIOBUTTON2;
switch (uMsg)
{
case WM_CREATE:
{
//创建控件
hButton1 = CreateWindow(
TEXT("BUTTON"),
TEXT("BS_PUSHBUTTON1"),
WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON ,
10, 10,
150, 40,
hwnd,
(HMENU)ID_BUTTON_PUSH1,
ghInstance,
NULL);
hButton2 = CreateWindow(
TEXT("BUTTON"),
TEXT("BS_PUSHBUTTON2"),
WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
170, 10,
150, 40,
hwnd,
(HMENU)ID_BUTTON_PUSH2,
ghInstance,
NULL);
hButton3 = CreateWindow(
TEXT("BUTTON"),
TEXT("BS_PUSHBUTTON3"),
WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON | BS_BITMAP,
330, 10,
150, 40,
hwnd,
(HMENU)ID_BUTTON_PUSH3,
ghInstance,
NULL);
hButton4 = CreateWindow(
TEXT("BUTTON"),
TEXT("BS_PUSHBUTTON4"),
WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
170, 60,
150, 40,
hwnd,
(HMENU)ID_BUTTON_PUSH4,
ghInstance,
NULL);
hButton5 = CreateWindow(
TEXT("BUTTON"),
TEXT("BS_PUSHBUTTON5"),
WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
330, 60,
150, 40,
hwnd,
(HMENU)ID_BUTTON_PUSH5,
ghInstance,
NULL);
hCheck = CreateWindow(
TEXT("BUTTON"),
TEXT("CHECKBOX"),
WS_CHILD | WS_VISIBLE | BS_CHECKBOX,
10, 60,
150, 40,
hwnd,
(HMENU)ID_BUTTON_CHECKBOX1,
ghInstance,
NULL);
hRADIOBUTTON1 = CreateWindow(
TEXT("BUTTON"),
TEXT("RADIOBUTTON1"),
WS_CHILD | WS_VISIBLE | BS_RADIOBUTTON,
10, 110,
150, 40,
hwnd,
(HMENU)ID_BUTTON_RADIOBUTTON1,
ghInstance,
NULL);
hRADIOBUTTON2 = CreateWindow(
TEXT("BUTTON"),
TEXT("RADIOBUTTON2"),
WS_CHILD | WS_VISIBLE | BS_RADIOBUTTON,
10, 160,
150, 40,
hwnd,
(HMENU)ID_BUTTON_RADIOBUTTON2,
ghInstance,
NULL);
//初始控件
SendMessage(hCheck, BM_SETCHECK, BST_CHECKED, 0);
SendMessage(hRADIOBUTTON1, BM_SETCHECK, BST_CHECKED, 0);
HBITMAP hBitmap = LoadBitmap((HINSTANCE)GetWindowLongPtr(hwnd, GWLP_HINSTANCE), MAKEINTRESOURCE(IDB_BITMAP1));
if (hBitmap != NULL)
{
SendMessage(hButton3, BM_SETIMAGE, (WPARAM)IMAGE_BITMAP, (LPARAM)hBitmap);
}
break;
}
case WM_DESTROY:
{
PostQuitMessage(0);
break;
}
case WM_COMMAND:
{
switch (LOWORD(wParam))
{
case ID_BUTTON_PUSH1:
{
MessageBox(NULL, TEXT("ID_BUTTON_PUSH1"), NULL, NULL);
break;
}
case ID_BUTTON_PUSH2:
{
SendMessage(hButton1, BM_CLICK, 0, 0);
break;
}
case ID_BUTTON_CHECKBOX1:
{
BOOL ret = SendMessage(hCheck, BM_GETCHECK, 0, 0) == BST_CHECKED;
HWND hChe = GetDlgItem(hwnd, ID_BUTTON_CHECKBOX1);
SendMessage(hChe, BM_SETCHECK, ret ? BST_UNCHECKED : BST_CHECKED, 0);
break;
}
case ID_BUTTON_RADIOBUTTON1:
case ID_BUTTON_RADIOBUTTON2:
{
SendMessage(hRADIOBUTTON1, BM_SETCHECK, BST_UNCHECKED, 0);
SendMessage(hRADIOBUTTON2, BM_SETCHECK, BST_UNCHECKED, 0);
SendMessage((HWND)lParam, BM_SETCHECK, BST_CHECKED, 0);
//SendMessage(GetDlgItem(hwnd, LOWORD(wParam)), BM_SETCHECK, BST_CHECKED, 0);
break;
}
case ID_BUTTON_PUSH5:
{
SendMessage(hButton4, BM_SETSTYLE, BS_CHECKBOX, TRUE);
break;
}
}
}
default:
return DefWindowProc(hwnd, uMsg, wParam, lParam);
}
}
Ongwu博客 版权声明:以上内容未经允许不得转载!授权事宜或对内容有异议或投诉,请联系站长,将尽快回复您,谢谢合作!