Win32程序创建按钮并响应单击
程序员文章站
2024-03-01 12:04:10
...
码;
#include <windows.h>
#include "resource.h"
LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);
HINSTANCE hInst;
TCHAR szClassName[] = TEXT("win32ButtonDemo");
int WINAPI
WinMain (HINSTANCE hThisInstance,
HINSTANCE hPrevInstance,
LPSTR lpszArgument,
int nFunsterStil)
{
HWND hwnd;
MSG messages;
WNDCLASSEX wincl;
hInst = hThisInstance;
wincl.hInstance = hThisInstance;
wincl.lpszClassName = szClassName;
wincl.lpfnWndProc = WindowProcedure;
wincl.style = CS_DBLCLKS;
wincl.cbSize = sizeof (WNDCLASSEX);
wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
wincl.lpszMenuName = MAKEINTRESOURCE (IDC_BUTTONDEMO);
wincl.cbClsExtra = 0;
wincl.cbWndExtra = 0;
wincl.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
if (!RegisterClassEx (&wincl))
return 0;
hwnd = CreateWindowEx (
0,
szClassName,
TEXT("win32ButtonDemo"),
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
300,
200,
HWND_DESKTOP,
NULL,
hThisInstance,
NULL
);
ShowWindow (hwnd, nFunsterStil);
while (GetMessage (&messages, NULL, 0, 0))
{
TranslateMessage(&messages);
DispatchMessage(&messages);
}
return messages.wParam;
}
LRESULT CALLBACK
WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
PAINTSTRUCT ps;
HDC hdc;
RECT rt;
int ret;
switch (message)
{
case WM_COMMAND:
switch (LOWORD(wParam))
{
case IDB_ONE:
MessageBox(hwnd, "您点击了第一个按钮。", "win32ButtonDemo", MB_OK | MB_ICONINFORMATION);
break;
case IDB_TWO:
MessageBox(hwnd, "您点击了第二个按钮。", "win32ButtonDemo", MB_OK | MB_ICONINFORMATION);
break;
case IDB_THREE:
MessageBox(hwnd, "您点击了第三个按钮。", "win32ButtonDemo", MB_OK | MB_ICONINFORMATION);
break;
case IDM_ABOUT:
MessageBox (hwnd, TEXT ("win32ButtonDemo v1.0\nCopyright (C) 2020\n by bo"),
TEXT ("win32ButtonDemo"), MB_OK | MB_ICONINFORMATION);
break;
case IDM_EXIT:
DestroyWindow(hwnd);
break;
default:
return DefWindowProc(hwnd, message, wParam, lParam);
}
break;
case WM_CREATE:
CreateWindow("Button", "按钮一", WS_VISIBLE | WS_CHILD | BS_PUSHBUTTON,
35, 10, 120, 30, hwnd, (HMENU)IDB_ONE, hwnd, NULL);
CreateWindow("Button", "按钮二", WS_VISIBLE | WS_CHILD | BS_PUSHBUTTON,
35, 50, 120, 30, hwnd, (HMENU)IDB_TWO, hwnd, NULL);
CreateWindow("Button", "按钮三", WS_VISIBLE | WS_CHILD | BS_PUSHBUTTON,
35, 90, 120, 30, hwnd, (HMENU)IDB_THREE, hwnd, NULL);
break;
case WM_PAINT:
hdc = BeginPaint(hwnd, &ps);
GetClientRect(hwnd, &rt);
EndPaint(hwnd, &ps);
break;
case WM_DESTROY:
PostQuitMessage (0);
break;
default:
return DefWindowProc (hwnd, message, wParam, lParam);
}
return 0;
}
图;
win7, CFree 5.0;新建一个窗口工程;
资源文件,头文件;
#include "resource.h"
#include <windows.h>
/////////////////////////////////////////////////////////////////////////////
//
// Menu
//
IDC_BUTTONDEMO MENU
BEGIN
POPUP "&File"
BEGIN
MENUITEM "E&xit", IDM_EXIT
END
POPUP "&Help"
BEGIN
MENUITEM "&About ...", IDM_ABOUT
END
END
#define IDM_EXIT 10001
#define IDM_ABOUT 10002
#define IDC_BUTTONDEMO 10101
#define IDD_ABOUTBOX 10102
#define IDB_ONE 53301
#define IDB_TWO 53302
#define IDB_THREE 53303