MFC实现原理基础
程序员文章站
2024-03-25 00:01:47
...
实现步骤:
- 注册窗口类
- 创建窗口
- 显示窗口
- 更新窗口
- 进行消息循环
- 消息转换
- 消息处理
代码实现:
//包含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;
}
上一篇: Android蓝牙BLE4.0踩过的坑
下一篇: Android开发总结之低功耗蓝牙开发