Solved

How to set the MousePosition on screen?

Posted on 2003-10-27
9
303 Views
Last Modified: 2012-08-13
I've tried:

Me.MousePosition =
Form1.MousePosition.X =

etc but they are ReadOnly.

How do you write to it and further how would you make it so the mouse pointer is moved to the position of a button in the form?

effectivly:  MousePosition = Button1.Possition

Thanks.

0
Comment
Question by:Craig_Sparks
  • 5
  • 4
9 Comments
 
LVL 16

Accepted Solution

by:
JohnBPrice earned 125 total points
ID: 9630050
You can use win API mouse_event as follows, it's deprecated, but still works.  You can also use SendInput, but it's more complicated.

Option Explicit
Private Declare Sub mouse_event Lib "user32" (ByVal dwFlags As Long, ByVal dx As Long, ByVal dy As Long, ByVal cButtons As Long, ByVal dwExtraInfo As Long)
Private Const MOUSEEVENTF_ABSOLUTE = &H8000 '  absolute move
Private Const MOUSEEVENTF_MOVE = &H1 '  mouse move

Private Sub Command1_Click()
    Call mouse_event(MOUSEEVENTF_ABSOLUTE + MOUSEEVENTF_MOVE, 5000, 5000, 0, 0)
End Sub
0
 

Author Comment

by:Craig_Sparks
ID: 9630051
Button1.Location.X
Button1.Location.Y

gets the button position. Now just how to move the mouse pointer there :)

0
 

Author Comment

by:Craig_Sparks
ID: 9630055
didn't see your post first. Ill try it now...
0
 

Author Comment

by:Craig_Sparks
ID: 9630162
When I do:

Dim XPos, YPos As Long
XPos = Button1.Location.X
YPos = Button1.Location.Y
TextBox1.Text = "X: " & XPos & " Y: " & YPos

Call mouse_event(MOUSEEVENTF_ABSOLUTE + MOUSEEVENTF_MOVE, XPos, YPos, 0, 0)

The position of Button1 is correctly displayed in the TextBox but the mouse pointer just goes up near the upper left corner.

For that matter it goes to the same place everytime no matter what I put in for XPos or YPos, like:

Call mouse_event(MOUSEEVENTF_ABSOLUTE + MOUSEEVENTF_MOVE, 5000, 5000, 0, 0)

How is the absolute postion related on onscreen pixel positions?

0
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 16

Expert Comment

by:JohnBPrice
ID: 9630194
The X and Y you pass to mouse_event are in normalized absolute coordinates i.e. a scale from 0 to 65536 of the screen.  If you want to move the mouse to a button on your form, you can use

    Const FudgeX = 80
    Const FudgeY = 475
    Call mouse_event(MOUSEEVENTF_ABSOLUTE + MOUSEEVENTF_MOVE, (Me.Left + Command2.Left + FudgeX) / Screen.Width * 65535, (Me.Top + Command2.Top + FudgeY) / Screen.Height * 65535, 0, 0)

The fudge factors are because me.left and me.top return the client area of your form, and excluding the window title bar.  You can get the exact numbers using SystemParametersInfo like this

Public Const SPI_GETWORKAREA = 48
Public Declare Function SystemParametersInfo Lib "user32" Alias "SystemParametersInfoA" (ByVal uAction As Long, ByVal uParam As Long, ByRef lpvParam As Any, ByVal fuWinIni As Long) As Long
Public Type RECT
        Left As Long
        Top As Long
        Right As Long
        Bottom As Long
End Type

But you pixels, not twips, so you'd have to covert to twips before you add them to button2.left, etc.

0
 
LVL 16

Expert Comment

by:JohnBPrice
ID: 9630205
Sorry, this is VB 6 code too.  Gotta scoot, but I'll follow up tomorrow.
0
 

Author Comment

by:Craig_Sparks
ID: 9630261
Call mouse_event(MOUSEEVENTF_ABSOLUTE + MOUSEEVENTF_MOVE, 65535, 65535, 0, 0)

puts it in the lower left corner. Its like the X doesnt work, only the Y. weird.

Yes, I'm using VB.NET 2002. Could that effect it?

0
 

Author Comment

by:Craig_Sparks
ID: 9630368
John,

Thanks again for your help. Here is the .NET solution:

Dim Pos As System.Drawing.Point =  Button1.Location
System.Windows.Forms.Cursor.Current.Position = Pos


Craig
0
 
LVL 16

Expert Comment

by:JohnBPrice
ID: 9633439
OK, Good.  BTW, you still need to add in your form offset if your form is not at 0,0, as follows (I don't know a good way to get the true form location including the non-client area.)

Dim Pos As System.Drawing.Point = Button1.Location

'size of the side non-client area
Dim XOffset As Integer = (Me.Size.Width - Me.ClientSize.Width) / 2

'side of the top non-client area
Dim YOffset As Integer = Me.Size.Height - Me.ClientSize.Height - XOffset

Pos.X = Pos.X + Me.Location.X + XOffset
Pos.Y = Pos.Y + Me.Location.Y + YOffset

System.Windows.Forms.Cursor.Current.Position = Pos
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now