欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

MFC实现原理基础

程序员文章站 2024-03-25 00:01:47
...

实现步骤:

  1. 注册窗口类
  2. 创建窗口
  3. 显示窗口
  4. 更新窗口
  5. 进行消息循环
  6. 消息转换
  7. 消息处理

代码实现:

//包含Windows头文件
#include<Windows.h>

//用于进行消息处理的回调函数
LRESULT CALLBACK WinProc(HWND hWNDnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
	switch (uMsg)
	{
	case WM_LBUTTONDOWN:
		MessageBox(hWNDnd, TEXT("xiaoming come on"), TEXT("mouse"), MB_OK);
		break;
	case WM_PAINT:
		PAINTSTRUCT ps;
		BeginPaint(hWNDnd, &ps);
		break;
	case WM_DESTROY:
		PostQuitMessage(2);
		break;
	default:
		return DefWindowProc(hWNDnd, uMsg, wParam, lParam);
		break;
	}

	return 0;
}

int WINAPI WinMain( HINSTANCE hInstance,  HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
//创建窗口类
	WNDCLASS wnd;
	wnd.cbClsExtra = 0;
	wnd.cbWndExtra = 0;
	wnd.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
	wnd.hCursor = LoadCursor(NULL, IDC_HELP);
	wnd.hIcon = LoadIcon(NULL, IDI_WARNING );
	wnd.hInstance = hInstance;
	wnd.lpfnWndProc = WinProc;
	wnd.lpszClassName = TEXT("ABC");
	wnd.lpszMenuName = NULL;
	wnd.style = nShowCmd;
//注册窗口类
	RegisterClass(&wnd);
//创建窗口
	HWND hWnd=CreateWindow(TEXT("ABC"), TEXT("hello"), WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL);
//显示窗口
	ShowWindow(hWnd,SW_SHOWNORMAL);
	//更新窗口
	UpdateWindow(hWnd);

	MSG msg;
	//进行消息循环
	while (GetMessage(&msg, NULL, 0, 0))
	{
	//消息转换
		TranslateMessage(&msg);
		//消息处理
		DispatchMessage(&msg);
	}
	return msg.wParam;
}
相关标签: MFC实现