CG学习笔记 / 创建窗口、消息循环、窗口消息

#include <Windows.h>

LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
	switch (msg)
	{
	case WM_CLOSE:
		PostQuitMessage(69);  //exitCode -> wParam
		break;
	case WM_KEYDOWN:
		if (wParam == 'F')
		{
			SetWindowText(hwnd, "F Keydown");
		}
		break;
	case WM_KEYUP:
		if (wParam == 'F')
		{
			SetWindowText(hwnd, "F Keyup");
		}
		break;
	}

	return DefWindowProc(hwnd, msg, wParam, lParam);
}
int CALLBACK WinMain(
	_In_ HINSTANCE hInstance, //handle to instance
	_In_opt_ HINSTANCE hPrevInstance, //handle to previous instance
	_In_ LPSTR IpCmdLine, //long pointer to string
	_In_ int nCmdShow
)
{
	const auto pClassName = "hw3dbutts";

	// register window class
	WNDCLASSEX wc = { 0 };
	wc.cbSize = sizeof wc; //size of the struct
	wc.style = CS_OWNDC;
	wc.lpfnWndProc = WndProc; //process func
	wc.cbClsExtra = 0;
	wc.cbWndExtra = 0; // 0 in usual
	wc.hInstance = hInstance;
	wc.hIcon = nullptr; //handle to icon
	wc.hCursor = nullptr;
	wc.hbrBackground = nullptr; // handle to backgroung brush
	wc.lpszMenuName = nullptr;
	wc.lpszClassName = pClassName;
	wc.hIconSm = nullptr; //handle to small icon
	RegisterClassEx(&wc);

	//create window instance
	HWND hwnd = CreateWindowEx(
		0, pClassName,
		"Happy Hard Window",
		WS_CAPTION | WS_MINIMIZEBOX | WS_SYSMENU,
		200, 200, 640, 480,
		nullptr, nullptr, hInstance, nullptr
	);

	//show the damn window
	ShowWindow(hwnd, SW_SHOW);

	//message pump
	MSG msg;
	BOOL gResult;
	while (gResult = GetMessage(&msg, nullptr, 0, 0) > 0)
	{
		TranslateMessage(&msg);
		DispatchMessage(&msg);
	}

	if (gResult == -1)
	{
		return -1;
	}
	else
	{
		return (int)msg.wParam;
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值