Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Passing Cursor Coordinates as Parameters

Passing Cursor Coordinates as Parameters

Scheduled Pinned Locked Moved C#
help
7 Posts 4 Posters 2 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    ASPnoob
    wrote on last edited by
    #1

    Hi All, I am trying to create a function that passes the cursor's starting and ending x,y coordinates as parameters. For instance I would like to set the cursor's starting x,y coordinate when the user clicks the left mouse button. Then as the user holds down the left mouse button and drags the cursor to a desired location, the ending x,y coordinate is dynamically set and updated. The final ending coordinate is set when the left button is released. I am currently working with the following code:

    private void Cursor_Coord(out Cursor C1, out Cursor C2)
    {
    // Set the Current cursor, move the cursor's Position,
    Cursor C1;
    Cursor C2;

    if(Mouse.LeftButton == MouseButtonState.Pressed)
    {
    C1 = new Cursor(Cursor.Current.Handle);
    C1.Position = new Point(Cursor.Position.X, Cursor.Position.Y);
    }

    if(Mouse.LeftButton == MouseButtonState.Released)
    {
    C2 = new Cursor(Cursor.Current.Handle);
    C2.Position = new Point(Cursor.Position.X, Cursor.Position.Y);
    }
    }

    I am planning to send the changing cursor coordinates to the Drawline method to be used as the first and second point. The effect I have in mind is that the line is drawn as the mouse is in motion. The problem is it's not working. Any help will be greatly appreciated, thanks in advance.

    P OriginalGriffO 2 Replies Last reply
    0
    • A ASPnoob

      Hi All, I am trying to create a function that passes the cursor's starting and ending x,y coordinates as parameters. For instance I would like to set the cursor's starting x,y coordinate when the user clicks the left mouse button. Then as the user holds down the left mouse button and drags the cursor to a desired location, the ending x,y coordinate is dynamically set and updated. The final ending coordinate is set when the left button is released. I am currently working with the following code:

      private void Cursor_Coord(out Cursor C1, out Cursor C2)
      {
      // Set the Current cursor, move the cursor's Position,
      Cursor C1;
      Cursor C2;

      if(Mouse.LeftButton == MouseButtonState.Pressed)
      {
      C1 = new Cursor(Cursor.Current.Handle);
      C1.Position = new Point(Cursor.Position.X, Cursor.Position.Y);
      }

      if(Mouse.LeftButton == MouseButtonState.Released)
      {
      C2 = new Cursor(Cursor.Current.Handle);
      C2.Position = new Point(Cursor.Position.X, Cursor.Position.Y);
      }
      }

      I am planning to send the changing cursor coordinates to the Drawline method to be used as the first and second point. The effect I have in mind is that the line is drawn as the mouse is in motion. The problem is it's not working. Any help will be greatly appreciated, thanks in advance.

      P Offline
      P Offline
      Pete OHanlon
      wrote on last edited by
      #2

      You don't need to use a cursor to do this. As you've rightly noted in your code, the part you are actually interested in is the Position - that's all you need to keep track of.

      I was brought up to respect my elders. I don't respect many people nowadays.
      CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier

      A 1 Reply Last reply
      0
      • P Pete OHanlon

        You don't need to use a cursor to do this. As you've rightly noted in your code, the part you are actually interested in is the Position - that's all you need to keep track of.

        I was brought up to respect my elders. I don't respect many people nowadays.
        CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier

        A Offline
        A Offline
        ASPnoob
        wrote on last edited by
        #3

        Hi, you've replied to my post as I was in the process of making changes to it.

        1 Reply Last reply
        0
        • A ASPnoob

          Hi All, I am trying to create a function that passes the cursor's starting and ending x,y coordinates as parameters. For instance I would like to set the cursor's starting x,y coordinate when the user clicks the left mouse button. Then as the user holds down the left mouse button and drags the cursor to a desired location, the ending x,y coordinate is dynamically set and updated. The final ending coordinate is set when the left button is released. I am currently working with the following code:

          private void Cursor_Coord(out Cursor C1, out Cursor C2)
          {
          // Set the Current cursor, move the cursor's Position,
          Cursor C1;
          Cursor C2;

          if(Mouse.LeftButton == MouseButtonState.Pressed)
          {
          C1 = new Cursor(Cursor.Current.Handle);
          C1.Position = new Point(Cursor.Position.X, Cursor.Position.Y);
          }

          if(Mouse.LeftButton == MouseButtonState.Released)
          {
          C2 = new Cursor(Cursor.Current.Handle);
          C2.Position = new Point(Cursor.Position.X, Cursor.Position.Y);
          }
          }

          I am planning to send the changing cursor coordinates to the Drawline method to be used as the first and second point. The effect I have in mind is that the line is drawn as the mouse is in motion. The problem is it's not working. Any help will be greatly appreciated, thanks in advance.

          OriginalGriffO Offline
          OriginalGriffO Offline
          OriginalGriff
          wrote on last edited by
          #4

          This is pretty easy to do, but I think you are trying to do it the wrong way. You don't say what your environment is, but I'll assume WinForms. First off, you probably don't need to use Cursor at all for this, a simple Point is enough. Try doing this by adding a few class level variables, and handling a few events:

          private Point pointStart;
          private Point pointEnd;
          private bool drawing = false;

          private void frmMain_MouseDown(object sender, MouseEventArgs e)
          {
          pointStart = e.Location;
          pointEnd = pointStart;
          drawing = true;
          Invalidate();
          }

          private void frmMain_MouseUp(object sender, MouseEventArgs e)
          {
          drawing = false;
          }

          private void frmMain_MouseMove(object sender, MouseEventArgs e)
          {
          if (drawing)
          {
          pointEnd = e.Location;
          Invalidate();
          }
          }

          private void frmMain_Paint(object sender, PaintEventArgs e)
          {
          e.Graphics.DrawLine(Pens.Blue, pointStart, pointEnd);
          }

          The universe is composed of electrons, neutrons, protons and......morons. (ThePhantomUpvoter)

          "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
          "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

          A 1 Reply Last reply
          0
          • OriginalGriffO OriginalGriff

            This is pretty easy to do, but I think you are trying to do it the wrong way. You don't say what your environment is, but I'll assume WinForms. First off, you probably don't need to use Cursor at all for this, a simple Point is enough. Try doing this by adding a few class level variables, and handling a few events:

            private Point pointStart;
            private Point pointEnd;
            private bool drawing = false;

            private void frmMain_MouseDown(object sender, MouseEventArgs e)
            {
            pointStart = e.Location;
            pointEnd = pointStart;
            drawing = true;
            Invalidate();
            }

            private void frmMain_MouseUp(object sender, MouseEventArgs e)
            {
            drawing = false;
            }

            private void frmMain_MouseMove(object sender, MouseEventArgs e)
            {
            if (drawing)
            {
            pointEnd = e.Location;
            Invalidate();
            }
            }

            private void frmMain_Paint(object sender, PaintEventArgs e)
            {
            e.Graphics.DrawLine(Pens.Blue, pointStart, pointEnd);
            }

            The universe is composed of electrons, neutrons, protons and......morons. (ThePhantomUpvoter)

            A Offline
            A Offline
            ASPnoob
            wrote on last edited by
            #5

            Hi, thanks for your reply. I have used your code but VS2010 is complaining that the Invalidate method does not exist in the current context. I assumed that Invalidate is a built-in function of C# but it doesn't appear to be the case.

            L OriginalGriffO 2 Replies Last reply
            0
            • A ASPnoob

              Hi, thanks for your reply. I have used your code but VS2010 is complaining that the Invalidate method does not exist in the current context. I assumed that Invalidate is a built-in function of C# but it doesn't appear to be the case.

              L Offline
              L Offline
              lukeer
              wrote on last edited by
              #6

              Invalidate()[^] is a method of the System.Windows.Forms.Control[^] class. As such, it can be called on all its derivates.

              Ciao, luker

              1 Reply Last reply
              0
              • A ASPnoob

                Hi, thanks for your reply. I have used your code but VS2010 is complaining that the Invalidate method does not exist in the current context. I assumed that Invalidate is a built-in function of C# but it doesn't appear to be the case.

                OriginalGriffO Offline
                OriginalGriffO Offline
                OriginalGriff
                wrote on last edited by
                #7

                At a guess, you have added the word "static" in there somewhere - static methods cannot access non-static class methods, fields or properties.

                The universe is composed of electrons, neutrons, protons and......morons. (ThePhantomUpvoter)

                "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
                "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups