PDA

Click to See Complete Forum and Search --> : Centering a form using API


bucko
Jan 24th, 2001, 02:43 AM
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

Vlatko
Jan 24th, 2001, 08:50 AM
This will get the screen size:

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

Chris
Jan 24th, 2001, 12:45 PM
bucko, what you need to use the API?


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


or you can archieve this without the API


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