Set Dialog Position based of other dialogs

Hints, tips and tricks for newbies

Moderators: Dorian (MJT support), JRL

Post Reply
Dominic_Fichera
Pro Scripter
Posts: 82
Joined: Mon Mar 24, 2014 12:15 pm

Set Dialog Position based of other dialogs

Post by Dominic_Fichera » Mon Aug 03, 2015 1:00 pm

Hey guys,

Is there anyway to set a dialog's position based off a "parent dialog"? E.g. I have a main menu, click a button, and the new window opens centred over the top of the existing dialog? In Dialog Properties, there's an option for "Position=poOwnerFormCenter", so don't know if I'd have to change something like this.

Thanks! :)

Dominic

User avatar
JRL
Automation Wizard
Posts: 3526
Joined: Mon Jan 10, 2005 6:22 pm
Location: Iowa

Re: Set Dialog Position based of other dialogs

Post by JRL » Mon Aug 03, 2015 4:27 pm

I'm not aware of an automatic way to do this. AFAIK you just have to do the math. Here's two examples one using SetParent and one not using SetParent. SetParent is a Microsoft function that lets you make a dialog (or any other window) functionally become an object in another dialog (or any other window). Note that the script without SetParent requires that Dialog1 be shown prior to acquiring its position. Also note that in that same script Dialog1 contains an invisible button that exists solely for the easy acquisition of the location of Dialog1's client area position. In the script with SetParent, the parent dialog's position is irrelevant. No parent dialog positional math is required because dialog2 becomes a child of the coordinate system within the client area of dialog1.

With SetParent

Code: Select all

Dialog>Dialog1
object Dialog1: TForm
  Caption = 'Parent'
  ClientHeight = 400
  ClientWidth = 600
end
EndDialog>Dialog1

AddDialogHandler>Dialog1,,OnClose,Quit

Dialog>Dialog2
object Dialog2: TForm
  Caption = 'Child'
  ClientHeight = 200
  ClientWidth = 200
end
EndDialog>Dialog2

Let>WIN_USEHANDLE=1
  GetWindowSize>Dialog2.Handle,Dlg2W,Dlg2H
  GetDialogProperty>Dialog1,,ClientWidth,Dlg1W
  GetDialogProperty>Dialog1,,ClientHeight,Dlg1H
  Let>Dlg2X={round((%Dlg1W%/2)-(%Dlg2W%/2))}
  Let>Dlg2Y={round((%Dlg1H%/2)-(%Dlg2H%/2))}
  MoveWindow>Dialog2.Handle,Dlg2X,Dlg2Y
Let>WIN_USEHANDLE=0

LibFunc>User32,SetParent,SPres,Dialog2.Handle,Dialog1.Handle

Show>Dialog1
Show>Dialog2

Label>Loop
  Wait>0.01
Goto>Loop

SRT>Quit
  Exit>0
END>Quit
Without SetParent

Code: Select all

Dialog>Dialog1
object Dialog1: TForm
  Caption = 'Parent'
  ClientHeight = 400
  ClientWidth = 600
  object MSButton1: tMSButton
    Left = 0
    Top = 0
    Visible = False
  end
end
EndDialog>Dialog1

AddDialogHandler>Dialog1,,OnClose,Quit

Dialog>Dialog2
object Dialog2: TForm
  Caption = 'Child'
  ClientHeight = 200
  ClientWidth = 200
end
EndDialog>Dialog2

Show>Dialog1

Let>WIN_USEHANDLE=1
  GetWindowSize>Dialog2.Handle,Dlg2W,Dlg2H
  GetWindowPos>Dialog1.MSButton1.Handle,Dlg1X,Dlg1Y
  GetDialogProperty>Dialog1,,ClientWidth,Dlg1W
  GetDialogProperty>Dialog1,,ClientHeight,Dlg1H
  Let>Dlg2X={round(%Dlg1X%+((%Dlg1W%/2)-(%Dlg2W%/2)))}
  Let>Dlg2Y={round(%Dlg1Y%+((%Dlg1H%/2)-(%Dlg2H%/2)))}
  MoveWindow>Dialog2.Handle,Dlg2X,Dlg2Y
Let>WIN_USEHANDLE=0

Show>Dialog2

Label>Loop
  Wait>0.01
Goto>Loop

SRT>Quit
  Exit>0
END>Quit

Post Reply
Sign up to our newsletter for free automation tips, tricks & discounts