How Can I Capture Mouse Coordinate on PictureBox?

You don't want to use MouseEnter at all. You want to use MouseDown because you don't want to start tracking the rectangle the user draws until after they click the mouse button So in the MouseDown event handler method, save the current coordinates of the cursor. This is the starting position of the rectangle, point (X, Y) private Point initialMousePos; private void pictureBox1_MouseDown(object sender, MouseEventArgs e) { this.

InitialMousePos = e. Location; } And then, in the MouseUp event which is raised when the user stops dragging and releases the mouse button, you want to save the final ending point of the mouse cursor, and combine that with the initial starting point to build a rectangle. The easiest way to build such a rectangle is using the FromLTRB static method of the Rectangle class : private void pictureBox1_MouseUp(object sender, MouseEventArgs e) { // Save the final position of the mouse Point finalMousePos = e.

Location; // Create the rectangle from the two points Rectangle drawnRect = Rectangle. FromLTRB( this.initialMousePos. X, this.initialMousePos.

Y, finalMousePos. X, finalMousePos. Y); // Do whatever you want with the rectangle here // ... } You probably want to combine this with some painting code in the MouseMove event handler method to give the user a visual indication of the rectangle that they're drawing while they're drawing it The right way to do this is to handle the MouseMove event to get the current position of the mouse, and then call the Invalidate method, which will raise the Paint event All of your painting code should be in the Paint event handler.

This is a better approach than the one taken by a lot of samples you find on the web, where you'll see something like CreateGraphics called inside of the MouseMove event handler method. Avoid that, if possible Sample implementation: private Point currentMousePos; private void pictureBox1_MouseMove(object sender, MouseEventArgs e) { // Save the current position of the mouse currentMousePos = e. Location; // Force the picture box to be repainted pictureBox1.Invalidate(); } private void pictureBox1_Paint(object sender, PaintEventArgs e) { // Create a pen object that we'll use to draw // (change these parameters to make it any color and size you want) using (Pen p = new Pen(Color.

Blue, 3.0F)) { // Create a rectangle with the initial cursor location as the upper-left // point, and the current cursor location as the bottom-right point Rectangle currentRect = Rectangle. FromLTRB( this.initialMousePos. X, this.initialMousePos.

Y, currentMousePos. X, currentMousePos. Y); // Draw the rectangle e.Graphics.

DrawRectangle(p, currentRect); } } A couple of things to note about this code if you've never done any type of drawing before. The first thing is that I've wrapped the creation of a Pen object (which we use to do the actual drawing) in a using statement This is good general practice any time you create an object that implements the IDisposable interface such as brushes and pens, to prevent a memory leak in your application Also, the PaintEventArgs provide us with an instance of the Graphics class which encapsulates all of the basic drawing functionality in a . NET application.

All you have to do is call methods like DrawRectangle or DrawImage on that class instance, passing in the appropriate objects as parameters, and all the drawing gets done for you automatically. Simple enough, right? And finally, notice that we've done exactly the same thing here to create a rectangle as we ultimately do in the MouseUp event handler method.

That makes sense, because we just want the instantaneous size of the rectangle while the user is creating it.

You don't want to use MouseEnter at all. You want to use MouseDown, because you don't want to start tracking the rectangle the user draws until after they click the mouse button. So in the MouseDown event handler method, save the current coordinates of the cursor.

This is the starting position of the rectangle, point (X, Y). Private Point initialMousePos; private void pictureBox1_MouseDown(object sender, MouseEventArgs e) { this. InitialMousePos = e.

Location; } And then, in the MouseUp event, which is raised when the user stops dragging and releases the mouse button, you want to save the final ending point of the mouse cursor, and combine that with the initial starting point to build a rectangle. The easiest way to build such a rectangle is using the FromLTRB static method of the Rectangle class: private void pictureBox1_MouseUp(object sender, MouseEventArgs e) { // Save the final position of the mouse Point finalMousePos = e. Location; // Create the rectangle from the two points Rectangle drawnRect = Rectangle.

FromLTRB( this.initialMousePos. X, this.initialMousePos. Y, finalMousePos.

X, finalMousePos. Y); // Do whatever you want with the rectangle here // ... } You probably want to combine this with some painting code in the MouseMove event handler method to give the user a visual indication of the rectangle that they're drawing while they're drawing it. The right way to do this is to handle the MouseMove event to get the current position of the mouse, and then call the Invalidate method, which will raise the Paint event.

All of your painting code should be in the Paint event handler. This is a better approach than the one taken by a lot of samples you find on the web, where you'll see something like CreateGraphics called inside of the MouseMove event handler method. Avoid that, if possible.

Sample implementation: private Point currentMousePos; private void pictureBox1_MouseMove(object sender, MouseEventArgs e) { // Save the current position of the mouse currentMousePos = e. Location; // Force the picture box to be repainted pictureBox1.Invalidate(); } private void pictureBox1_Paint(object sender, PaintEventArgs e) { // Create a pen object that we'll use to draw // (change these parameters to make it any color and size you want) using (Pen p = new Pen(Color. Blue, 3.0F)) { // Create a rectangle with the initial cursor location as the upper-left // point, and the current cursor location as the bottom-right point Rectangle currentRect = Rectangle.

FromLTRB( this.initialMousePos. X, this.initialMousePos. Y, currentMousePos.

X, currentMousePos. Y); // Draw the rectangle e.Graphics. DrawRectangle(p, currentRect); } } A couple of things to note about this code if you've never done any type of drawing before.

The first thing is that I've wrapped the creation of a Pen object (which we use to do the actual drawing) in a using statement. This is good general practice any time you create an object that implements the IDisposable interface, such as brushes and pens, to prevent a memory leak in your application. Also, the PaintEventArgs provide us with an instance of the Graphics class, which encapsulates all of the basic drawing functionality in a .

NET application. All you have to do is call methods like DrawRectangle or DrawImage on that class instance, passing in the appropriate objects as parameters, and all the drawing gets done for you automatically. Simple enough, right?

And finally, notice that we've done exactly the same thing here to create a rectangle as we ultimately do in the MouseUp event handler method. That makes sense, because we just want the instantaneous size of the rectangle while the user is creating it.

– Kelly May 22 at 10:46 @Kelly: Updated with a painting sample. Not sure how familiar you are with this type of thing in the . NET world, so I tried to explain it as well as possible.

Please leave a comment if you have any more questions! – Cody Gray May 22 at 10:58.

You don't want to use MouseEnter at all. You want to use MouseDown, because you don't want to start tracking the rectangle the user draws until after they click the mouse button. So in the MouseDown event handler method, save the current coordinates of the cursor.

This is the starting position of the rectangle, point (X, Y). And then, in the MouseUp event, which is raised when the user stops dragging and releases the mouse button, you want to save the final ending point of the mouse cursor, and combine that with the initial starting point to build a rectangle. You probably want to combine this with some painting code in the MouseMove event handler method to give the user a visual indication of the rectangle that they're drawing while they're drawing it.

The right way to do this is to handle the MouseMove event to get the current position of the mouse, and then call the Invalidate method, which will raise the Paint event. All of your painting code should be in the Paint event handler. This is a better approach than the one taken by a lot of samples you find on the web, where you'll see something like CreateGraphics called inside of the MouseMove event handler method.

Avoid that, if possible. A couple of things to note about this code if you've never done any type of drawing before.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions