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…

e

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

Option Explicit
Option Compatible

Private oDlg As Object

Sub OpenDialog()
Dim oCC 				' ViewController
Dim oViewOnScreen		' com.sun.star.awt.Rectangle
Dim oComponentWindow	
Dim oWindowPosSize		
Dim oToolKitWorkArea	
Dim oTopWindowPosSize	

	DialogLibraries.LoadLibrary("Standard")
	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
		.execute()
		.dispose()
	End With
	
End Sub

========SAMPLE========
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)	
	.execute()
	.dispose()
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 com.sun.star.awt.Size, koef as double
	
	with oSize 'size of oDlg.Model
		.Width=oDlg.Model.Width
		.Height=oDlg.Model.Height
	end with
	koef=oSize.Width / oDlg.convertSizeToPixel(oSize, com.sun.star.util.MeasureUnit.APPFONT).Width 'coefficient for correction of Size

	with oDlg.Model
		.PositionX=(koef*oTopWindowPosSize.Width - .Width) / 2
		.PositionY=(koef*oTopWindowPosSize.Height - .Height) / 2
	end with
	
	with oDlg
		.execute()
		.dispose()
	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 com.sun.star.beans.PropertyValue

DialogLibraries.LoadLibrary(“Standard”)
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, com.sun.star.awt.PosSize.POS)
ArgOut = Dlg.Execute()

Document = ThisComponent.CurrentController.Frame
Dispatcher = createUnoService(“com.sun.star.frame.DispatchHelper”)

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

Dlg.Dispose()

Exit Sub

ErrorHandler:
MsgBox “Something went wrong. Please try again.”
Dlg.Dispose()

End Sub 'CentreDialog

Felipe,

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.
'Input:
'-- 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("com.sun.star.frame.Frame") Then
	'module frame
	lo_Ctnr = pCallerWindow.ContainerWindow
ElseIf pCallerWindow.SupportsService("com.sun.star.awt.UnoControlDialog") Then
	'dialogue
	lo_Ctnr = pCallerWindow
Else
	'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, com.sun.star.awt.PosSize.POS)

End Sub 'CenterDialog

HTH,

2 Likes