tags:

views:

171

answers:

2

I'm trying to control the coordinates of where my program opens a new window because currently they're opening ontop of each other. Does anyone have a working example of how to do this?

+6  A: 

You can always set the .Top and .Left properties manually, like this:

procedure TForm1.Button1Click(Sender: TObject);
var
  frm : TForm;
begin
  frm := TForm.Create(Self);
  frm.Left := 100;  //replace with some integer variable
  frm.Top := 100;  //replace with some integer variable
  frm.Show;
end;

However, Windows has a default window placement algorithm that tries to keep the title bars of each window visible. On my computer, repeated clicks to this Button1 procedure give nicely stacked windows:

procedure TForm1.Button1Click(Sender: TObject);
var
  frm : TForm;
begin
  frm := TForm.Create(Self);
  frm.Show;
end;

Also, don't forget that you can use the built-in set of TPosition locations:

procedure TForm1.Button1Click(Sender: TObject);
var
  frm : TForm;
begin
  frm := TForm.Create(Self);
  frm.Position := poOwnerFormCenter;
  {
  Other possible values:
    TPosition = (poDesigned, poDefault, poDefaultPosOnly, poDefaultSizeOnly,
      poScreenCenter, poDesktopCenter, poMainFormCenter, poOwnerFormCenter);

  //}
  frm.Show;
end;
JosephStyons
Thanks for all the help!Do you know of a way to set the x,y of an outside program though? For instance if I open the default browser with my current application the commands you listed wouldn't work :-/
You should edit your question to include this clarification.
Scott W
@kogus: nicely constructed answer (+1)! It's a shame the OP was not clear.
Argalatyr
+4  A: 

This type of functionality has been explained for C# in another question on SO.

Also, for Delphi, check out Understanding and Using Windows Callback Functions in Delphi which describes getting handles for windows that are currently open. And see Shake a window (form) from Delphi code which describes how to move a window once you've got its handle.

Scott W