0

I am writing an Alt+Tab replacement in C#, and have trouble with fullscreen applications.

Is there a way to detect if a SetForegroundWindow(hWnd) call is going to change the screen resolution? Or equivalently, if hWnd is a fullscreen application? I would like to wait until the resolution change is done, or if there is no change, proceed immediately.

The screen resolution change is done asynchronously, the function call returns well before it happens, so my code runs prematurely, and draws my application onto the surface of the fullscreen application, with wrong dimensions, then after the resolution change, it looks especially ugly.

Source of my application is at https://bitbucket.org/FrigoCoder/frigotab/src if anyone is interested.

To clarify, I would be more interested in knowing beforehand if a resolution change occurs than detecting it later. I already know a half-solution where I call SetForegroundWindow() on GetDesktopWindow() or some other window and watch SystemEvents.DisplaySettingsChanging and DisplaySettingsChanged. This however only gives me a late asynchronous notification if a resolution change occurs, and does not tell me if it does not.

mx0
  • 6,445
  • 12
  • 49
  • 54
Frigo
  • 1,709
  • 1
  • 14
  • 32
  • You are trying to solve the wrong problem. Just handle resolution changes, in one place, and everything will be fine. When the resolution changes, simply redraw your UI. No need for any hacks. – IInspectable Aug 18 '17 at 18:19

2 Answers2

2

I managed to solve the issue. Instead of trying to detect fullscreen applications, I simply send an inactivation message to the foreground application, which triggers an early resolution change:

SendMessage(GetForegroundWindow(), WM_ACTIVATEAPP, false, GetCurrentThreadId());

This exact same message is also sent during application switches, so I essentially emulate one before it actually happens. I have not encountered any side effects yet.

Mind you however, that this does not solve DWM issues. Windows 7 automatically disables DWM composition for compatibility launches, or when it detects direct access to the primary display surface. It does not allow you to re-enable, and I do not see an easy solution to this problem. Thankfully this issue will eventually go away since DWM composition is always enabled in Windows 8 and newer.

Frigo
  • 1,709
  • 1
  • 14
  • 32
0

Perhaps the Winforms Event SizeChanged can help you.

You could use this event as a continuation of sorts for the rest of your code. In the case that the event doesn't fire due to no resizing, you could have a secondary continuation that will run after a specified timeout. It's not perfect, but may meet your needs.

IInspectable
  • 46,945
  • 8
  • 85
  • 181
  • I think the OP wants to know if the resolution *will* change, **before** he actually performs the alt+tab – Rafalon Aug 18 '17 at 15:44
  • Added some more detail to my question. Basically yeah I would like to know beforehand for best results. I already have a primitive way of detecting a resolution change later, but does not give me an immediate answer, and does not give me an answer at all if there is no resolution change. – Frigo Aug 18 '17 at 17:57