How do I move the Image inside the picturebox? - c#

I want an Image to move inside the picturebox. It shouldn't be possible that you can drag it out. I found an answer with the Padding and tried it out but it drags in the opposite direction. So I tried out to switch it with Right and down, but it is not getting dragged. Also I found an answer where the picturebox get moved but then it can be moved out of the form and isn't there anymore. So I need something that can just move the picture inside the picturebox or something that moves the picturebox but not out of the form.
private bool Dragging;
private Point lastLocation;
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
Dragging = true;
lastLocation = e.Location;
}
}
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
if (Dragging == true)
{
int dx = e.X - lastLocation.X;
int dy = e.Y - lastLocation.Y;
pictureBox1.Padding = new Padding(0, 0, Padding.Right - dx, Padding.Bottom - dy);
pictureBox1.Invalidate();
}
}
private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
Dragging = false;
}

Do this
pictureBox1.Padding = new Padding(Padding.Left + dx, Padding.Top + dy, Padding.Right - dx, Padding.Bottom - dy);
instead of this
pictureBox1.Padding = new Padding(0, 0, Padding.Right - dx, Padding.Bottom - dy);

I have done it by creating a panel and inserted image box inside of it.It's working in my side.Please find the code blow .
private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Left)
{
int moveLeftRight = e.X + pictureBox1.Left - MouseDownLocation.X;
int moveUpDown = e.Y + pictureBox1.Top - MouseDownLocation.Y;
int panlTopLocation = panel1.Location.Y;
int panlbottomLocation = panel1.Location.Y + panel1.Height - pictureBox1.Height;
int panlLeftLocation = panel1.Location.X;
int panlRightLocation = panel1.Location.X + panel1.Width - pictureBox1.Width ;
if (panlLeftLocation < moveLeftRight)
{
if (panlRightLocation > moveLeftRight)
{
pictureBox1.Left = moveLeftRight;
}
else
{
pictureBox1.Left = panlRightLocation;
}
}
else
{
pictureBox1.Left = panlLeftLocation;
}
if (panlTopLocation < moveUpDown)
{
if (panlbottomLocation > moveUpDown)
{
pictureBox1.Top = moveUpDown;
}
else
{
pictureBox1.Top = panlbottomLocation;
}
}
else
{
pictureBox1.Top = panlTopLocation;
}
}
}

Related

How to draw a Rectangle in any direction after MouseDown using C#?

How can I draw a rectangle in all directions? Current code:
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
// Starting point of the selection:
if (e.Button == MouseButtons.Left)
{
_selecting = true;
_selection = new Rectangle(new Point(e.X, e.Y), new Size());
}
}
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
// Update the actual size of the selection:
if (_selecting)
{
_selection.Width = e.X - _selection.X;
_selection.Height = e.Y - _selection.Y;
pictureBox1.Refresh(); // redraw picturebox
}
}
private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left && _selecting)
{
_selecting = false;
}
}
This allows me to MouseDown and then draw down and to the right, but I am unable to draw in any other direction beyond the anchor point. How can I draw a rectangle in any direction like e.g. Microsoft Paint?
I first tried:
_selection.Width = Math.Abs(e.X - _selection.X);
_selection.Height = Math.Abs(e.Y - _selection.Y);
But this creates a funny mirror effect (which is not what I want). I then tried a simple shift:
_selection.X = _selection.Left - 5;
This did what I expected and moved a static rectangle 5 units left, so I thought it would be a simple matter of continuously shifting the anchor point during the Paint event:
private void UpdateRectange(Point newPos)
{
var width = newPos.X - _selection.X;
var height = newPos.Y - _selection.Y;
_selection.Width = Math.Abs(width);
_selection.Height = Math.Abs(height);
if (width < 0 && height > 0) // move down (+) and left (-)
{
//_selection.X = _selection.Left + width;
_selection.Offset(width, 0);
}
uxScreenGrab.Refresh(); // redraw picturebox
}
But this resulted in pushing a vertical line across the screen towards the left, when moving to the left of the original anchor point.The width does not properly update for some reason.
Here's another snippet to draw a selection rectangle on a PictureBox:
//...
private Point startPoint;
private Point endPoint;
private readonly Pen P1 = new Pen(Color.SteelBlue, 2) {
Alignment = PenAlignment.Center, DashStyle = DashStyle.Dash};
//...
Set the startPoint in the MouseDown event and reset the endPoint variables:
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
startPoint = new Point(e.X, e.Y);
endPoint = Point.Empty;
}
}
Set the endPoint and call Invalidate() method in the MouseMove event:
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
var p = new Point(e.X, e.Y);
if (e.Button == MouseButtons.Left &&
!p.Equals(startPoint))
{
endPoint = p;
pictureBox1.Invalidate();
}
}
Also call Invalidate() in the MouseUp event to remove the selection rectangle:
private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
pictureBox1.Invalidate();
}
Draw the selection rectangle:
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
if(MouseButtons == MouseButtons.Left &&
!startPoint.Equals(endPoint) &&
!endPoint.Equals(Point.Empty))
{
var g = e.Graphics;
var rect = Rectangle.Empty;
if(startPoint.X < endPoint.X)
{
rect.X = startPoint.X;
rect.Width = endPoint.X - startPoint.X;
}
else
{
rect.X = endPoint.X;
rect.Width = startPoint.X - endPoint.X;
}
if(startPoint.Y < endPoint.Y)
{
rect.Y = startPoint.Y;
rect.Height = endPoint.Y - startPoint.Y;
}
else
{
rect.Y = endPoint.Y;
rect.Height = startPoint.Y - endPoint.Y;
}
g.DrawRectangle(P1, rect);
}
}
And don't forget to clean up:
private void YourForm_FormClosing(object sender, FormClosingEventArgs e)
{
P1.Dispose();
}
Keep it simple.
Related Posts
How to call a method that uses PaintEventArgs and coordinates variables
The solution proposed in the question looks good:
_selection.Width = Math.Abs(e.X - initiallySelectedX);
_selection.Height = Math.Abs(e.Y - initiallySelectedY);
...but You have to move the origin of the rectangle when (e.X - initiallySelectedX) < 0
So probably You want to add something like this to Your code:
var diffX = e.x - initiallySelectedX;
if (diffX < 0) _selection.X = initiallySelectedX - diffX;
var diffY = e.y - initiallySelectedY;
if (diffY < 0) _selection.Y = initiallySelectedY - diffY;
Where initiallySelectedX and initiallySelectedY are variables set onMouseDown.
this is only a rough idea.
The idea behind is that the Width and Height of the Rectangle cannot be NEGATIVE !!
UPDATE:
Keeping track of the starting point on the MouseDown event allows the anchor point to correctly update:
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
// Starting point of the selection:
if (e.Button == MouseButtons.Left)
{
_selecting = true;
_selection = new Rectangle(new Point(e.X, e.Y), new Size());
_startingPoint = e.Location;
}
}
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
// Update the actual size of the selection:
if (_selecting)
{
UpdateRectange(e.Location);
}
}
private void UpdateRectange(Point newPos)
{
var diffX = newPos.X - _startingPoint.X;
var diffY = newPos.Y - _startingPoint.Y;
var newSize = new Size(Math.Abs(diffX), Math.Abs(diffY));
if (diffX > 0 && diffY > 0)
{
_selection = new Rectangle(_startingPoint, newSize);
}
else if (diffX < 0 && diffY < 0)
{
_selection = new Rectangle(newPos, newSize);
}
else if (diffX > 0 && diffY < 0)
{
_selection = new Rectangle(new Point(_startingPoint.X, _startingPoint.Y + diffY), newSize);
}
else
{
_selection = new Rectangle(new Point(_startingPoint.X + diffX, _startingPoint.Y), newSize);
}
uxScreenGrab.Invalidate();
}
UPDATE 2:
Re-wrote UpdateRectangle to simply move the anchor point, instead of instantiating at every call:
private void UpdateRectange(Point newPos)
{
var diffX = newPos.X - _startingPoint.X;
var diffY = newPos.Y - _startingPoint.Y;
var newSize = new Size(Math.Abs(diffX), Math.Abs(diffY));
if (diffX > 0 && diffY > 0)
{
_selection.Size = newSize;
}
else if (diffX < 0 && diffY < 0)
{
_selection.Location = newPos;
_selection.Size = newSize;
}
else if (diffX > 0 && diffY < 0)
{
_selection.Y = _startingPoint.Y + diffY;
_selection.Size = newSize;
}
else
{
_selection.X = _startingPoint.X + diffX;
_selection.Size = newSize;
}
uxScreenGrab.Invalidate();
}

C# Why is the Form Location Reseting to (0, 0)

Whenever I move the form in its maximum state and move the form, the form location will default (0, 0). I use a MouseDown, MouseMove, and MouseUp event.
private void TopPanel_MouseDown(object sender, MouseEventArgs e)
{
AdjustingTheForm = true;
}
private void TopPanel_MouseMove(object sender, MouseEventArgs e)
{
if (AdjustingTheForm)
{
scr = Screen.FromControl(this).WorkingArea;
LastLocation = e.Location;
if (FormNormalSize == false)
{
FormNormalSize = true;
CustomForm_Resize(sender, e);
this.Location = new Point(e.X - 400, e.Y - 15);
this.Update();
}
AdjustingTheForm = false;
MovingTheForm = true;
Console.WriteLine("1. " + this.Location);
}
if(MovingTheForm)
{
this.Location = new Point((this.Location.X - LastLocation.X) + e.X, (this.Location.Y - LastLocation.Y) + e.Y);
this.Update();
Console.WriteLine("2. " + this.Location + " " + e.Location);
}
}
private void TopPanel_MouseUp(object sender, MouseEventArgs e)
{
scr = Screen.FromControl(this).WorkingArea;
MovingTheForm = false;
Here is where I put the private instance members:
namespace CustomForm_Practice_1
{
public partial class CustomForm : Form
{
bool minimizeB1MouseDown, maximizeB1MouseDown, exitB1MouseDown;
bool FormNormalSize;
bool AdjustingTheForm, MovingTheForm;
Point LastLocation;
Rectangle scr;
.......
Here are the results when I move the form (UPDATED):
1. this.Location: {X=1100,Y=6}
2. LastLocation: {X=1100,Y=6}
3. e.Location: {X=1100,Y=6}
1. this.Location: {X=0,Y=0}
2. LastLocation: {X=1100,Y=6}
3. e.Location: {X=0,Y=0}
1. this.Location: {X=0,Y=0}
2. LastLocation: {X=1100,Y=6}
3. e.Location: {X=1100,Y=6}
1. this.Location: {X=0,Y=2}
2. LastLocation: {X=1100,Y=6}
3. e.Location: {X=1100,Y=8}
OLD I don't know why x jumps from 703 to 0 and y from 8 to 0. This problem, however, only occurs when the size of the form changes and the form is moved. When the form is at normal size (800, 600). Here is the form resize event:
New This time this.Location started at X = 1100 and Y = 6 and then it went to (0, 0). e.Location did the same thing. Here is the form resize event:
private void CustomForm_Resize(object sender, EventArgs e)
{
if (FormNormalSize == false)
//Maximized Window
{
scr = Screen.FromControl(this).WorkingArea;
this.Location = new Point(scr.X, scr.Y);
this.Size = new Size(scr.Width, scr.Height);
this.Update();
//Panel Heights
TopPanel.Height = 30;
BottomPanel.Height = scr.Height - 32;
//Panel Widths
TopPanel.Width = scr.Width - 2;
BottomPanel.Width = scr.Width - 2;
}
else if (FormNormalSize)
//Normal Window
{
this.Size = new Size(800, 600);
//Panel Heights
TopPanel.Height = 30;
BottomPanel.Height = this.Height - 32;
//Panel Widths
TopPanel.Width = this.Width - 2;
BottomPanel.Width = this.Width - 2;
}
//Panel Locations
TopPanel.Location = new Point(1, 1);
BottomPanel.Location = new Point(1, TopPanel.Height + 1);
The question is, why does the form location go to (0, 0) when this line
this.Location = new Point(e.X - 400, e.Y - 15); changes the location that was previously set this.Location = new Point(scr.X, scr.Y);?
Moving/resizing a form like this can be somewhat fiddly, because the mouse location in MouseEventArgs is given relative to the top left of the form rather than in screen coords.
A better way to track mouse coords when you need screen coords is to use the MousePosition class along with mouse capture via Control.Capture = true.
The easiest way for me to demonstrate this is through a sample app:
Create a default Windows Forms app. I'm going to assume that the main form is called Form1.
Drop onto the main form a Panel called panel1 and set its Dock
property to Fill.
Add to panel1 the following handlers: panel1_MouseDown, panel1_MouseMove and panel1_MouseUp.
Change the code in Form1.cs as follows:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
bool moving;
Point offset;
Point original;
void panel1_MouseDown(object sender, MouseEventArgs e)
{
moving = true;
panel1.Capture = true;
offset = MousePosition;
original = this.Location;
}
void panel1_MouseMove(object sender, MouseEventArgs e)
{
if (!moving)
return;
int x = original.X + MousePosition.X - offset.X;
int y = original.Y + MousePosition.Y - offset.Y;
this.Location = new Point(x, y);
}
void panel1_MouseUp(object sender, MouseEventArgs e)
{
moving = false;
panel1.Capture = false;
}
}
Compile and run the application then click and drag on the main window to move it around. It should follow the mouse as you move it around.
Once you have that working, you should be able to apply the same logic to your application.
I figured it out. PointToClient help me get the LastLocation value. Here is the new code:
private void TopPanel_MouseDown(object sender, MouseEventArgs e)
{
if(e.Button == MouseButtons.Left)
{
AdjustingTheForm = true;
}
else if (e.Button == MouseButtons.Right)
{
AdjustingTheForm = false;
}
LastLocation = PointToClient(e.Location);
}
private void TopPanel_MouseMove(object sender, MouseEventArgs e)
{
scr = Screen.FromControl(this).WorkingArea;
if (AdjustingTheForm)
{
if (FormNormalSize == false)
{
FormNormalSize = true;
CustomForm_Resize(sender, e);
this.Location = new Point(Cursor.Position.X - 400, Cursor.Position.Y - 15);
this.Update();
LastLocation = PointToClient(e.Location);
MovingFormMaximized = true;
}
else
{
MovingFormMinimized = true;
}
AdjustingTheForm = false;
}
if ((MovingFormMinimized && this.Location.Y > 0) || (MovingFormMinimized && this.Location.Y <= 0 && e.Y > 32))
{
mouseHuggingWall = false;
this.Location = new Point(this.Location.X - LastLocation.X + e.X,
this.Location.Y - LastLocation.Y + e.Y);
this.Update();
}
else if (MovingFormMinimized && this.Location.Y <= 0)
{
mouseHuggingWall = true;
this.Location = new Point(this.Location.X - LastLocation.X + e.X, 0);
this.Update();
}
if ((MovingFormMaximized && this.Location.Y > 0) || (MovingFormMaximized && this.Location.Y <= 0 && e.Y > 32))
{
mouseHuggingWall = false;
this.Location = new Point(this.Location.X -LastLocation.X + e.X,
this.Location.Y - LastLocation.Y + e.Y);
this.Update();
}
else if (MovingFormMaximized && this.Location.Y <= 0)
{
mouseHuggingWall = true;
this.Location = new Point(this.Location.X - LastLocation.X + e.X, 0);
this.Update();
}
}
private void TopPanel_MouseUp(object sender, MouseEventArgs e)
{
scr = Screen.FromControl(this).WorkingArea;
if(mouseHuggingWall == true)
{
FormNormalSize = false;
CustomForm_Resize(sender, e);
}
MovingFormMinimized = false;
MovingFormMaximized = false;
mouseHuggingWall = false;
}
I added more to the code but the main fix is in the code.

Stop dragging picturebox c #

I have a picturebox and movement just right and to the left, but when moving it to the left or right edge of the picturebox appears in the middle of the form , as the picture below
This thus
what I want is when moving left or right when the board of picturebox were in the same position of the board form the image can not be more moves , just to the other lado.Conforme the image below
Follows the script:
private void imagemPictureBox_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button==MouseButtons.Left)
{
x = e.X;
//y = e.Y;
}
}
private void imagemPictureBox_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
imagemPictureBox.Left += (e.X - x);
//imagemPictureBox.Top += (e.Y - y);
}
}
Try something like this:
// the point in the PB where we grab it:
Point mDown;
void imagemPictureBox_MouseDown(object sender, MouseEventArgs e)
{
// grab the pb
mDown = e.Location;
}
void imagemPictureBox_MouseMove(object sender, MouseEventArgs e)
{
// new position:
int x = imagemPictureBox.Left + e.X - mDown.X;
int y = imagemPictureBox.Top + e.Y - mDown.Y;
// limit to form size:
x = Math.Min(Math.Max(x, 0), this.ClientSize.Width - imagemPictureBox.Width);
y = Math.Min(Math.Max(y, 0), this.ClientSize.Height - imagemPictureBox.Height);
// move the pb:
imagemPictureBox.Location = new Point(x, y);
}
void imagemPictureBox_MouseUp(object sender, MouseEventArgs e)
{
//release it (optional)
mDown = Point.Empty;
}

How to draw a rectangle by holding down mouse button in a PictureBox inside a windows form?

In my application I want to draw a rectangle on a picture box by holding down a mouse button, the 2 points defining the rectangle must be retrieved from the moment the mousbutton was pressed down and when it was released. The problem is, the coordinate points of the rectangle are with respect to windows form but I need coordinate points with respect to picture box that means the picture box should use separate coordinate points and here is my code snippets...
public partial class Form1 : Form
{
Boolean bHaveMouse;
Point ptOriginal = new Point();
Point ptLast = new Point();
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
bHaveMouse = false;
}
private void MyDrawReversibleRectangle(Point p1, Point p2)
{
Rectangle rc = new Rectangle();
p1 = PointToScreen(p1);
p2 = PointToScreen(p2);
if (p1.X < p2.X)
{
rc.X = p1.X;
rc.Width = p2.X - p1.X;
}
else
{
rc.X = p2.X;
rc.Width = p1.X - p2.X;
}
if (p1.Y < p2.Y)
{
rc.Y = p1.Y;
rc.Height = p2.Y - p1.Y;
}
else
{
rc.Y = p2.Y;
rc.Height = p1.Y - p2.Y;
}
ControlPaint.DrawReversibleFrame(rc,
Color.DarkGreen, FrameStyle.Thick);
textBox2.Text = (rc.Width).ToString();
textBox3.Text = (rc.Height).ToString();
}
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
Point ptCurrent = new Point(e.X, e.Y);
if (bHaveMouse)
{
if (ptLast.X != -1)
{
MyDrawReversibleRectangle(ptOriginal, ptLast);
}
ptLast = ptCurrent;
MyDrawReversibleRectangle(ptOriginal, ptCurrent);
textBox1.Text = (ptOriginal).ToString();
textBox4.Text = (ptLast).ToString();
}
}
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
bHaveMouse = true;
ptOriginal.X = e.X;
ptOriginal.Y = e.Y;
ptLast.X = -1;
ptLast.Y = -1;
}
private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
bHaveMouse = false;
if (ptLast.X != -1)
{
Point ptCurrent = new Point(e.X, e.Y);
MyDrawReversibleRectangle(ptOriginal, ptLast);
}
ptLast.X = -1;
ptLast.Y = -1;
ptOriginal.X = -1;
ptOriginal.Y = -1;
}
I think you are overcomplicating things.
Instead of using ControlPaint you can simply get a Graphics-Object "on" your picture box, that way, inside that graphics object you have to use coordinates relative to itself and not to the control containing it, a little example:
public Form1()
{
InitializeComponent();
Bitmap myBitmap = new Bitmap(this.pictureBox1.Width, this.pictureBox1.Height);
using (Graphics g = Graphics.FromImage(myBitmap))
{
g.Clear(Color.Aqua);
g.DrawRectangle(new Pen(Brushes.Black), 10, 10, 100, 100);
}
this.pictureBox1.Image = myBitmap;
}
Output of that looks like this for me (form was: 284x262 here and the picture box: 156x124 and positioned at (47, 35) relative to the form origin)
Using most parts of your code you can use the coordinates relative to the pictue box pretty easily (I did not use the output of coordinates into a textbox and replaced your MyDrawReversibleRectangle with a simple one of mine, now you can just span a rectangle by holding down a mousebutton inside the picture box:
public partial class Form1 : Form
{
Boolean bHaveMouse;
Point ptOriginal = new Point();
Point ptLast = new Point();
public Form1()
{
InitializeComponent();
this.pictureBox1.MouseMove += new System.Windows.Forms.MouseEventHandler(this.pictureBox1_MouseMove);
this.pictureBox1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.pictureBox1_MouseDown);
this.pictureBox1.MouseUp += new System.Windows.Forms.MouseEventHandler(this.pictureBox1_MouseUp);
}
private void MyDrawReversibleRectangle(Point p1, Point p2)
{
Bitmap myBitmap = new Bitmap(this.pictureBox1.Width, this.pictureBox1.Height);
using (Graphics g = Graphics.FromImage(myBitmap))
{
g.Clear(Color.Aqua);
g.DrawRectangle(new Pen(Brushes.Black), p1.X, p1.Y, p2.X - p1.X, p2.Y - p1.Y);
}
this.pictureBox1.Image = myBitmap;
}
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
Point ptCurrent = new Point(e.X, e.Y);
if (bHaveMouse)
{
if (ptLast.X != -1)
{
MyDrawReversibleRectangle(ptOriginal, ptLast);
}
ptLast = ptCurrent;
MyDrawReversibleRectangle(ptOriginal, ptCurrent);
}
}
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
bHaveMouse = true;
ptOriginal.X = e.X;
ptOriginal.Y = e.Y;
ptLast.X = -1;
ptLast.Y = -1;
}
private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
bHaveMouse = false;
if (ptLast.X != -1)
{
Point ptCurrent = new Point(e.X, e.Y);
MyDrawReversibleRectangle(ptOriginal, ptLast);
}
ptLast.X = -1;
ptLast.Y = -1;
ptOriginal.X = -1;
ptOriginal.Y = -1;
}
}
EDIT: You did not include that part of the code, but as I do it in the Form1-constructor I thought I could also mention it explicitly: You need to define your mouse event handlers on the picture box or this code won't work.

Modifying all items in a list of Points

List<Point> pointList;
public int pickedIndexRight = -1;
public int diffX = 0;
public int diffY = 0;
public Form1()
{
InitializeComponent();
pointList = new List<Point>();
}
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
Point myPoint = new Point(e.X, e.Y);
if (e.Button == MouseButtons.Left)
{
if (pickedIndex == -1)
{
if (pointList.Contains(myPoint) == false)
{
pointList.Add(myPoint);
}
}
}
else if (e.Button == MouseButtons.Right)
{
//if right click near a point then pickedIndexRight is index of that point in list
pickedIndexRight = pointList.FindIndex(delegate(Point point) { return Distance(point, myPoint) < 10; });
}
Invalidate();
}
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right && pickedIndexRight != -1)
{
for (int i = 0; i < pointList.Count - 1; i++)//(int i = pointList.Count - 1; i > 0; i--)
{
diffX = pointList[i].X + (e.X - pointList[i].X);
diffY = pointList[i].Y + (e.Y - pointList[i].Y);
pointList[i] = new Point(diffX, diffY);
Invalidate();
}
}
}
private void Form1_MouseUp(object sender, MouseEventArgs e)
{
pickedIndexRight = -1;
Invalidate();
}
private double Distance(Point p1, Point p2) //calculate distance between two points
{
double d = Math.Sqrt((p2.X - p1.X) * (p2.X - p1.X) + (p2.Y - p1.Y) * (p2.Y - p1.Y));
return d;
}
private void Form1_Paint(object sender, PaintEventArgs e)
{
foreach (Point myPoint in pointList)
{
e.Graphics.DrawRectangle(Pens.Black, myPoint.X, myPoint.Y, 1, 1);
}
}
Ok, so I need my app to draw points on a form with every left click - that works just fine. All the points are stored in a list pointList and then the Paint form draws them each one by one.
The thing is, i need the program to have another functionality - moving all points in parallel by dragging one of the points with the right mouse button - I already wrote the function for that but I can't figure out why my code doesn't work properly - it seems to screw up the entire list when i right click.
I'm all out of ideas, I'd be thankful for any hints.
In this line:
diffX = pointList[i].X + (e.X - pointList[i].X);
The pointList[i].X terms cancel out. So it's just:
diffX = e.X;
You're assigning the current mouse position to every point. If you want to move all the points by the distance that the mouse has moved, but keep their positions relative to each other, you need to remember the previous position of the mouse, so you can compare it with the new position. The difference between new and old mouse positions is the correct amount to add to each point.
So add a field such as:
Point oldMousePosition;
And initialise it when the button-down occurs. In each move event:
pointList[i] = new Point(pointList[i].X + (e.X - oldMousePosition.X),
pointList[i].Y + (e.Y - oldMousePosition.Y))

Categories