Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to know whether my application is visible or not?

I've seen some software which can detect whether or not a form is visible to the user. This might include being minimized, another screen covering it up, monitor turned off, and even when on remote desktop, knows when the remote desktop view is not visible. I'm guessing it has to do with whether or not anything is being drawn in the application. Perhaps video drivers can provide this information?

How can I make my application to detect this? Is there a Windows message I can monitor for this?

For the record, the mentioned software is one which streams multiple surveillance cameras in real-time (RTSP) which uses this ability to the advantage of pausing streaming when the screen isn't visible.

like image 310
Jerry Dodge Avatar asked Feb 15 '23 18:02

Jerry Dodge


2 Answers

Most of the conditions do not have window messages associated with them, so you have to detect the conditions manually.

Use the TForm.WindowState property, or the Win32 API IsIconic() function, to detect your window's minimized state. You can also catch WM_SYSCOMMAND messages looking for the SC_MINIMIZE, SC_MAXIMIZE, and SC_RESTORE states.

Use the Win32 API EnumWindows() function to loop through all top-level windows, calling GetWindowRect() on each one, to detect if any areas of your window are not covered by other windows. To account for z-ordering, you may have to use GetTopWindow() and GetNextWindow() to iterate the z-order to see which window is on top of the other window.

Use MonitorFromWindow() and GetDevicePowerState() to detect the local monitor's power state. You can also catch WM_SYSCOMMAND messages looking for the SC_MONITORPOWER notification.

Detecting the Remote Desktop state is a bit trickier. You can use ProcessIdToSessionId() and WTSQuerySessionInformation(WTSIsRemoteSession) (Windows 7+) or GetSystemMetrics(SM_REMOTESESSION) to determine if your app is running inside of a Remote Desktop session, but I don't think you can detect whether the remote display is on/off (although WTSQuerySessionInformation() can query the remote display's resolution and color depth, and even if the session is locked/unlocked).

like image 105
Remy Lebeau Avatar answered Feb 17 '23 07:02

Remy Lebeau


I'm guessing it has to do with whether or not anything is being drawn in the application.

For partially obscured windows, Canvas.ClipRect (which is equal to the rcPaint member of PAINTSTRUCT or the result gotten from GetUpdateRect) will be the portion of the device context that has to be redrawn. GetUpdateRect can be called outside a WM_PAINT handler, Canvas.ClipRect only within.

But if you could of would solely rely on paint messages being sent, I am not sure. I think Remy's suggestions are more robust. Or a combination of all.

like image 35
NGLN Avatar answered Feb 17 '23 08:02

NGLN