הערה
הגישה לדף זה מחייבת הרשאה. באפשרותך לנסות להיכנס או לשנות מדריכי כתובות.
הגישה לדף זה מחייבת הרשאה. באפשרותך לנסות לשנות מדריכי כתובות.
Question
Thursday, June 23, 2016 2:31 PM
I need to control mouse position and click the mouse on buttons in a window on a 2nd monitor. I have tried several methods of controlling mouse position and clicking, but have not found a way to send the mouse to any monitor other than the primary monitor. I'm working with Visual Basic (.NET 2015).
Public Class Form1
Public Declare Auto Function SetCursorPos Lib "User32.dll" (ByVal X As Integer, ByVal Y As Integer) As Long
Public Declare Sub mouse_event Lib "user32" Alias "mouse_event" (ByVal dwFlags As Long, ByVal dx As Long, ByVal dy As Long, ByVal cButtons As Long, ByVal dwExtraInfo As Long)
Public Const M_LEFTDOWN = &H2 ' left button down
Public Const M_LEFTUP = &H4 ' left button up
Private Sub OneClick_Click(sender As Object, e As EventArgs) Handles OneClick.Click
'OneClick is the button name
Dim Monitor As Object
Monitor = Screen.AllScreens(1).WorkingArea
'What can I do to use a specified monitor??
SetCursorPos(299, 299)
mouse_event(M_LEFTDOWN, 0, 0, 0, 0)
mouse_event(M_LEFTUP, 0, 0, 0, 0)
End Sub
End Class
All replies (6)
Thursday, June 23, 2016 2:36 PM ✅Answered
Have you coinsidered using the Cursor in .NET instead of the user32.dll?
then do something like:
Dim screen As Screen
screen = screen.AllScreens(2)
'mouse at 100,100 on second screen
Cursor.Position = screen.Bounds.Location + new Point(100, 100)
Thursday, June 23, 2016 2:52 PM
I had actually tried that first, with similar results... I have a Samsung SE790C (3440x1440 pixels) as primary monitor, and diagonally adjacent an older Flatron W2242T (1680x1050) where I am running some older legacy software that does not display correctly on the higher resolution monitor. I need to keep my control on the primary monitor, to operate the software on the old monitor. I'm not getting relative cursor positions to work properly between these two monitors, and actually, haven't gotten my widget to successfully move the cursor to the other monitor at all. Even if I start debugging in the second monitor, and run my app on that monitor, it still places the cursor positions on the main monitor...
Well, to correct myself, I just tried again with new -relative- coordinates and did get the mouse to move to the other monitor. I may be able to work with this - if I just figure out the actual pixel relativity of the two monitors once, then I can add that to my functions.
Thursday, June 23, 2016 2:55 PM
And what are you running? win 10?
Have you tried to update your older monitor drivers?
Thursday, June 23, 2016 3:17 PM
Thank you,
This
Dim screen As Screen screen = screen.AllScreens(2) Cursor.Position = screen.Bounds.Location + new Point(100, 100)
was what I needed.
Thursday, June 23, 2016 3:47 PM
Great, if it helped mark it as answer...
Thursday, June 23, 2016 7:04 PM
You should also be aware that you are using an old VB6 signature for the mouse_event api function. Vb.Net does not use Longs in the signatures like VB6 did. The correct signature for VB.Net would be as shown below. If you do not have Option Strict turned on, i would highly recommend turning it on too.
<System.Runtime.InteropServices.DllImport("user32.dll")>
Private Shared Sub mouse_event(ByVal dwFlags As UInteger, ByVal dx As Integer, ByVal dy As Integer, ByVal dwData As Integer, ByVal dwExtraInfo As UInteger)
End Sub
If you say it can`t be done then i`ll try it