|
-
Dec 12th, 1999, 11:52 PM
#1
Thread Starter
New Member
If anyone has a code snippet to show how to center a window using Win32
API, I'd appreciate seeing it.
Thanks.
-
Dec 13th, 1999, 01:03 AM
#2
Thread Starter
New Member
Thanks Aaron ... will give it a try.
-
Dec 13th, 1999, 12:49 PM
#3
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|