Reputation: 45
My laptop screen size is 1280x800. I want to set the cursor position (x and y values) to a certain % of my screen resoultion. when I use the following code, it moves the cursor to the bottom right corner of my screen. When I don't use variables and specifically give it coordinates, it works fine. See code below.
Public Declare Function GetSystemMetrics Lib "user32.dll" (ByVal index As Long) As Long
Private Declare Function SetCursorPos Lib "user32" (ByVal x As Integer, ByVal y As Integer) As Long
Public Const SM_CXSCREEN = 0
Public Const SM_CYSCREEN = 1
Sub SetCoordinates()
Dim xaxis, yaxis as long
xaxis = GetSystemMetrics(SM_CXSCREEN)
yaxis = GetSystemMetrics(SM_CYSCREEN)
xaxis = xaxis * 0.7421875 '74.21875% across the x axis of the screen
yaxis = yaxis * 0.875 '87.5% down the Y axis of the screen
SetCursorPos xaxis, yaxis
' The above line always moves the cursor to the bottom right corner of my screen. The math
'evaluates to the same numbers below. x = 950 and y = 700. When I don't use variables and
'specifically give it coordinates, it works fine. like below.
SetCursorPos 950, 700
End Sub
What should be done to get the SetCursorPos function to accept variable inputs? I tried changing ByVal to ByRef with the same results.
Upvotes: 1
Views: 6950
Reputation: 31
You can not use SetCursorPos with predefined variables, instead switch off option 'Option Explicit', and use undefined variables, like this:
Public Declare Function GetSystemMetrics Lib "user32.dll" (ByVal index As Long) As Long
Private Declare Function SetCursorPos Lib "user32" (ByVal x As Integer, ByVal y As Integer) As Long
Public Const SM_CXSCREEN = 0
Public Const SM_CYSCREEN = 1
Sub SetCoordinates()
Dim xaxis, yaxis as long
xaxis = GetSystemMetrics(SM_CXSCREEN)
yaxis = GetSystemMetrics(SM_CYSCREEN)
newx = xaxis * 0.7421875 '74.21875% across the x axis of the screen
newy = yaxis * 0.875 '87.5% down the Y axis of the screen
SetCursorPos newx, newy 'instead of xaxis, yaxis
'With the nondefined newx and newy variables,
'the above line will always move the cursor to the right position of the screen.
End Sub
Upvotes: 3
Reputation: 154
The Docs explain all
From Help.
SM_CXSCREEN
0 The width of the screen of the primary display monitor, in pixels. This is the same value obtained by calling GetDeviceCaps as follows: GetDeviceCaps( hdcPrimaryMonitor, HORZRES).
SetCursorPos Function
Parameters
X [in] Specifies the new x-coordinate of the cursor, in screen coordinates.
Y [in] Specifies the new y-coordinate of the cursor, in screen coordinates.
Windows Vista enables users to change the dots-per-inch (dpi) setting so that most user interface (UI) elements on the screen appear larger. Although this feature has long been available in Microsoft Windows, in previous versions the scaling had to be implemented by applications. In Windows Vista, the Desktop Window Manager performs default scaling for all applications that do not handle their own scaling. Active Accessibility client applications must take this feature into account.
Scaling in Windows Vista
The default dpi setting is 96, which means that 96 pixels occupy a width or height of one notional inch. The exact measure of an "inch" depends on the size and physical resolution of the monitor. For example, on a monitor 12 inches wide, at a horizontal resolution of 1280 pixels, a horizontal line of 96 pixels extends about 9/10 of an inch.
Changing the dpi setting is not the same as changing the screen resolution. With dpi scaling, the number of physical pixels on the screen remains the same. However, scaling is applied to the size and location of UI elements. This scaling can be performed automatically by the Desktop Window Manager (DWM) for the desktop and for applications that do not explicitly ask not to be scaled.
In effect, when the user sets the scale factor to 120 dpi, a vertical or horizontal inch on the screen becomes bigger by 25 percent. All dimensions are scaled accordingly. The offset of a window from the top and left edges of the screen increases by 25 percent. The size of the window increases in the same proportion, along with the offsets and sizes of all UI elements it contains.
Upvotes: -1