Search code examples
windowswinapieventsdwm

Hook to Win32 API event to get notified when Desktop fully loads


Imagine a scenario when a user is logged in and without signing out switches to another user. Then switches back. When switching back (because we never signed out), Windows will first display a black desktop then do some Window flickering (especially with 2 monitors), then finally re-arrange everything to the state that we left off. That flickering and re-arrangement takes some time (roughly 1 second).

What would be the most efficient way of getting a notification about Desktop being fully loaded? I need this because I want to call my specific function when the desktop is fully loaded.

One option is to hook to WM_PAINT, but I used Spy++ and I get 12 WM_PAINT events on desktop, so that won't be reliable. Anyone aware of another way?


Solution

  • Found a much, much simpler solution. A call to to DwmFlush(). The function is part of Windows DWM api.

    Here is a sample code that I tested and it actually worked with fast user switching in Windows 8.1:

    while (true)
    {
        HRESULT hr = DwmFlush();
    
        if (hr == S_OK)
        {
            std::cout << "TRUE" << std::endl;
        }
        else
        {
            std::cout << "FALSE" << std::endl;
        }
    
        Sleep(1000);
    
    }