1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| Private Declare Function GetDC Lib "user32.dll" (ByVal hWnd As Long) As Long
Private Declare Function ReleaseDC Lib "user32.dll" (ByVal hWnd As Long, ByVal hdc As Long) As Long
Private Declare Function GetDeviceCaps Lib "gdi32.dll" (ByVal hdc As Long, ByVal nIndex As Long) As Long
Function PositionForm(Form As Object, rng As Range)
Dim pttopx#, ttop#, lleft#, ecc#, Hheight#, Wwidth#, zz#
Dim PixelsPerPointsX As Double, PixelsPerPointsY As Double
DC = GetDC(0)
PixelsPerPointsX = GetDeviceCaps(DC, 88) / 72
PixelsPerPointsY = GetDeviceCaps(DC, 90) / 72
ReleaseDC 0, DC
lleft = ActiveWindow.PointsToScreenPixelsX(rng.Left * PixelsPerPointsX) / PixelsPerPointsX
ttop = ActiveWindow.PointsToScreenPixelsY(rng.Top * PixelsPerPointsY) / PixelsPerPointsY
PositionForm = Array(lleft, ttop)
End Function
Sub TestUserform()
r = PositionForm(UserForm1, [b3])
With UserForm1: .Show 0: .Left = r(0): .Top = r(1): End With
End Sub |