Twips, pixels, and conversion between them
I use the following code to move the mouse at one selected spot (20000, 20000) on the screen.
Code:
mouse_event MOUSEEVENTF_MOVE Or MOUSEEVENTF_ABSOLUTE, 20000, 20000, 0, 0
If I click the mouse at THAT spot, the Form_MouseDown returns the following coords: x=351, y=237 (There are in PIXELS, I believe). (see EDIT)
I want to know how I can convert 351,237 back to 20000, 20000.. I don't think that mouse_event coords (20000,20000) are in twips (351 * 15 != 20000, but 5265).
I'm sure that's a quite easy one.. Thanks for helping me out.
Edit: I started using the GetCursorPos API to get the X,Y coords in pixels - x=351,y=263 would be more like it. Extra pixels in the Y coord because the form_mousedown would calculate only the visible form, EXCLUDING the title bar. In my case, I need the title bar included... MOUSEEVENTF_MOVE with coords 1000 and 1000 will bring the mouse over the title bar, and that's something I may need to do.
Re: Twips, pixels, and conversion between them
TWIPS are 1440 to an inch.
Screen.TwipsPerPixelX and .TwipsPerPixelY will give you values for translating between pixels (which are device specific) and TWIPS - which are a static value.
Re: Twips, pixels, and conversion between them
Using mouse_event coords VS pixels:
5000, 5000 - 87, 65
10000,10000 - 175,131
20000,20000 - 351,263
By trial an error, I noticed that I could take the X coord in pixel (87, 175 and 351) and multiply it by 57 to have the corresponding "mouse_event X coord".
I can work with this from now on, but I just don't understand WHY "57".
Re: Twips, pixels, and conversion between them
Read szlammy's post. He explained it CLEARLY. 1440 twips per inch. If you change your screen resolution, that number will no longer be valid.
Twip = PixelNum& * screen.twipsperpixelx for horizontal and screen.twipsperpixely for vertical.
If you are using API, you can get the client area of your window with
Code:
Private Declare Function GetClientRect Lib "user32" (ByVal hWnd As Long, ByRef lpRect As RECT) As Long
where hwnd is the handle to your window
and you can convert form pixel position to screen pixel position with this:
Code:
Public Declare Function ClientToScreen Lib "user32" (ByVal hWnd As Long, lpPoint As POINTAPI) As Long
where as above hwnd is the handle to your form.
Both RECT and POINTAPI are structures defined in the api text browser.