Windows程序设计-按钮的创建

#include <Windows.h>

typedef WNDPROC WMPROC;
typedef struct{
	UINT msg;
	WMPROC wmProc;
}MsgWithProc;

#define WINDOW_WIDTH 800
#define WINDOW_HEIGHT 600
#define WINDOW_TITLE L"按钮的创建"


HDC g_hdc = NULL;
HINSTANCE g_hInstance = NULL;

LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam);

LRESULT __stdcall WMcreate(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam);
LRESULT __stdcall WMpaint(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam);
LRESULT __stdcall WMcommand(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam);
LRESULT __stdcall WMkeydown(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam);
LRESULT __stdcall WMdestory(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam);

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd){
	g_hInstance = hInstance;
	WNDCLASSEX wndClass = {0};
	wndClass.cbSize = sizeof(WNDCLASSEX);
	wndClass.style = CS_HREDRAW | CS_VREDRAW;
	wndClass.lpfnWndProc = WndProc;
	wndClass.cbClsExtra = 0;
	wndClass.cbWndExtra = 0;
	wndClass.hInstance = hInstance;
	wndClass.hIcon = NULL;
	wndClass.hCursor = LoadCursor(NULL, IDC_ARROW);
	wndClass.hbrBackground = (HBRUSH)GetStockObject(GRAY_BRUSH);
	wndClass.lpszMenuName = NULL;
	wndClass.lpszClassName = L"Page36";

	if(!RegisterClassEx(&wndClass)){
		return -1;
	}

	HWND hwnd = CreateWindowEx(
		NULL,
		L"Page36",
		WINDOW_TITLE,
		WS_POPUP | WS_SYSMENU | WS_SIZEBOX,
		CW_USEDEFAULT, CW_USEDEFAULT, WINDOW_WIDTH, WINDOW_HEIGHT,
		NULL, NULL, hInstance, NULL
	);

	ShowWindow(hwnd, nShowCmd);
	UpdateWindow(hwnd);

	MSG msg = {0};
	while(msg.message != WM_QUIT){
		if(PeekMessage(&msg, 0, 0, 0, PM_REMOVE)){
			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}
	}
	UnregisterClass(L"Page36", hInstance);
	return 0;
}

LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam){

	MsgWithProc msgWithProc[] = {
		{WM_CREATE,WMcreate},
		//{WM_PAINT,WMpaint},
		{WM_COMMAND,WMcommand},
		{WM_KEYDOWN,WMkeydown},
		{WM_DESTROY,WMdestory}
	};

	for(MsgWithProc mwp : msgWithProc){
		if(mwp.msg == message){
			mwp.wmProc(hwnd, message, wParam, lParam);
			return 0;
		}
	}
	return DefWindowProc(hwnd, message, wParam, lParam);
}

LRESULT __stdcall WMcreate(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam){
	CreateWindow(
		TEXT("button"),
		TEXT("Hello"),
		WS_CHILD | WS_VISIBLE,
		20, 20, 50, 30,
		hwnd,
		(HMENU)10001,
		g_hInstance, NULL
	);
	return 0;
}

LRESULT __stdcall WMpaint(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam){
	
	PAINTSTRUCT paintStruct;
	g_hdc = BeginPaint(hwnd, &paintStruct);

	EndPaint(hwnd, &paintStruct);
	ValidateRect(hwnd, NULL);
	

	return 0;
}

LRESULT __stdcall WMcommand(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam){
	WORD wHigh = HIWORD(wParam);
	WORD wLow = LOWORD(wParam);
	switch(wLow){
		case 10001:
			MessageBox(0, TEXT("按钮被点击"), 0, 0);
			break;
	}
	return 0;
}

LRESULT __stdcall WMkeydown(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam){
	switch(wParam){
		case VK_F11:
		{
			HWND    hDesk;
			RECT    rc;
			hDesk = GetDesktopWindow();
			GetWindowRect(hDesk, &rc);
			SetWindowLong(hwnd, GWL_STYLE, WS_BORDER);
			SetWindowPos(hwnd, HWND_TOPMOST, 0, 0, rc.right, rc.bottom, SWP_SHOWWINDOW);
			break;
		}
		case VK_ESCAPE:
			PostQuitMessage(0);
			break;
	}
	return 0;
}

LRESULT __stdcall WMdestory(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam){
	PostQuitMessage(0);
	return 0;
}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值