MousePosition relative to Form/Component?
MousePosition relative to Form/Component?
MousePosition relative to Form/Component?
MousePosition relative to Form/Component?
MousePosition relative to Form/Component?
MousePosition relative to Form/Component? MousePosition relative to Form/Component? MousePosition relative to Form/Component? MousePosition relative to Form/Component? MousePosition relative to Form/Component? MousePosition relative to Form/Component? MousePosition relative to Form/Component? MousePosition relative to Form/Component?
MousePosition relative to Form/Component? MousePosition relative to Form/Component?
MousePosition relative to Form/Component?
Go Back  Xtreme Visual Basic Talk > > > MousePosition relative to Form/Component?


Reply
 
Thread Tools Display Modes
  #1  
Old 01-16-2011, 01:42 PM
WhatsMyUsername's Avatar
WhatsMyUsername WhatsMyUsername is offline
Regular
 
Join Date: Jan 2011
Location: Currently? Costa Rica
Posts: 52
Default MousePosition relative to Form/Component?


So I have decided to learn more about this Invalidate() thing. I decided to simply draw a rectangle where the mouse clicks in the form, for testing.

Code:
Public Class Form1
    Dim p As Point

    Private Sub Form1_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
        Dim g As Graphics = e.Graphics
        g.FillRectangle(Brushes.Orange, New Rectangle(p.X, p.Y, 10, 10))
    End Sub

    Private Sub Form1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Click
        p.X = Form1.MousePosition.X
        p.Y = Form1.MousePosition.Y
        Me.Invalidate()
    End Sub
End Class
But there's a problem: The.. rectangle is draw much farther from where I clicked. What is interesting, is that it is draw much closer if I have my form maximized. Maybe I am not getting the correct Mouse coordinates. Is there another way, then?
Reply With Quote
  #2  
Old 01-16-2011, 02:25 PM
AtmaWeapon's Avatar
AtmaWeaponMousePosition relative to Form/Component? AtmaWeapon is offline
Fabulous Florist

Forum Leader
* Guru *
 
Join Date: Feb 2004
Location: Austin, TX
Posts: 9,500
Default

Control.MousePosition says it gets the cursor's location in screen coordinates. If you have a look at the example in the documentation, you'll get a hint about how to get the coordinates in terms of the control's coordinate space, the PointToClient() method. When something doesn't seem to do what you think it should, it's always a good idea to double-check the documentation and make sure your expectations are correct. Here's a demonstration that works more like you'd expect:
Code:
Public Class Form1
    Dim p As Point

    Private Sub Form1_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
        Dim g As Graphics = e.Graphics
        g.FillRectangle(Brushes.Orange, New Rectangle(p.X, p.Y, 10, 10))
    End Sub

    Private Sub Form1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Click
        Dim screenPoint As Point = Cursor.Position
        Dim clientPoint As Point = Me.PointToClient(screenPoint)
        p = clientPoint
        Me.Invalidate()
    End Sub
End Class
Cursor.Position is similar to MousePosition but less confusing since it doesn't make you think about a particular control when you use it.
__________________
.NET Resources
My FAQ threads | Tutor's Corner | Code Library
I would bet money 2/3 of .NET questions are already answered in one of these three places.
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
MousePosition relative to Form/Component?
MousePosition relative to Form/Component?
MousePosition relative to Form/Component? MousePosition relative to Form/Component?
MousePosition relative to Form/Component?
MousePosition relative to Form/Component?
MousePosition relative to Form/Component? MousePosition relative to Form/Component? MousePosition relative to Form/Component? MousePosition relative to Form/Component? MousePosition relative to Form/Component? MousePosition relative to Form/Component? MousePosition relative to Form/Component?
MousePosition relative to Form/Component?
MousePosition relative to Form/Component?
 
MousePosition relative to Form/Component?
MousePosition relative to Form/Component?
 
-->