January 9, 2013
Make sure a form is visible on an existing monitor
In most of my applications, I make all form size and position persistent: On startup (FormShow) I read the size and position back from an ini file and on close, I write the size and position back to the ini file.
But sometimes, the number of attached monitor is reduced, or I change the relative position of the monitors, or the screen resolution is lowered. This has the negative side effect that my forms are sometime positioned on an invisible part of the desktop!
The fix for this annoying issue is quite easy. #Delphi has a global variable “Screen” which holds an array of monitor descriptors. It is enough to iterate thru this array and check if the form position lies inside a visible monitor area. Actually the code below make sure at least 100 pixels are visible.
Here is the code to put in the FormShow event handler:
I : Integer;
Ri : TRect;
Margin = 100; // Number of pixels to be seen, at least
I := 0;
while I < Screen.MonitorCount do begin
// Compute the intersection between screen and form
// Check the intersection is large enough
if (Ri.Width > Margin) and (Ri.Height > Margin) then
if I >= Screen.MonitorCount then begin
// Form is outside of any monitor.
// Move to center of main monitor
Top := (Screen.Height - Height) div 2;
Left := (Screen.Width - Width) div 2;
Writing an iterator for a container
MidWare multi-tier framework
Follow me on Twitter