MFC实现原理基础

实现步骤:

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

代码实现:

//包含Windows头文件
#include

//用于进行消息处理的回调函数
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)