0

I'm handling WndProc in my WPF application in order to respond to the event where some other application enters fullscreen. At this time, the main window of my application needs to hide. Here is the code I have written:

Hooking:

HwndSource source = HwndSource.FromHwnd(MyMainWindowHandle);
source.AddHook(this.WndProc);

WndProc:

private IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
    {
        Debug.Print("In wndproc");
        QUERY_USER_NOTIFICATION_STATE state;

        SHQueryUserNotificationState(out state);

        if (MyMainWindow.Visibility == Visibility.Visible && state == QUERY_USER_NOTIFICATION_STATE.QUNS_BUSY)
        {
            Debug.Print("hiding");
            MyMainWindow.Hide();
        }
        else if (MyMainWindow.Visibility == Visibility.Hidden && state != QUERY_USER_NOTIFICATION_STATE.QUNS_BUSY)
        {
            Debug.Print("showing");
            MyMainWindow.Show();
        }

        return IntPtr.Zero;
    }

This works as expected with applications like PowerPoint or Skype. However, when a web browser (Chrome) enters full screen while playing a video, the WndProc is not called when the user exits full screen. It is called and behaves as expected after the user does something else, like click the Windows task bar, etc. Does anyone know why / a workaround to this issue?

xandermonkey
  • 4,054
  • 2
  • 31
  • 53
  • I have found this related question, https://stackoverflow.com/questions/40059589/detecting-fullscreen-app-exit-in-windows windows does not hide your form when an application enters full screen mode it just happens to overlay yours. – Nathan May 24 '17 at 17:18
  • It is crude, a WndProc() always should be interested in a *specific* message. But there certainly is not one for "a window was made full screen". So if Chrome just creates a window that is as large as the monitor and doesn't otherwise cause your program to be deactivated then you just don't know. Nor would there be any point in hiding a window, so no harm done. – Hans Passant May 24 '17 at 17:24
  • @HansPassant this is not the issue. The WndProc *is* called and behaves normally when chrome *enters* fullscreen. The issue is only present on exit. And, as the question states, the WndProc behaves normally once the user moves their mouse over the taskbar or does any other significant action. – xandermonkey May 24 '17 at 17:26
  • 1
    Just have a look for yourself, use the Spy++ utility. It tells you exactly which messages you get. – Hans Passant May 24 '17 at 17:37
  • A small update, if I exit fullscreen and leave the machine without touching it for about 1 minute, the WndProc gets called. Why would Windows wait so long to send this message again? – xandermonkey May 24 '17 at 17:40
  • @HansPassant I am trying to use the tool, but when I attach it to my application's window (or any window, for that matter) and open the messages screen, nothing is shown. – xandermonkey May 24 '17 at 17:45
  • @AlexRosenfeld Called with what message? – 15ee8f99-57ff-4f92-890c-b56153 May 24 '17 at 17:46
  • @EdPlunkett I'm not sure what you mean – xandermonkey May 24 '17 at 17:46
  • @AlexRosenfeld When WndProc is called a minute after you exit fullscreen and leave the machine untouched, what is [the value of the `int msg` parameter](https://wiki.winehq.org/List_Of_Windows_Messages) to `WndProc`? – 15ee8f99-57ff-4f92-890c-b56153 May 24 '17 at 17:47
  • @EdPlunkett Allow me to place a debug print line and rerun the application. Thanks for your patience. I should add that the way I'm checking for if some other app was fullscreen is NOT through identifying a specific `msg`, but through the use of `SHQueryUserNotificationState`. – xandermonkey May 24 '17 at 17:48
  • @AlexRosenfeld No prob. But the value of `msg` will tell you *why* `WndProc` was called, which is important. Welcome to Petzoldville, population... pretty sparse, these days. – 15ee8f99-57ff-4f92-890c-b56153 May 24 '17 at 17:50
  • I see. Thanks. I'm rebuilding and debugging now... – xandermonkey May 24 '17 at 17:51
  • @EdPlunkett The message value that coincide with when the application reappears is 49886. – xandermonkey May 24 '17 at 17:59
  • I should add, the function is called with that `msg` number constantly during run time. It may not mean much, given how common / unrelated to fullscreen events it seems. – xandermonkey May 24 '17 at 18:11
  • @AlexRosenfed I'm not finding anything for that one on Google. – 15ee8f99-57ff-4f92-890c-b56153 May 24 '17 at 18:20
  • I'm unable to find anything for that as well. Something strange is going on, not sure what. – xandermonkey May 24 '17 at 18:29

0 Answers0