
Originally Posted by
Arnoutdv
Do you have a sample for this?
Here's my CenterWindow function. If I remember correctly it's a direct translation from MFC.
Code:
'C Style Logical Not (!) vs VB Bitwise Inversion (~)
'C's ! operator does an implicit conversion to bool before inversion
Public Function NOT_(ByVal Expr As Long) As Long
'NOT_ = Not CBool(Expr)
NOT_ = (Expr = 0&)
End Function
Public Function CenterWindow(hWnd As Long, Optional hWndCenter As Long) As Long
If (hWndCenter) Then
Dim dwWindowStyle As Long
dwWindowStyle = GetWindowLong(hWndCenter, GWL_STYLE)
If (NOT_(dwWindowStyle And WS_VISIBLE) Or (dwWindowStyle And WS_MINIMIZE)) Then _
hWndCenter = NULL_
End If
Dim mi As MONITORINFO
mi.cbSize = LenB(mi)
Dim rcCenter As RECT
Dim rcScreen As RECT
If (hWndCenter) Then
Call GetMonitorInfo(MonitorFromWindow(hWndCenter, MONITOR_DEFAULTTONEAREST), mi)
rcScreen = mi.rcWork
Call GetWindowRect(hWndCenter, rcCenter)
Else
Call GetMonitorInfo(MonitorFromWindow(hWnd, MONITOR_DEFAULTTONEAREST), mi)
rcScreen = mi.rcWork
rcCenter = mi.rcWork
End If
Dim rc As RECT
Call GetWindowRect(hWnd, rc)
Dim W&, H&, X&, Y&
W = rc.Right - rc.Left
H = rc.Bottom - rc.Top
X = (rcCenter.Left + rcCenter.Right - W) \ 2&
Y = (rcCenter.Top + rcCenter.Bottom - H) \ 2&
If (X < rcScreen.Left) Then
X = rcScreen.Left
ElseIf (X > rcScreen.Right - W) Then
X = rcScreen.Right - W
End If
If (Y < rcScreen.Top) Then
Y = rcScreen.Top
ElseIf (Y > rcScreen.Bottom - H) Then
Y = rcScreen.Bottom - H
End If
Call SetWindowPos(hWnd, NULL_, X, Y, -1&, -1&, SWP_NOSIZE Or SWP_NOZORDER Or SWP_NOACTIVATE)
End Function
as for responding to WM_DISPLAYCHANGE
Code:
Option Explicit
Implements ISubclass
Private Declare Function FindWindow Lib "user32" Alias "FindWindowW" (Optional ByVal lpClassName As Long, Optional ByVal lpWindowName As Long) As Long
Private Declare Function GetWindow Lib "user32" (ByVal hWnd As Long, ByVal uCmd As Long) As Long
Private m_hWnd As Long
Public Property Get hWnd() As Long
hWnd = m_hWnd
End Property
Public Property Get Owner(ByVal hWnd As Long) As Long
Const GW_OWNER As Long = 4&
Owner = GetWindow(hWnd, GW_OWNER)
End Property
Private Sub Class_Initialize()
If (Forms.Count) Then
m_hWnd = Owner(Forms(0&).hWnd)
Else
App.Title = Right$("0000" & Hex$(ObjPtr(Me)), 4) & App.Title
m_hWnd = FindWindow(0&, StrPtr(App.Title))
App.Title = Mid$(App.Title, 5)
End If
SetSubclass m_hWnd, Me
End Sub
Private Sub Class_Terminate()
RemoveSubclass m_hWnd, Me
End Sub
Private Function ISubclass_SubclassProc(ByVal hWnd As Long, ByVal uMsg As Long, ByVal wParam As Long, ByVal lParam As Long, ByVal dwRefData As Long) As Long
Const WM_SETTINGCHANGE As Long = &H1A&
Const WM_DISPLAYCHANGE As Long = &H7E&
Const WM_KEYUP As Long = &H101&
Select Case uMsg
Case WM_DISPLAYCHANGE
DebugOut "display change"
Case WM_SETTINGCHANGE
DebugOut "setting change!"
Case WM_KEYUP
DebugOut "KeyPress!"
End Select
ISubclass_SubclassProc = DefSubclassProc(hWnd, uMsg, wParam, lParam)
End Function
edit:
NULL_ is the constant 0&