Solved

How to set the MousePosition on screen?

Posted on 2003-10-27
9
308 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

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

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…

756 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