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:

var
    I  : Integer;
    Ri : TRect;
const
    Margin = 100;  // Number of pixels to be seen, at least
begin
    I := 0;
    while I < Screen.MonitorCount do begin

        // Compute the intersection between screen and form
        Windows.IntersectRect(Ri, BoundsRect,

                              Screen.Monitors[I].BoundsRect);
        // Check the intersection is large enough
        if (Ri.Width > Margin) and (Ri.Height > Margin) then
            break;
        Inc(I);
    end;
    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;
    end;

end;
 
Suggested readings:
     Writing an iterator for a container
    Original method to iterate thru the bits of an integer
    Adding properties to a set
    Internet Component Suite (ICS)
    MidWare multi-tier framework

Follow me on Twitter

4 comments:

Craig Peterson said...

You can replace the loop with TScreen.MonitorFromWindow():

if Screen.MonitorFromWindow(Handle, mdNull) = null 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;
end;

Arioch, the said...

And if the're two monitors - the form would launch split half between them ?

And if monitors have different PPI resolution or there is taskbar in the middle - that would look weird...

I believe you select soem monitor and arrange form to it's center, not to the virtual screen center

Craig Peterson said...

@Arioch,

I thought the same, but that code is correct. TScreen.Width/Height actually returns the width/height of the primary monitor, not all of the monitors combined.

Jonathan Barton said...

Use Screen.DesktopWidth and Screen.DesktopHeight for the dimensions of the combined monitors.