Offset centre popup form.

Zak14

Registered User.
Local time
Today, 00:00
Joined
Jun 27, 2014
Messages
166
I am using the below code to centre a popup form, both horizontally and vertically.

All my non-popup forms are 25cm wide.
It looks weird that the popup forms appear on the centre of the screen, when the centre of the form is more towards the left. This is especially true in widescreens.

How can I alter this code, so that the popup form appears on the centre (horizontally only) of the first 25cm of the screen.
25 cm = 9.8425 in = 14173 twips = 945 pixels

Edit:
I've attached a picture to explain better


Code:
' API declarations:
#If Win64 Then
    Private Declare PtrSafe Function apiGetClientRect Lib "user32" Alias "GetClientRect" (ByVal hwnd As Long, lpRect As typRect) As Long
    Private Declare PtrSafe Function apiGetWindowRect Lib "user32" Alias "GetWindowRect" (ByVal hwnd As Long, lpRect As typRect) As Long
    Private Declare PtrSafe Function apiSetWindowPos Lib "user32" Alias "SetWindowPos" (ByVal hwnd As Long, ByVal hWndInsertAfter As Long, ByVal x As Long, ByVal y As Long, ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long) As Long
    Private Declare PtrSafe Function apiShowWindow Lib "user32" Alias "ShowWindow" (ByVal hwnd As Long, ByVal nCmdShow As Long) As Long
#Else
    Private Declare Function apiGetClientRect Lib "user32" Alias "GetClientRect" (ByVal hwnd As Long, lpRect As typRect) As Long
    Private Declare Function apiGetWindowRect Lib "user32" Alias "GetWindowRect" (ByVal hwnd As Long, lpRect As typRect) As Long
    Private Declare Function apiSetWindowPos Lib "user32" Alias "SetWindowPos" (ByVal hwnd As Long, ByVal hWndInsertAfter As Long, ByVal x As Long, ByVal y As Long, ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long) As Long
    Private Declare Function apiShowWindow Lib "user32" Alias "ShowWindow" (ByVal hwnd As Long, ByVal nCmdShow As Long) As Long
#End If
' Type declarations:
Private Type typRect
    Left As Long
    Top As Long
    Right As Long
    Bottom As Long
End Type
' Constant declarations:
Private Const SW_RESTORE = 9
Private Const SWP_NOSIZE = &H1 ' Don't alter the size
Private Const SWP_NOZORDER = &H4 ' Don't change the Z-order
Private Const SWP_SHOWWINDOW = &H40 ' Display the window
' **************************************
' * Center a form in the Access window *
' **************************************
' Created 1-22-2002 by Peter M. Schroeder
Public Function gfncCenterForm(parForm As Form) As Boolean
    Dim varAccess As typRect, varForm As typRect
    Dim varX As Long, varY As Long
    
    On Error GoTo CenterForm_Error
    Call apiGetClientRect(hWndAccessApp, varAccess) ' Get the Access client area coordinate
    Call apiGetWindowRect(parForm.hwnd, varForm) ' Get the form window coordinates
    varX = CLng((varAccess.Left + varAccess.Right) / 2) - CLng((varForm.Right - varForm.Left) / 2) ' Calculate a new left for the form
    varY = CLng((varAccess.Top + varAccess.Bottom) / 2) - CLng((varForm.Bottom - varForm.Top) / 2) ' Calculate a new top for the form
    varY = varY - 45 ' Adjust top for true center
    varY = varY - 20 ' Adjust top for appearance
    Call apiShowWindow(parForm.hwnd, SW_RESTORE) ' Restore form window
    Call apiSetWindowPos(parForm.hwnd, 0, varX, varY, (varForm.Right - varForm.Left), (varForm.Bottom - varForm.Top), SWP_NOZORDER Or SWP_SHOWWINDOW Or SWP_NOSIZE) ' Set new form coordinates
    gfncCenterForm = True
    Exit Function
    
CenterForm_Error:
    gfncCenterForm = False
End Function

Thanks.
 

Attachments

  • Capture.PNG
    Capture.PNG
    91.2 KB · Views: 663
Last edited:
Umm, not sure I get it... My pop ups are centered even on my wide screen. Maybe the correct question is why are your Forms appearing slightly to the left? Mine also auto center.
 
Umm, not sure I get it... My pop ups are centered even on my wide screen. Maybe the correct question is why are your Forms appearing slightly to the left? Mine also auto center.

Sorry I probably didn't do a good job of explaining.
I've attached a picture. Hopefully, it will explain it better.


Seems awfully complicated - why not just use the move function in your popup form open event?

https://msdn.microsoft.com/en-us/library/office/aa221370(v=office.11).aspx

That wouldn't work since the popup form will not be centred vertically on different screen sizes. I want it centred according to screen size vertically (just as it is, when i use the above code), but I want it 12.5cm from the left horizontally.
 

Attachments

  • Capture JPG.jpg
    Capture JPG.jpg
    93.9 KB · Views: 457
That wouldn't work since the popup form will not be centred vertically on different screen sizes.
If you say so, tho' I've never had a problem in positioning forms exactly where I want them.
 
An Access.Form has a .Move method. I would use that.
 
There is even simpler code

Private Declare Function GetSystemMetrics Lib "user32" (ByVal nIndex As Long) As Long

Dim ScreenWidth As Long
Dim ScreenHeight As Long


ScreenWidth = GetSystemMetrics(0)
ScreenHeight = GetSystemMetrics(1)


Incidentally Zak, you don't need to declare your API's twice, once for 64 bit and again for 32 bit, you just need to declare it the once with ptrsafe, no if Win64 required
 
Thanks everyone for your replies.

I've discarded the entire code snippet in on my first post and gone with the below:
Code:
Private Function fctPositionForm()
    Dim intCm As Double
    Dim intDatabaseWidth As Integer
    Dim intDatabaseHeight As Integer
    Dim intLeftPosition As Integer
    Dim intTopPosition As Integer
    intCm = 567
    intDatabaseWidth = 25 * intCm
    intDatabaseHeight = 17 * intCm
    intLeftPosition = Int((intDatabaseWidth / 2) - (Me.InsideWidth / 2))
    intTopPosition = Int((intDatabaseHeight / 2) - (Me.InsideHeight / 2))
    DoCmd.MoveSize intLeftPosition, intTopPosition
End Function

I don't care about the different monitor sizes anymore - it's too complicated.
I just went with the centre of the first horizontal 25cm (which again, is the width of my database) and the centre of the first vertical 17cm, since I've seen that the smallest of monitors will be roughly 17cm.
The problem with this is that, in the larger monitors, the forms will appear somewhat above the centre, but that's fine. I've never used Libraries and I don't know how they work - it's better that I keep it simple.
 

Users who are viewing this thread

Back
Top Bottom