Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" _
(ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function WindowFromPoint Lib "user32" _
(ByVal xPoint As Long, ByVal yPoint As Long) As Long
Private Declare Function ClientToScreen Lib "user32" _
(ByVal hwnd As Long, lpPoint As POINTAPI) As Long
Private Declare Function GetDeviceCaps Lib "Gdi32" (ByVal hDC As Long, ByVal _
nIndex As Long) As Long
Private Declare Function GetDC Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function ReleaseDC Lib "user32" (ByVal hwnd As Long, ByVal _
hDC As Long) As Long
Private Const LOGPIXELSX = 88
Private Const LOGPIXELSY = 90
Private Type POINTAPI
x As Long
y As Long
End Type
'The width of a pixel in Excel's userform coordinates
'thanks to Stephen Bullen
Public Function PointsPerPixelX() As Double
Dim hDC As Long
hDC = GetDC(0)
'A point is defined as 1/72 of an inch and LOGPIXELSX returns
'the number of pixels per logical inch, so divide them to give
'the width of a pixel in Excel's userform coordinates
PointsPerPixelX = 72 / GetDeviceCaps(hDC, LOGPIXELSX)
ReleaseDC 0, hDC
End Function
'The HEIGHT of a pixel in Excel's userform coordinates
'thanks to Stephen Bullen
Public Function PointsPerPixelY() As Double
Dim hDC As Long
hDC = GetDC(0)
'A point is defined as 1/72 of an inch and LOGPIXELSY returns
'the number of pixels per logical inch, so divide them to give
'the width of a pixel in Excel's userform coordinates
PointsPerPixelY = 72 / GetDeviceCaps(hDC, LOGPIXELSY)
ReleaseDC 0, hDC
End Function
' displays the frame hwnd
Sub CommandButton1_Click()
Dim P As POINTAPI
Dim lngHwnd, lngFrameHwnd As Long
Dim lngLeftInPixels, lngTopInPixels As Long
lngHwnd = FindWindow(vbNullString, Me.Caption)
lngLeftInPixels = Me.Frame1.Left / PointsPerPixelX
lngTopInPixels = Me.Frame1.Top / PointsPerPixelY
P.x = lngLeftInPixels
P.y = lngTopInPixels
ClientToScreen lngHwnd, P
lngFrameHwnd = WindowFromPoint(P.x, P.y)
MsgBox "The Frame handle is : " & lngFrameHwnd
End Sub