Link to home
Start Free TrialLog in
Avatar of Craig_Sparks
Craig_Sparks

asked on

How to set the MousePosition on screen?

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.

ASKER CERTIFIED SOLUTION
Avatar of JohnBPrice
JohnBPrice

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Craig_Sparks
Craig_Sparks

ASKER

Button1.Location.X
Button1.Location.Y

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

didn't see your post first. Ill try it now...
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?

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.

Sorry, this is VB 6 code too.  Gotta scoot, but I'll follow up tomorrow.
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?

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