Results 1 to 3 of 3

Thread: win32 API

  1. #1

    Thread Starter
    New Member
    Join Date
    Oct 1999
    Posts
    6

    Post

    If anyone has a code snippet to show how to center a window using Win32
    API, I'd appreciate seeing it.

    Thanks.

  2. #2

    Thread Starter
    New Member
    Join Date
    Oct 1999
    Posts
    6

    Post

    Thanks Aaron ... will give it a try.

  3. #3
    Guru Aaron Young's Avatar
    Join Date
    Jun 1999
    Location
    Red Wing, MN, USA
    Posts
    2,177

    Post

    You can use the SetWindowPos API to Center a Window, eg.

    Add a Timer Control to a Form..
    Code:
    Private Type RECT
            Left As Long
            Top As Long
            Right As Long
            Bottom As Long
    End Type
    
    Private Type POINTAPI
            x As Long
            y As Long
    End Type
    
    Private Declare Function GetWindowRect Lib "user32" (ByVal hwnd As Long, lpRect As RECT) As Long
    Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
    Private Declare Function WindowFromPoint Lib "user32" (ByVal xPoint As Long, ByVal yPoint As Long) As Long
    Private Declare Function GetParent Lib "user32" (ByVal hwnd As Long) As Long
    Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer
    
    Private Declare Function SetWindowPos Lib "user32" (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 Const SWP_NOSIZE = &H1
    Private Const SWP_NOZORDER = &H4
    Private Const SWP_NOACTIVATE = &H10
    
    Private Sub Form_Load()
        Timer1.Interval = 100
    End Sub
    
    Private Sub Timer1_Timer()
        Dim tPOINT As POINTAPI
        Dim lHwnd As Long
        Dim lParent As Long
        
        If GetAsyncKeyState(vbKeyControl) <> 0 And GetAsyncKeyState(vbKeyC) <> 0 Then
            Call GetCursorPos(tPOINT)
            lHwnd = WindowFromPoint(tPOINT.x, tPOINT.y)
            lParent = GetParent(lHwnd)
            If lParent Then lHwnd = lParent
            CenterWindow lHwnd
        End If
    End Sub
    
    Private Sub CenterWindow(ByVal lHwnd As Long)
        Dim iWidth As Integer
        Dim iHeight As Integer
        Dim tRECT As RECT
        
        Call GetWindowRect(lHwnd, tRECT)
        iWidth = Screen.Width \ Screen.TwipsPerPixelX
        iHeight = Screen.Height \ Screen.TwipsPerPixelY
        Call SetWindowPos(lHwnd, 0, (iWidth - (tRECT.Right - tRECT.Left)) / 2, (iHeight - (tRECT.Bottom - tRECT.Top)) / 2, 0, 0, SWP_NOSIZE Or SWP_NOZORDER Or SWP_NOACTIVATE)
    End Sub
    Point to a Form and Press CTRL + C to Center It.

    ------------------
    Aaron Young
    Analyst Programmer
    [email protected]
    [email protected]

Posting Permissions

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



Click Here to Expand Forum to Full Width