6 Months Free & No Setup Fees ASP.NET Hosting!
Skip Navigation Links
Home
Forum Home
Latest 50
Unanswered
Win Prizes
All Time Leaders
Jump to CategoryExpand Jump to Category
Login 
    Welcome Guest!
 Search Forum For :  
X
 Login
Please login to submit a new post, reply and edit exiting posts, see user profiles, and access more features. If you are not a registered member, Register here.
User Id / Email:
Password:  
Forgot Password | Forgot UserName
   Home » General » How To: Control the exact position of the mouse on your screen (Outside of the form)
       
Author Reply
Scott Pendleton
posted 1 posts
since Sep 02, 2010 
from

How To: Control the exact position of the mouse on your screen (Outside of the form)

  Posted on: 03 Sep 2010       
Hello!
----------------------------------------
I'm new to this forum and I would like to contribute something that might be helpful to a good amount of people!
The code I am going to show you will tell you how to control the exact position of your mouse (On your screen of course :P)
--------------------------------------------------------
The code below is the entire source code, if you want to only take certain parts.
 Module Mouse
   
 Public 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)

    Public Declare Function SetCursorPos Lib "user32" (ByVal x As Long, ByVal y As Long) As Long

    Public Declare Function GetCursorPos Lib "user32" (ByVal lpPoint As POINTAPI) As Long

    Public Const MOUSEEVENTF_LEFTDOWN = &H2

    Public Const MOUSEEVENTF_LEFTUP = &H4

    Public Const MOUSEEVENTF_MIDDLEDOWN = &H20

    Public Const MOUSEEVENTF_MIDDLEUP = &H40

    Public Const MOUSEEVENTF_RIGHTDOWN = &H8

    Public Const MOUSEEVENTF_RIGHTUP = &H10

    Public Const MOUSEEVENTF_MOVE = &H1

    Public Structure POINTAPI

        Dim x As Long
   
     Dim y As Long

    End Structure
    Public Function GetX() As Long
        Dim n As POINTAPI

        GetCursorPos(n)

        GetX = n.x

    End Function

    Public Function GetY() As Long

        Dim n As POINTAPI

        GetCursorPos(n)

        GetY = n.y


    End Function

    Public Sub LeftClick()

        LeftDown()

        LeftUp()

    End Sub

    Public Sub LeftDown()

        mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0)

    End Sub

    Public Sub LeftUp()

        mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0)

    End Sub

    Public Sub MiddleClick()

        MiddleDown()

        MiddleUp()

    End Sub

    Public Sub MiddleDown()

        mouse_event(MOUSEEVENTF_MIDDLEDOWN, 0, 0, 0, 0)

    End Sub

    Public Sub MiddleUp()

        mouse_event(MOUSEEVENTF_MIDDLEUP, 0, 0, 0, 0)

    End Sub

    Public Sub RightClick()

        RightDown()

        RightUp()

    End Sub

    Public Sub RightDown()

        mouse_event(MOUSEEVENTF_RIGHTDOWN, 0, 0, 0, 0)

    End Sub

    Public Sub RightUp()

        mouse_event(MOUSEEVENTF_RIGHTUP, 0, 0, 0, 0)

    End Sub

    Public Sub MoveMouse(ByVal xMove As Long, ByVal yMove As Long)

        mouse_event(MOUSEEVENTF_MOVE, xMove, yMove, 0, 0)

    End Sub

    Public Sub SetMousePos(ByVal xPos As Long, ByVal yPos As Long)

        SetCursorPos(xPos, yPos)

    End Sub


End Module
 


That will just about do it, there isn't any Reference's to made nor are there any imports :) Just copy and paste!

Here is an example on how to use it:
'This will get the coordinates for the exact center of the screen 
	Dim intX As Integer = Screen.PrimaryScreen.Bounds.Width
        Dim intY As Integer = Screen.PrimaryScreen.Bounds.Height
'Moves the mouse to the center of the screen and a little bit down.
        Mouse.SetMousePos(intX \ 2, intY \ 2 + 90)
'Click's on the coordinates
	Mouse.LeftClick()

I hope that this will be useful to you in anyway! I will also use this as some sort of a database (Just in case I loose it :P)

- Scott Pendleton
       
Top Articles
View all »
DevExpress Free UI Controls
Dynamic PDF
ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
Discover the top 5 tips for understanding .NET Interop
Ricky Leeks presents the top 5 tips for understanding .NET Interoperability. Learn more.
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Nevron Chart for .NET 2010.1 Now Available
The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
ASP.NET 4 Hosting
Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites – Click Here!
6 Months Free & No Setup Fees ASP.NET Hosting!
 Hosted by MaximumASP  |  Found a broken link?  |  Contact Us  |  Terms & conditions  |  Privacy Policy  |  Site Map  |  Advertise with us
Current Version: 5.2011.3.12
 © 1999 - 2012  Mindcracker LLC. All Rights Reserved