gpt4 book ai didi

捕捉 WM_DEVICECHANGE

转载 作者:可可西里 更新时间:2023-11-01 09:21:15 28 4
gpt4 key购买 nike

如何知道 WM_DEVICECHANGE 的到来?

WndProc 被覆盖。我收到了所有消息,但没有一条是 WM_DEVICECHANGE 类型的。 RegisterDeviceNotification 使链接器提示找不到函数!所以我陷入了这个巫毒魔法。请帮忙。

P.S.:当然,我已经用谷歌搜索和 stackoverflowing(笑)所有这些东西大约 8 个小时了。

int CALLBACK WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow)
{
LPTSTR lolclassname = "lolclass";
WNDCLASS lolclass;
HWND lolwindow;
MSG lolmsg;
UINT msgstatus;

lolclass.style = CS_VREDRAW;
lolclass.lpfnWndProc = &lol_wnd_proc;
lolclass.cbClsExtra = 0;
lolclass.cbWndExtra = 0;
lolclass.hInstance = hInstance;
lolclass.hIcon = NULL;
lolclass.hCursor = NULL;
lolclass.hbrBackground = (HBRUSH)(COLOR_BACKGROUND + 1);
lolclass.lpszMenuName = NULL;
lolclass.lpszClassName = lolclassname;
if(!RegisterClass(&lolclass)) fail("RegisterClassEx");

lolwindow = CreateWindow("lolclass", NULL, WS_MINIMIZE, CW_USEDEFAULT,
CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
HWND_MESSAGE, NULL, hInstance, NULL);

if(lolwindow == NULL) fail("CreateWindowEx");

/*ShowWindow(lolwindow, nCmdShow);
UpdateWindow(lolwindow);*/

do {
/* if(!SetWindowPos(lolwindow, HWND_TOPMOST, 1, 1, 1, 1,
SWP_HIDEWINDOW))
fail("SetWindowPos");*/
msgstatus = GetMessage(&lolmsg, lolwindow, 0, 0);
if(!msgstatus) break;
if(msgstatus == - 1) fail("GetMessage");
TranslateMessage(&lolmsg);
DispatchMessage(&lolmsg);
Sleep(1000);
} while(1);

return lolmsg.wParam;
}

lol_wnd_proc 已执行但从未按预期执行(当然是在设备更改时,我清楚吗?)

最佳答案

问题是您正在创建一个 message-only window不接收广播:

A message-only window enables you to send and receive messages. It is not visible, has no z-order, cannot be enumerated, and does not receive broadcast messages. The window simply dispatches messages.

因此,您不能使用仅显示消息的窗口,而需要制作一个永远不会显示的顶级窗口。这很容易实现 — 停止将 HWND_MESSAGE 传递给 CreateWindow 并确保您永远不会调用 ShowWindow


顺便说一句,消息循环中间的 Sleep(1000) 将是一场灾难。您需要及时发送消息,而不是在工作中睡着。您必须摆脱那个 Sleep。请注意,如果队列为空,GetMessage 将阻塞,因此您无需担心您的应用程序运行过热。

你的消息循环应该是这样的:

while( (bRet = GetMessage( &msg, NULL, 0, 0 )) != 0)
{
if (bRet == -1)
{
// handle the error and possibly exit
}
else
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}

关于捕捉 WM_DEVICECHANGE,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10168467/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com