当前位置:网站首页>GetMessage底层机制分析
GetMessage底层机制分析
2022-07-01 18:45:00 【小小bugbug】
GUI线程的消息队列其实上是分为七个子消息队列,
其中有一个子消息队列专门存储sendmessage函数跨线程传递过来的消息
GetMessage内部内层循环会首先去判断sendMessageListHead子消息队列中是否由消息,
如果有则通过调用KeUserModeCallBack返回三环调用对应窗口的窗口过程函数去处理消息,
等所有sendMessageListHead子消息队列中的
消息被处理完后,会依次从其他队列中去获取消息,
这时如果获取到了消息则跳出外层循环GetMessage函数返回;
并把获取的消息传递给dispatchMessage函数处理;
如果没有取得消息就一直反复循环=>GetMessage阻塞的效果
注意在上述过程中处理sendMessageListHead子队列中的消息时,
是在GetMessage函数内部直接调用KeUserModeCallBack最终调用窗口过程函数去处理的;
而对于其他消息只是获取然后返回;
为什么说sendMessageListHead存储的是sendmessage函数跨线程传递过来的消息呢,
因为sendMessage只有跨线程传递时消息才走消息队列,
如果是同一进程sendMessage直接调用对应的窗口过程函数,
不会把消息放入消息队列中即不会把消息放入sendMessageListHead子消息队列
// WinMessage.cpp : 定义应用程序的入口点。
//
#include "framework.h"
#include "WinMessage.h"
#define MAX_LOADSTRING 100
// 全局变量:
HINSTANCE hInst; // 当前实例
WCHAR szTitle[MAX_LOADSTRING]; // 标题栏文本
WCHAR szWindowClass[MAX_LOADSTRING]; // 主窗口类名
HWND g_hwnd;
// 此代码模块中包含的函数的前向声明:
ATOM MyRegisterClass(HINSTANCE hInstance);
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
INT_PTR CALLBACK About(HWND, UINT, WPARAM, LPARAM);
int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
_In_opt_ HINSTANCE hPrevInstance,
_In_ LPWSTR lpCmdLine,
_In_ int nCmdShow)
{
UNREFERENCED_PARAMETER(hPrevInstance);
UNREFERENCED_PARAMETER(lpCmdLine);
// TODO: 在此处放置代码。
// 初始化全局字符串
LoadStringW(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
LoadStringW(hInstance, IDC_WINMESSAGE, szWindowClass, MAX_LOADSTRING);
MyRegisterClass(hInstance);
// 执行应用程序初始化:
if (!InitInstance (hInstance, nCmdShow))
{
return FALSE;
}
HACCEL hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_WINMESSAGE));
MSG msg;
SendMessage(g_hwnd, WM_LBUTTONDOWN, 0, 0);//如果sendMessage没有跨线程发送消息 即便注释掉整个消息循环,依然可以调用窗口过程函数=>这时消息不进入消息队列存储了
// 主消息循环:
/* while (GetMessage(&msg, nullptr, 0, 0)) { if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg)) { TranslateMessage(&msg); DispatchMessage(&msg); } }*/
return 0;
}
//
// 函数: MyRegisterClass()
//
// 目标: 注册窗口类。
//
ATOM MyRegisterClass(HINSTANCE hInstance)
{
WNDCLASSEXW wcex;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_WINMESSAGE));
wcex.hCursor = LoadCursor(nullptr, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wcex.lpszMenuName = MAKEINTRESOURCEW(IDC_WINMESSAGE);
wcex.lpszClassName = szWindowClass;
wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));
return RegisterClassExW(&wcex);
}
//
// 函数: InitInstance(HINSTANCE, int)
//
// 目标: 保存实例句柄并创建主窗口
//
// 注释:
//
// 在此函数中,我们在全局变量中保存实例句柄并
// 创建和显示主程序窗口。
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
hInst = hInstance; // 将实例句柄存储在全局变量中
HWND hWnd = CreateWindowW(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, nullptr, nullptr, hInstance, nullptr);
if (!hWnd)
{
return FALSE;
}
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
g_hwnd = hWnd;
return TRUE;
}
//
// 函数: WndProc(HWND, UINT, WPARAM, LPARAM)
//
// 目标: 处理主窗口的消息。
//
// WM_COMMAND - 处理应用程序菜单
// WM_PAINT - 绘制主窗口
// WM_DESTROY - 发送退出消息并返回
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_COMMAND:
{
int wmId = LOWORD(wParam);
// 分析菜单选择:
switch (wmId)
{
case IDM_ABOUT:
DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
break;
case IDM_EXIT:
DestroyWindow(hWnd);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
}
break;
case WM_PAINT:
{
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hWnd, &ps);
// TODO: 在此处添加使用 hdc 的任何绘图代码...
EndPaint(hWnd, &ps);
}
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
case WM_LBUTTONDOWN://依然被执行
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
// “关于”框的消息处理程序。
INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
UNREFERENCED_PARAMETER(lParam);
switch (message)
{
case WM_INITDIALOG:
return (INT_PTR)TRUE;
case WM_COMMAND:
if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
{
EndDialog(hDlg, LOWORD(wParam));
return (INT_PTR)TRUE;
}
break;
}
return (INT_PTR)FALSE;
}
边栏推荐
- Redis 实现限流的三种方式
- 商业智能BI开发和报表开发有什么本质区别?
- ubuntu14安装MySQL并配置root账户本地与远程访问
- English grammar_ Adjective / adverb Level 3 - precautions
- Lake Shore - crx-em-hf low temperature probe station
- 【pytorch记录】模型的分布式训练DataParallel、DistributedDataParallel
- MATLAB中subplot函数的使用
- 论文泛读【FiLM: Visual Reasoning with a General Conditioning Layer】
- SuperVariMag 超导磁体系统 — SVM 系列
- [go ~ 0 to 1] day 4 June 30 defer, structure, method
猜你喜欢
![[to.Net] C set class source code analysis](/img/59/4c7b910bc6505e5f81015ce80812fc.png)
[to.Net] C set class source code analysis

Lake Shore低温恒温器的氦气传输线

ubuntu14安装MySQL并配置root账户本地与远程访问

赋能「新型中国企业」,SAP Process Automation 落地中国

Lake Shore M91快速霍尔测量仪

一次SQL优化,数据库查询速度提升 60 倍

见证时代!“人玑协同 未来已来”2022弘玑生态伙伴大会开启直播预约

June issue | antdb database participated in the preparation of the "Database Development Research Report" and appeared on the list of information technology and entrepreneurship industries

Digital business cloud: from planning to implementation, how does Minmetals Group quickly build a new pattern of digital development?

Lake shore M91 fast hall measuring instrument
随机推荐
Learn MySQL from scratch - database and data table operations
水产行业智能供应链管理平台解决方案:支撑企业供应链数字化,提升企业管理效益
C-end dream is difficult to achieve. What does iFLYTEK rely on to support the goal of 1billion users?
Ubuntu14 install MySQL and configure root account local and remote access
Les canaux de culture intensive s'efforcent de développer Fu Xin et Wei Shi jiajie pour organiser une conférence de formation sur les nouveaux produits
Boost the development of digital economy and consolidate the base of digital talents - the digital talent competition was successfully held in Kunming
如何正确使用Vertx操作Redis(3.9.4带源码分析)
Learning notes - steps of JDBC connection database operation
transform + asm资料
Lake shore optimag superconducting magnet system om series
宝,运维100+服务器很头疼怎么办?用行云管家!
241. Different Ways to Add Parentheses
The former 4A executives engaged in agent operation and won an IPO
MySQL常用图形管理工具 | 黑马程序员
Solidity - 合约结构 - 错误(error)- ^0.8.4版本新增
nacos配置文件发布失败,请检查参数是否正确的解决方案
见证时代!“人玑协同 未来已来”2022弘玑生态伙伴大会开启直播预约
论文泛读【FiLM: Visual Reasoning with a General Conditioning Layer】
Once the SQL is optimized, the database query speed is increased by 60 times
Dom4j parsing XML, XPath retrieving XML