Capture Mouse Coordinates in VS2005

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • =?Utf-8?B?Unlhbg==?=

    Capture Mouse Coordinates in VS2005

    I have a winform containing a scrollable panel and a groupbox inside the
    panel. There is a button inside the groupbox.
    When that button is clicked; how do I capture and display the X and Y
    coordinates in a messagebox?
  • Nicholas Paldino [.NET/C# MVP]

    #2
    Re: Capture Mouse Coordinates in VS2005

    Ryan,

    You can simply look at the static Position class on the Cursor class in
    the System.Windows. Forms namespace. This will give you the position in
    screen coordinates, which you will have to convert to your application
    (which can be done on any control window by calling the PointToClient method
    on the control itself).

    There are some issues with this, as you are polling for the coordinate
    after the mouse button is pressed, in which case, you might not get an exact
    coordinate (as the mouse location can move before your code is called, and
    you are polling for the coordinate).

    If you need the exact coordinate, handle the MouseUp event (as that is
    when the click is registered) and store the coordinates of the mouse from
    the MouseEventArgs that is passed in the event handler. Then, in the Click
    event, you would take those stored coordinates and work with those.


    --
    - Nicholas Paldino [.NET/C# MVP]
    - mvp@spam.guard. caspershouse.co m

    "Ryan" <Ryan@discussio ns.microsoft.co mwrote in message
    news:74922AE0-5756-4D13-B348-957F9A4D7FE4@mi crosoft.com...
    >I have a winform containing a scrollable panel and a groupbox inside the
    panel. There is a button inside the groupbox.
    When that button is clicked; how do I capture and display the X and Y
    coordinates in a messagebox?

    Comment

    • =?Utf-8?B?Unlhbg==?=

      #3
      Re: Capture Mouse Coordinates in VS2005

      Thanks Nicholas.
      Would you, or someone else, be able provide a sample code?

      "Nicholas Paldino [.NET/C# MVP]" wrote:
      Ryan,
      >
      You can simply look at the static Position class on the Cursor class in
      the System.Windows. Forms namespace. This will give you the position in
      screen coordinates, which you will have to convert to your application
      (which can be done on any control window by calling the PointToClient method
      on the control itself).
      >
      There are some issues with this, as you are polling for the coordinate
      after the mouse button is pressed, in which case, you might not get an exact
      coordinate (as the mouse location can move before your code is called, and
      you are polling for the coordinate).
      >
      If you need the exact coordinate, handle the MouseUp event (as that is
      when the click is registered) and store the coordinates of the mouse from
      the MouseEventArgs that is passed in the event handler. Then, in the Click
      event, you would take those stored coordinates and work with those.
      >
      >
      --
      - Nicholas Paldino [.NET/C# MVP]
      - mvp@spam.guard. caspershouse.co m
      >
      "Ryan" <Ryan@discussio ns.microsoft.co mwrote in message
      news:74922AE0-5756-4D13-B348-957F9A4D7FE4@mi crosoft.com...
      I have a winform containing a scrollable panel and a groupbox inside the
      panel. There is a button inside the groupbox.
      When that button is clicked; how do I capture and display the X and Y
      coordinates in a messagebox?
      >
      >
      >

      Comment

      • Stanimir Stoyanov

        #4
        Re: Capture Mouse Coordinates in VS2005

        Hi Ryan,

        What Nicholas meant is this:

        MessageBox.Show (Cursor.Positio n.ToString());

        In case you have or need to obtain the cursor position, relative to one of
        your controls, you call the PointToClient method on it: e.g.

        MessageBox.Show (button1.PointT oClient(Cursor. Position).ToStr ing());

        Best Regards,
        Stanimir Stoyanov | www.stoyanoff.info

        "Ryan" <Ryan@discussio ns.microsoft.co mwrote in message
        news:F7F2D729-6F32-48C5-961F-5F149EEDDE70@mi crosoft.com...
        Thanks Nicholas.
        Would you, or someone else, be able provide a sample code?
        >
        "Nicholas Paldino [.NET/C# MVP]" wrote:
        >
        >Ryan,
        >>
        > You can simply look at the static Position class on the Cursor class
        >in
        >the System.Windows. Forms namespace. This will give you the position in
        >screen coordinates, which you will have to convert to your application
        >(which can be done on any control window by calling the PointToClient
        >method
        >on the control itself).
        >>
        > There are some issues with this, as you are polling for the
        >coordinate
        >after the mouse button is pressed, in which case, you might not get an
        >exact
        >coordinate (as the mouse location can move before your code is called,
        >and
        >you are polling for the coordinate).
        >>
        > If you need the exact coordinate, handle the MouseUp event (as that
        >is
        >when the click is registered) and store the coordinates of the mouse from
        >the MouseEventArgs that is passed in the event handler. Then, in the
        >Click
        >event, you would take those stored coordinates and work with those.
        >>
        >>
        >--
        > - Nicholas Paldino [.NET/C# MVP]
        > - mvp@spam.guard. caspershouse.co m
        >>
        >"Ryan" <Ryan@discussio ns.microsoft.co mwrote in message
        >news:74922AE 0-5756-4D13-B348-957F9A4D7FE4@mi crosoft.com...
        >I have a winform containing a scrollable panel and a groupbox inside the
        panel. There is a button inside the groupbox.
        When that button is clicked; how do I capture and display the X and Y
        coordinates in a messagebox?
        >>
        >>
        >>

        Comment

        • =?Utf-8?B?Unlhbg==?=

          #5
          Re: Capture Mouse Coordinates in VS2005

          Nicholas and Stanimir,
          Thanks to both of you; it worked the way I wanted to.

          "Stanimir Stoyanov" wrote:
          Hi Ryan,
          >
          What Nicholas meant is this:
          >
          MessageBox.Show (Cursor.Positio n.ToString());
          >
          In case you have or need to obtain the cursor position, relative to one of
          your controls, you call the PointToClient method on it: e.g.
          >
          MessageBox.Show (button1.PointT oClient(Cursor. Position).ToStr ing());
          >
          Best Regards,
          Stanimir Stoyanov | www.stoyanoff.info
          >
          "Ryan" <Ryan@discussio ns.microsoft.co mwrote in message
          news:F7F2D729-6F32-48C5-961F-5F149EEDDE70@mi crosoft.com...
          Thanks Nicholas.
          Would you, or someone else, be able provide a sample code?

          "Nicholas Paldino [.NET/C# MVP]" wrote:
          Ryan,
          >
          You can simply look at the static Position class on the Cursor class
          in
          the System.Windows. Forms namespace. This will give you the position in
          screen coordinates, which you will have to convert to your application
          (which can be done on any control window by calling the PointToClient
          method
          on the control itself).
          >
          There are some issues with this, as you are polling for the
          coordinate
          after the mouse button is pressed, in which case, you might not get an
          exact
          coordinate (as the mouse location can move before your code is called,
          and
          you are polling for the coordinate).
          >
          If you need the exact coordinate, handle the MouseUp event (as that
          is
          when the click is registered) and store the coordinates of the mouse from
          the MouseEventArgs that is passed in the event handler. Then, in the
          Click
          event, you would take those stored coordinates and work with those.
          >
          >
          --
          - Nicholas Paldino [.NET/C# MVP]
          - mvp@spam.guard. caspershouse.co m
          >
          "Ryan" <Ryan@discussio ns.microsoft.co mwrote in message
          news:74922AE0-5756-4D13-B348-957F9A4D7FE4@mi crosoft.com...
          I have a winform containing a scrollable panel and a groupbox inside the
          panel. There is a button inside the groupbox.
          When that button is clicked; how do I capture and display the X and Y
          coordinates in a messagebox?
          >
          >
          >
          >
          >

          Comment

          Working...