Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

method to Showmessage centred on Form? 2

Status
Not open for further replies.

CADTenchy

Technical User
Dec 12, 2007
237
GB
I have a small app that pops up a Showmessage.
As this app will be run on stations with dual screens, and I don't know which screen they prefer it to be on, or where, is there a method to pop the Showmessage (or another box) centred on the main form's location, rather than spanning the join of the two screens?
 
make a popup screen based on a tform yourself.

in the FormCreate event you can alter the left property of your form:

// set form to the last available monitor
Left := Screen.Monitors[Screen.MonitorCount-1].Left+100;


-----------------------------------------------------
What You See Is What You Get
Never underestimate tha powah of tha google!
 
Use MessageDlgPos. Base its coordinates on the main form's location.
 
I too use dual monitors for developement. My solution is to set Form.Position to ScreenCenter, NOT DesktopCenter, which results in splitting both screens. ScreenCenter will center it on the default monitor.

A word of caution:
In your Windows Desktop>Screen>Properies, if you have monitor 2 on the left or if Monitor 1 is not the primary, beware of Delphi Form.Position = poDesigned - if you put a form on the left monitor. This will result in Form.Left being a negative number. Users without identical monitor configurations will not be able to see or close your form!

The only way to be sure if designing for dual monitors is to "Read Monitor.WorkAreaRect to learn the dimensions of the monitor, minus any space taken by task bars or tool bars. WorkAreaRect gives the dimensions of the work area in pixels, where (0,0) represents the top-left corner of the primary monitor." (from delphi help)

The short of it is you need to look at how the desktop is "currently" configured before making assumptions.

Applies to forms and dialogs. THere is much more you can do with other more basic properties of TMonitor and TScreen.

Roo
Delphi Rules!
 
Thanks all for replies.

Now I have the below code.
For the form calling it (main form) I have set the properties as so:

DefaultMonitor: dmPrimary
Position: psScreenCenter

With those settings, will code below be OK, regarding Roo's comments etc? (currently I'm working at home on laptop, with no dual screen test bed..)

Steve

Code:
procedure TSDIAppForm.Button3Click(Sender: TObject);
Var
  Sure, Xpos, Ypos: integer;
begin
  Xpos := (SDIAppForm.Width Div 2) + SDIAppForm.Left - 120;
  Ypos := (SDIAppForm.Height Div 2) + SDIAppForm.Top - 50;
  Sure := MessageDlgPos('Are you sure?',mtCustom, [mbYes,mbCancel], 0, Xpos, Ypos);
end;
 
Yes - Tested on dual monitor with the following actual code:
Code:
procedure TForm1.Button3Click(Sender: TObject);
//procedure TSDIAppForm.Button3Click(Sender: TObject);
Var
  Sure, Xpos, Ypos: integer;
begin
  Xpos := (Self{SDIAppForm}.Width Div 2) + Self{SDIAppForm}.Left - 120;
  Ypos := (Self{SDIAppForm}.Height Div 2) + Self{SDIAppForm}.Top - 50;
  Sure := MessageDlgPos('Are you sure?',mtCustom, [mbYes,mbCancel], 0, Xpos, Ypos);
end;
(Reason for changes should be obvious) Results:
I tried it with Form1.Position eq. to poDesigned, poScreenCenter and poDesktopCenter. Message was always visible and relative to Form1. Works!

Roo
Delphi Rules!
 
Cool, thanks Roo.

And yep can't beat a small community that spans, oh, the globe...

 
And next the universe...
915.gif
...y'all!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top