How to register/listen the desktop window to my own WndProc?


  • I need to get the the mouse messages on the desktop window (GetDesktopWindow()), how can I register my own WndProc so I can listen to those mouse messages?

    I thought I could do the following, however, per MSDN, RegisterClass() is for CreateWindow(), etc. As Desktop window has been created, how can I assign my own WndProc to the desktop window so I can listen all mouse messages?

      WCHAR consoleTitle[500];
        GetConsoleTitle( consoleTitle, 500);    
        HWND hwndConsole = FindWindow(NULL, consoleTitle);  
        HINSTANCE hInstance = (HINSTANCE)GetWindowLong(hwndConsole, GWL_HINSTANCE);
        WNDCLASS wc = {0};
        wc.hbrBackground =(HBRUSH)GetStockObject(HOLLOW_BRUSH);
        wc.hCursor = LoadCursor( hInstance, IDC_ARROW );
        wc.hIcon = LoadIcon( hInstance, IDI_APPLICATION );
        // Provide handle from GetDesktopWindow() to WNDCLASS ?!
        wc.hInstance = GetDesktopWindow();
        wc.lpfnWndProc = WndProc;
        wc.lpszClassName = L"Test Window"; = CS_HREDRAW | CS_VREDRAW;
        if (! RegisterClass( &wc ) )
            // throw exception;
        HWND hDesktop;
        RECT rt;
        hDesktop = GetDesktopWindow();
        GetWindowRect(hDesktop, &rt);
        // Use CreateWindow() to create the desktop window again?


    • Editado Wood-MSDN miércoles, 07 de marzo de 2012 1:14
    miércoles, 07 de marzo de 2012 1:13

Todas las respuestas

  • As I don't know the objective you need to fulfill with this request I cannot be sure, but I'll suggest an alternative anyway:  Raw Input.  It is a great alternative to mouse or keyboard hooking.

    Jose R. MCP

    miércoles, 07 de marzo de 2012 1:22
  • Thanks for the info, look like Raw Input still needs to use WM_* (windows message), e.g. WM_INPUT to get the raw input. So I guess my initial question still apply, but correctly me if I'm wrong.

    Also, raw input has a structure called RAWINPUTDEVICE structure, which has usUsagePage and usUsage. Where can I get the usUsagePage/usUsage for the following input devices?

    keyboard, mouse and touch?

    Basically, I need to capture the input device's (mouse/keyboard/touch) input from a user's computer and display what the mouse's co-ordinate/action, keyboard input and touch point co-ordinate/action on my app.

    Their input is not limited to my app's window area, but the overall user's desktop, regardless my app is in the foreground or background.


    miércoles, 07 de marzo de 2012 6:13
  • You could try and use the SetWinEventHook function.

    Microsoft MVP (Visual C++)

    Polymorphism in C

    miércoles, 07 de marzo de 2012 12:13
  • Yes, you need a window that receives the WM_INPUT message, but the good thing is that you don't need to subclass a foreign window and instead you can simply provide your own window, which simplifies the scenario.

    As for the codes, for mouse and keyboard you use usUsagePage = 1, usUsage = 1 or 6 (or was it 6 or 1?  Verify it).  For other input devices, I don't know.  You'll have to look them up or request them from Microsoft, maybe from Microsoft Support (not free).

    Jose R. MCP

    miércoles, 07 de marzo de 2012 13:47