赶上WM_DEVICECHANGE [英] catching the WM_DEVICECHANGE

查看:109
本文介绍了赶上WM_DEVICECHANGE的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何知道WM_DEVICECHANGE的到来?

WndProc被覆盖.我捕获了所有消息,但是没有一个是WM_DEVICECHANGE类型. RegisterDeviceNotification使链接器抱怨找不到该函数!所以我陷入了这种伏都教魔法中.请帮助.

WndProc's overwritten. I catch the whole bunch of messages but none of them are of type WM_DEVICECHANGE. RegisterDeviceNotification makes the linker to complain that it can't find the function! So I'm stuck in this voodoo magic. Kindly help.

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

P.S.: of course I have been googling and stackoverflowing (lol) all this stuff for about 8 hours.

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_pro c已执行,但从未执行过(当然,在设备更改时,我知道吗?)

lol_wnd_proc is executed but never when it supposed to (on device change of course, am I clear?)

推荐答案

问题是您正在创建

The problem is that you are creating a message-only window which does not receive broadcasts:

仅消息窗口使您可以发送和接收消息.它不可见,没有z顺序,无法枚举,并且不接收广播消息.窗口只是调度消息.

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.

So, you cannot use a message-only window and instead need to make a top-level window that is never shown. That's trivial to achieve — stop passing HWND_MESSAGE to CreateWindow and make sure that you never call ShowWindow.

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

As an aside, Sleep(1000) in the middle of a message loop is going to be a disaster. You need to pump messages in a timely fashion, not fall asleep on the job. You must get rid of that Sleep. Note that GetMessage will block if the queue is empty, so you don't need to worry about your application running hot.

您的消息循环应如下所示:

Your message loop should look like this:

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

这篇关于赶上WM_DEVICECHANGE的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆