Passing Cursor Coordinates as Parameters
-
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.
-
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.
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 -
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 -
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.
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)
-
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)
-
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.
Invalidate()[^] is a method of the System.Windows.Forms.Control[^] class. As such, it can be called on all its derivates.
Ciao, luker
-
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.
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)