Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

How to set the MousePosition on screen?

Posted on 2003-10-27
9
Medium Priority
?
322 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 500 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
Independent Software Vendors: 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
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…
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…
Suggested Courses

604 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