Hi, can anyone point me in the right direction for centering a form using API.
the reason for this is, I'm using VBA and it doesn't recognise the screen function
thanks in advance
MICK
Printable View
Hi, can anyone point me in the right direction for centering a form using API.
the reason for this is, I'm using VBA and it doesn't recognise the screen function
thanks in advance
MICK
This will get the screen size:
Code:Private Declare Function GetSystemMetrics Lib "user32" (ByVal nIndex As Long) As Long
Const SM_CXSCREEN = 0 'X Size of screen
Const SM_CYSCREEN = 1 'Y Size of Screen
Me.Print "Screen X:" + Str$(GetSystemMetrics(SM_CXSCREEN))
Me.Print "Screen Y:" + Str$(GetSystemMetrics(SM_CYSCREEN))
'center it then
bucko, what you need to use the API?
or you can archieve this without the APICode:Option Explicit
Private Declare Function MoveWindow Lib "user32" (ByVal hwnd As Long, ByVal x As Long, ByVal y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal bRepaint As Long) As Long
Private Sub Form_Load()
Dim NewX As Long
Dim NewY As Long
Dim NewW As Long
Dim NewH As Long
NewX = ScaleX((Screen.Width - Me.Width) \ 2, vbTwips, vbPixels)
NewY = ScaleY((Screen.Height - Me.Height) \ 2, vbTwips, vbPixels)
NewW = ScaleX(Me.Height, vbTwips, vbPixels)
NewH = ScaleY(Me.Height, vbTwips, vbPixels)
MoveWindow Me.hwnd, NewX, NewY, NewW, NewH, True
End Sub
Code:Private Sub Form_Resize()
If Me.WindowState <> vbMinimized Then
Me.Left = (Screen.Width - Me.Width) \ 2
Me.Top = (Screen.Height - Me.Height) \ 2
End If
End Sub