How to set a dialog always at the center of the screen?

I need to position a dialog box in the center of the user’s screen, no matter what the screen resolution or size.
I’ve tried some of the ideas in these topics…


But I haven’t succeeded.
========SIMPLE CODE PIECE========

Option Explicit
Option Compatible

Private oDlg As Object

Sub OpenDialog()
Dim oCC 				' ViewController
Dim oViewOnScreen		'
Dim oComponentWindow	
Dim oWindowPosSize		
Dim oToolKitWorkArea	
Dim oTopWindowPosSize	

	oDlg = CreateUnoDialog(DialogLibraries.Standard.Dialog1)
	oCC = ThisComponent.getCurrentController()
	REM Full Screen Mode (Except for the top bar)
	oCC.Frame.LayoutManager.HideCurrentUI = false
	oViewOnScreen = oCC.VisibleAreaOnScreen
	oComponentWindow = oCC.ComponentWindow
	REM A few metrics to help with possible solutions
	With oComponentWindow
		oWindowPosSize		= .getPosSize()
		oToolKitWorkArea	= .Toolkit.WorkArea
		oTopWindowPosSize	= .Toolkit.ActiveTopWindow.getPosSize()
	End With
	With oDlg
		.Model.PositionX = oTopWindowPosSize.Width/2 'oViewOnScreen.Width/2
		.Model.PositionY = oTopWindowPosSize.Height/2 'oViewOnScreen.Height/2
	End With
End Sub

Ask_dialog-position-on-screen.ods (12,4,KB)
Could someone help me with this?
I’ve even tried the Pythagorean Theorem, without success either. :sweat_smile:

1 Like

Try it:

With oDlg
	.setPosSize(oTopWindowPosSize.Width/2 - oDlg.PosSize.Width/2 , oTopWindowPosSize.Height/2 - oDlg.PosSize.Height/2, oDlg.PosSize.Width, oDlg.PosSize.Height, 15)	
End With
1 Like

@FelipeAle or if you want to set oDlg.Mode.PositionX / .PositionY then you need conversion of Size.

	dim oSize as new, koef as double
	with oSize 'size of oDlg.Model
	end with
	koef=oSize.Width / oDlg.convertSizeToPixel(oSize, 'coefficient for correction of Size

	with oDlg.Model
		.PositionX=(koef*oTopWindowPosSize.Width - .Width) / 2
		.PositionY=(koef*oTopWindowPosSize.Height - .Height) / 2
	end with
	with oDlg
	end with
1 Like

Thank you all for your suggested solutions. Both were satisfactory, but I’ll stick with @Zizi64 for its simplicity.
@KamilLanda , your solution was incredible too. However, I’ll have to study more before I apply.
Thanks for your solution as well, @jfn .

Hi Felipe,

I modified one of our subs which we use to position a dialog relative to the user’s active cell. This seems to work:

Sub CentreDialog
Dim Dispatcher, Document As Object
Dim Doc, ObjSelect, Cell As Object
Dim oContainerWindow As Object
Dim ObjControl As Object
Dim LogSheet As Object
Dim Posw, Window As Object
Dim ArgOut As Long
Dim BoxH, BoxW As Long
Dim Bottom, Right,X, Y As Long
Dim args1(0) As New

Dlg = CreateUnoDialog(DialogLibraries.Standard.EventInputWizard)
Doc = ThisComponent
oContainerWindow = Doc.getCurrentController.GetFrame.getContainerWindow()
LogSheet = Doc.Sheets.getByName(“Log Page”)

Right = oContainerWindow.getPosSize().Width
Bottom = oContainerWindow.getPosSize().Height

BoxH = Dlg.PosSize.Height
BoxW = Dlg.PosSize.Width

X = (Right - BoxW)*0.5
Y = (Bottom - BoxH)*0.5

Dlg.setPosSize(X, Y,
ArgOut = Dlg.Execute()

Document = ThisComponent.CurrentController.Frame
Dispatcher = createUnoService(“”)

’ The wizard is closed, we extract the data.
If ArgOut = 1 Then
MsgBox “Get your data”
End If


Exit Sub

MsgBox “Something went wrong. Please try again.”

End Sub 'CentreDialog


here’s the code I’m using for this in my macros and extensions. It works AFAIK (but might possibly have glitches :slight_smile:

Sub CenterDialog(ByRef pDlg As Object, Optional ByRef pCallerWindow As Object)
'centers a given dialog in front of a “owning” dialog or screen.
'-- pDlg: the dialogue to be centered.
'-- pCallerWindow: (optional) the window that owns the dialog.
’ If not specified, the owner is the LibO module frame.

Dim lo_Ctnr As Object		'the container object
Dim l_VOffset As Long
Dim l_HOffset As Long
Dim X As Long
Dim Y As Long

If IsMissing(pCallerWindow) Then pCallerWindow = ThisComponent.CurrentController.Frame

'check caller container
If pCallerWindow.SupportsService("") Then
	'module frame
	lo_Ctnr = pCallerWindow.ContainerWindow
ElseIf pCallerWindow.SupportsService("") Then
	lo_Ctnr = pCallerWindow
	'not something we know of
	Exit Sub
End If

'set new position
l_HOffset = (lo_Ctnr.PosSize.Width  - pDlg.PosSize.Width) \ 2
l_VOffset = (lo_Ctnr.PosSize.Height - pDlg.PosSize.Height) \ 2
X = pDlg.PosSize.X + l_HOffset
Y = pDlg.PosSize.Y + l_VOffset
pDlg.setPosSize(X, Y, 0, 0,

End Sub 'CenterDialog