VBA Positioning of Userform

A

Andy

I am trying to position a user form adjacent to a shape. I have taken
the PinX and pinY of the shape and converted these to windows
coordinates. However, my user form appears in the wrong place.

My screen resolution is 1280 x 1024. If my shape is in the middle of
the screen, i get approximately the right windows coordinates, allowing
for scroll bars etc. But my userform appears further right and lower.
If I move my userfrom th the right hand edge of the screen, It has a
left coordinate of 960, not around 1024.

Am I missing something here, shouldn't the left coordinate be relative
to the active window. The Visio window fills the screen, with no
rulers, shape windows etc.

Thanks in advance
 
J

JuneTheSecond

I found the position of mouse and userform are not equal.
In my pc, the differnce amounts 3 by 4.
In order to coincde them, the position of userform should be multfied by 0.75.
For example;

Private Declare Function SetCursorPos Lib "user32" (ByVal X As Long, ByVal Y
As Long) As Long

Sub test()
Dim X As Long, Y As Long
X = 1000
Y = 800
SetCursorPos X, Y
With UserForm1
.Show vbModeless
.Top = Y * 0.75
.Left = X * 0.75
End With
End Sub
 
J

JuneTheSecond

Here, I uploaded an example drawing that have VBA macro to move userform to
the position clicke by the mouse.
http://60.43.141.155/visiosquare/cgi-bin/upload/image/140.zip
If you unzip downloaded file, save Visio drawing, and then userform will
move to the clicked position. But I do not know the constant 0.75 in the code
of module "ThisDocument" is always correct, I wonder if it depends on
computors.
 
A

Andy

Thanks for your help. After some further searching I found the reason
for multiplying the pixel position by .75. It seems the userforms etc
are in points, so I have to convert from pixels to points, and this is
based on the screen dpi. The following functions can be used to
calculate the factor to be used, in my case my screen was 96dpi, so
this gives the factor of 72/96 = .75

Thanks again.

Declare Function GetDC Lib "user32" ( _
ByVal hwnd As Long) As Long

Declare Function GetDeviceCaps Lib "Gdi32" ( _
ByVal hDC As Long, _
ByVal nIndex As Long) As Long

Declare Function ReleaseDC Lib "user32" ( _
ByVal hwnd As Long, _
ByVal hDC As Long) As Long

Const LOGPIXELSX = 88
Const LOGPIXELSY = 90

Public Function pointsPerPixelX() As Double
Dim hDC As Long
hDC = GetDC(0)
pointsPerPixelX = 72 / GetDeviceCaps(hDC, LOGPIXELSX)
ReleaseDC 0, hDC
End Function

Public Function pointsPerPixelY() As Double
Dim hDC As Long
hDC = GetDC(0)
pointsPerPixelY = 72 / GetDeviceCaps(hDC, LOGPIXELSY)
ReleaseDC 0, hDC
End Function
 
J

JuneTheSecond

Thank you for your code, Andy. Now , I can convert pixel to points correctly.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top