Results 1 to 3 of 3

Thread: Centering a form using API

  1. #1
    Member
    Join Date
    Feb 99
    Posts
    34
    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

  2. #2
    Frenzied Member Vlatko's Avatar
    Join Date
    Aug 00
    Location
    Skopje, Macedonia
    Posts
    1,409
    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
    I am become death, the destroyer of worlds.
    mail:vlatkovr@hotmail.com

    • Visual Basic 6.0 & .NET
    • Visual C++ 6.0 & .NET
    • ASP
    • LISP
    • PROLOG
    • C
    • Pascal

  3. #3
    PowerPoster Chris's Avatar
    Join Date
    Jan 99
    Location
    K-PAX
    Posts
    3,238
    bucko, what you need to use the API?

    Code:
    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

    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •