Solved

How to set the MousePosition on screen?

Posted on 2003-10-27
9
306 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
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 

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
 
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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

813 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

16 Experts available now in Live!

Get 1:1 Help Now