?
Solved

Need a quick prog to show mouse location

Posted on 1999-12-15
10
Medium Priority
?
178 Views
Last Modified: 2010-05-02
Please..a quick program to show the current location (x,y) of the mouse on the screen ( without clicking or pressing a command button) just as it roves around.
0
Comment
Question by:rbend
  • 4
  • 2
  • 2
  • +2
10 Comments
 
LVL 22

Expert Comment

by:ture
ID: 2283848
rbend,

Create a form with noyhing but two labels on it. Then add this code...

Private Sub Form_Activate()
  Me.WindowState = vbMaximized
End Sub

Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
  Label1.Caption = X
  Label2.Caption = Y
End Sub

Ture Magnusson
Karlstad, Sweden
0
 
LVL 13

Expert Comment

by:crazyman
ID: 2283878
this one will get the mouse position in relation to the screen not just when moved on the form

Option Explicit
Public Type POINTAPI
        X As Long
        y As Long
End Type
Public Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long


Private Sub Timer1_Timer()
'(This occurs like 1000 times a second)
'This dims the variable where the current cursor pos is held
Dim mousexy As POINTAPI
dim q as long
'This gets the current cursor pos
q = GetCursorPos(mousexy)

'This sets text1.text to the x value of the cursor position
Text1.Text = mousexy.X

'This sets text2.text to the y value of the cursor position
Text2.Text = mousexy.y

End Sub
0
 
LVL 22

Expert Comment

by:ture
ID: 2283933
rbend,

This modification allows the form to occupy the entire screen, except for the taskbar if it is visible.

Private Sub Form_Activate()
  Me.BorderStyle = vbBSNone
  Me.WindowState = vbMaximized
End Sub

Private Sub Form_Click()
  Unload Me
End Sub

Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
  Label1.Caption = X
  Label2.Caption = Y
End Sub

/Ture
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 

Expert Comment

by:richardwads
ID: 2283954
Quick fix for 50 points

open a standard VB project

Put two command buttons, four labels and a timer control on to the form.  Leave the default names.  Paste the following code into the code module for form1

Option Explicit

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


Private Type POINTAPI
        x As Long
        y As Long
End Type

Private Sub Command1_Click()
    Timer1.Enabled = True
    Command1.Enabled = False
    Command2.Enabled = True
End Sub

Private Sub Command2_Click()
    Timer1.Enabled = False
    Command2.Enabled = False
    Command1.Enabled = True
End Sub

Private Sub Form_Load()

    Timer1.Interval = 50
    Timer1.Enabled = False
    Command1.Top = 240
    Command2.Top = 240
    Command1.Left = 120
    Command2.Left = 1440
   
    Command1.Caption = "Start"
    Command2.Caption = "Stop"
   
    Label1.Top = 1080
    Label2.Top = 1080
   
    Label3.Top = 1320
    Label4.Top = 1320
   
    Label1.Left = 360
    Label3.Left = 360
   
    Label2.Left = 1080
    Label4.Left = 1080

    Label1.Caption = "Y"
    Label3.Caption = "Y"
   
    Command2.Enabled = False
   
End Sub

Private Sub Timer1_Timer()
    Dim pt As POINTAPI
    Dim lngRetVal As Long
   
    lngRetVal = GetCursorPos(pt)
   
    Label2.Caption = pt.x
    Label4.Caption = pt.y
End Sub
0
 
LVL 13

Expert Comment

by:crazyman
ID: 2284004
call me paranoid but isnt richardwads the same as mine but just with poitioning the labels and some captions ...???
0
 

Expert Comment

by:richardwads
ID: 2284035
Easy crazyman.  

I thought the same thing after submitting my answer.

We obviously think alike.  
0
 
LVL 13

Expert Comment

by:crazyman
ID: 2284041
hehe sorry no offence meant
0
 
LVL 32

Expert Comment

by:Erick37
ID: 2284104
How about this:
Display current cursor position (screen coords) in your app, even if the mouse moves off the application.  Works everywhere except the taskbar.  No timer needed.

Option Explicit
Private Type POINTAPI
    x As Long
    y As Long
End Type
Private Declare Function ClientToScreen Lib "user32" _
    (ByVal hwnd As Long, lpPoint As POINTAPI) As Long
Private Declare Function SetCapture Lib "user32" _
    (ByVal hwnd As Long) As Long
Private Declare Function ReleaseCapture Lib "user32" () As Long

Private pt As POINTAPI

Private Sub Command1_Click()
    'Capture the mouse moves
    Call SetCapture(hwnd)
End Sub

Private Sub Command2_Click()
    'Release mouse capture
    Call ReleaseCapture
End Sub

Private Sub Form_MouseMove(Button As Integer, Shift As Integer, _
        x As Single, y As Single)
    'Assuming twips scalemode
    pt.x = x / Screen.TwipsPerPixelX
    pt.y = y / Screen.TwipsPerPixelY
    Call ClientToScreen(hwnd, pt)
    Caption = "Screen  X: " & pt.x & " Y: " & pt.y
End Sub
0
 

Author Comment

by:rbend
ID: 2284923
Crazyman actually had what I wanted exactly.
I do thank the rest of you for helping.
You are all brilliant.
Crazyman please answer this.
0
 
LVL 13

Accepted Solution

by:
crazyman earned 200 total points
ID: 2285194
posting for points
0

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

Question has a verified solution.

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

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Suggested Courses

601 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