|
|
Title | Center the mouse over a control |
Keywords | center mouse, mouse, center cursor, cursor |
Categories | Tips and Tricks, API, Controls |
|
|
You can use this routine to center the mouse over a control. For example, when you display a custom dialog box, you can put the mouse over the Ok button. (Personally I don't like that feature but it's common on some operating systems so your users may be accustomed to it.)
Use the ClientToScreen API function to convert the coordinates (0, 0) in the control's coordinate space into the screen's coordinate space. Add half of the control's width and height (in pixels), and use SetCursorPos to move the mouse to the desired position.
|
|
' Center the mouse over the control.
Private Sub CenterMouseOverControl(ByVal ctl As Control)
Dim pt As POINTAPI
' Get the control's upper left position
' in screen coordinates.
' Note that this function converts the values
' in pt into screen coordinates. Because we just
' allocated pt, the values start as 0.
ClientToScreen ctl.hwnd, pt
' Position the cursor. SetCursorPos uses screen
' coordinates (pixels).
SetCursorPos _
pt.X + ScaleX(ctl.Width / 2, ScaleMode, vbPixels), _
pt.Y + ScaleY(ctl.Height / 2, ScaleMode, vbPixels)
End Sub
|
|
|
|
|
|