Windows编程之生成一个简单的完整的窗口

版权声明:转载请注明出处: https://blog.csdn.net/qq_33757398/article/details/82263392
#include <windows.h>

//窗口过程回调函数
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);

int WINAPI WinMain(
	HINSTANCE hInstance,
	HINSTANCE hPrevInstance,
	LPSTR lpCmdLine,
	int nShowCmd)
{
	HWND hwnd;//窗口句柄
	MSG msg;//消息
	WNDCLASS wc;//窗口类

	//1.设计一个窗口类
	wc.style = 0;
	wc.lpfnWndProc = (WNDPROC)WndProc;
	wc.cbClsExtra = 0;
	wc.cbWndExtra = 0;
	wc.hInstance = hInstance;
	wc.hIcon = LoadIcon(NULL, IDI_WINLOGO);
	wc.hCursor = LoadCursor(NULL, IDC_ARROW);
	wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
	wc.lpszMenuName = NULL;
	wc.lpszClassName = "MyWndClass";

	//2.注册窗口类
	RegisterClass(&wc);

	//3.创建窗口
	hwnd = CreateWindow(
		TEXT("MyWndClass"),
		TEXT("Hello SDK Application"),
		WS_OVERLAPPEDWINDOW,
		CW_USEDEFAULT,
		CW_USEDEFAULT,
		CW_USEDEFAULT,
		CW_USEDEFAULT,
		NULL,//父窗口句柄
		NULL,//窗口菜单句柄
		hInstance,
		NULL);

	//4.显示和更新窗口
	ShowWindow(hwnd, nShowCmd);
	UpdateWindow(hwnd);

	//5.消息循环
	while (GetMessage(&msg,NULL,0,0))
	{
		TranslateMessage(&msg);
		DispatchMessage(&msg);//转发到窗口过程
	}

	return msg.wParam;
}

LRESULT CALLBACK WndProc(
	HWND hwnd,
	UINT message,
	WPARAM wParam,
	LPARAM lParam)
{
	PAINTSTRUCT ps;
	HDC hdc;//DC句柄
	RECT rect;

	//对各种消息进行处理
	switch (message)
	{
	case WM_SIZE:
		//重画paint
		return 0;
	case WM_LBUTTONDOWN://鼠标左键按下
		//MessageBox(hwnd, TEXT("Mouse Clicked!"), TEXT("message"),MB_OK);
		//PostQuitMessage(0);
		return 0;
	case WM_PAINT://绘制消息
		hdc = BeginPaint(hwnd, &ps);
		GetClientRect(hwnd, &rect);
		//Ellipse(hdc, 0, 0, 200, 100);
		DrawText(hdc, TEXT("Hello Windows!"), -1, &rect,
			DT_SINGLELINE | DT_CENTER | DT_VCENTER);
		EndPaint(hwnd, &ps);
		return 0;

	case WM_DESTROY://销毁窗口消息(关闭)
		PostQuitMessage(0);
		return 0;
	default:
		break;
	}
	return DefWindowProc(hwnd, message, wParam, lParam);
}

运行结果:

猜你喜欢

转载自blog.csdn.net/qq_33757398/article/details/82263392