I have a form which can be moved when the user clicks and drags in a border area. The implementations I've seen all lock to the current mouse position, so that when the form is moved, the form jumps so that the mouse is in the upper-left corner. I'd like to change it so that it behaves like a normal windows form, and the form stays at the same position relative to the mouse when moved. My current code looks like this:
Point locationClicked;
bool isMouseDown = false;
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
isMouseDown = true;
locationClicked = new Point(e.Location.X, e.Location.Y);
}
private void Form1_MouseUp(object sender, MouseEventArgs e)
{
isMouseDown = false;
}
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
if (isMouseDown && targetCell == new Point(-1, -1) && (mouseLocation.X < margin.X || mouseLocation.Y < margin.Y ||
mouseLocation.X > margin.X + cellSize.Width * gridSize.Width ||
mouseLocation.Y > margin.Y + cellSize.Height * gridSize.Height))
{
this.Location = new Point(e.Location.X - locationClicked.X, e.Location.Y - locationClicked.Y);
}
}
When I drag the window, it behaves similarly to what I want. The form flickers between two locations on the screen, one of which moves at about half the rate of the mouse. Is there a way I can fix this?
Try this out...
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
Point locationClicked;
bool dragForm = false;
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Left)
{
locationClicked = new Point(e.Location.X, e.Location.Y);
if (isMouseDown && targetCell == new Point(-1, -1) && (mouseLocation.X < margin.X || mouseLocation.Y < margin.Y ||
mouseLocation.X > margin.X + cellSize.Width * gridSize.Width ||
mouseLocation.Y > margin.Y + cellSize.Height * gridSize.Height))
{
dragForm = true;
}
}
}
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
if (dragForm)
{
this.Location = new Point(this.Location.X + (e.X - locationClicked.X), this.Location.Y + (e.Y - locationClicked.Y));
}
}
private void Form1_MouseUp(object sender, MouseEventArgs e)
{
dragForm = false;
}
}
Related
customBtn1 is the custom scrollbar. The below code sample works, but the scrolling action is jittery and stuttery. It does not scroll smoothly. Any idea why this could be happening and how to fix it?
int PreviousBarLoc;
private void MainForm_Load(object sender, EventArgs e)
{
PreviousBarLoc= customBtn1.Location.Y;
}
//move the scrollbar up and down when the user drags it
private void customBtn1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Left)
{ MouseDownLocation = e.Location; }
}
private Point MouseDownLocation;
private void customBtn1_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
customBtn1.Top = e.Y + customBtn1.Top - MouseDownLocation.Y;
}
}
//scroll the panel
private void customBtn1_LocationChanged(object sender, EventArgs e)
{
int locDifference = customBtn1.Location.Y - PreviousBarLoc;
if (steamSrvListPnl.VerticalScroll.Value + locDifference <= 255 && steamSrvListPnl.VerticalScroll.Value + locDifference >= 0)
{
steamSrvListPnl.VerticalScroll.Value += locDifference;
}
PreviousBarLoc= customBtn1.Location.Y;
}
Your deltas are not quite right. The way it is currently written, if you drag the mouse down and then start dragging back up, it will not scroll back up because your current e.Y is still below the original MouseDownLocation.Y. I'm not entirely sure of your setup, but below is an example of how to get smooth scrolling to work when the left mouse button is pressed and dragged on a button:
private int _prevY = 0;
private bool _mouseDown = false;
private void button1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
_mouseDown = true;
_prevY = e.Y;
}
}
private void button1_MouseMove(object sender, MouseEventArgs e)
{
if (_mouseDown)
{
panel1.VerticalScroll.Value = Math.Max(panel1.VerticalScroll.Minimum, Math.Min(panel1.VerticalScroll.Maximum, panel1.VerticalScroll.Value + e.Y - _prevY));
_prevY = e.Y;
}
}
private void button1_MouseUp(object sender, MouseEventArgs e)
{
if (_mouseDown)
{
_mouseDown = false;
_prevY = 0;
}
}
I am attempting to drag Music Notes vertically, up and down a Music Staff. However, rather than a constant drag, I would like the music notes to only be allowed to be dragged onto particular intervals (only specific y-coordinates). For example, in a vertical line, a music note can be dragged on to coordinates (0,0), (0,5) or (0,10).
Below is my relevant code:
private Point MouseDownLocation;
private void Note_MouseDown(object sender, MouseEventArgs e)
{
foreach (MusicNote mn in panel2.Controls.OfType<MusicNote>())
{
if (sender == mn)
{
if (e.Button == MouseButtons.Left)
{
MouseDownLocation = e.Location;
}
}
}
}
private void Note_MouseMove(object sender, MouseEventArgs e)
{
foreach(MusicNote mn in panel2.Controls.OfType<MusicNote>())
{
if (sender == mn)
{
if (e.Button == MouseButtons.Left)
{
mn.Top = e.Y + mn.Top - MouseDownLocation.Y;
}
}
}
}
Any help is appreciated. Thank you!
Basically, you need to check if you drag up or drag down
You should want to check the MouseDown.X and compare it to the MouseUp.X (or Y if you want to check vertical direction as well). It is important to note that (0, 0) is the upper left of your screen. So you need to compare the X position from mouse down event to the mouse up event.
here's an example with one label that moves up and down in steps of 10
private void label1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Left)
{
if (label1.Location.Y > 0 && label1.Location.Y < panel1.Size.Height) // not the most accurate way, but you get the idea
{
mPointDown = new Point(e.X, e.Y);
}
}
}
private void label1_MouseUp(object sender, MouseEventArgs e)
{
bool movedUp, movedDown;
if (e.Y == mPointDown.Y)
{
movedUp = movedDown = false;
}
else
{
movedUp = e.Y < mPointDown.Y;
movedDown = !movedUp;
}
if (movedDown)
{
label1.Location = new Point(label1.Location.X, label1.Location.Y + 10);
}
else if (movedUp)
{
label1.Location = new Point(label1.Location.X, label1.Location.Y - 10);
}
}
private void label1_MouseMove(object sender, MouseEventArgs e)
{
mouseDownPoint = e.Location;
}
How do I prevent my Mouse from getting out my pictureBox (500 x 500 pixels) when dragging an image inside it?
Here's the mouse events:
private void pictureBox_Canvass_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
drag = true;
//start = new Point(e.Location.X + (int)imageRect.Location.X, e.Location.Y + (int)imageRect.Location.Y);
start = new Point((int)Shape.center.X - ((int)imageRect.Location.X - e.X), (int)Shape.center.Y - ((int)imageRect.Location.Y - e.Y));
}
}
private void pictureBox_Canvass_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left && drag == true)
{
Point loc = new Point((int)((e.X - start.X) - (imageRect.Width / 2)), (int)((e.Y - start.Y) - (imageRect.Height / 2)));
start.Offset(loc.X, loc.Y);
imageRect.Location = start;
Debug.WriteLine(start);
pictureBox_Canvass.Invalidate();
}
}
private void pictureBox_Canvass_MouseUp(object sender, MouseEventArgs e)
{
drag = false;
}
I need to drag 6 images, which are in PictureBoxes, to 6 answer panels, one at a time and if the correct image is dragged to the correct spot it adds 1 to the score which is in a class called 'score'. There is code below to show one of the pcitures working but the form lags alot and if the image is dragged anywhere above the panel, it will snap into place. is there a way to stop this?
namespace DragAndDropQuiz
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void pcBxLiverpool_MouseEnter(object sender, EventArgs e)
{
this.Cursor = Cursors.Hand;
}
private void pcBxLiverpool_MouseLeave(object sender, EventArgs e)
{
this.Cursor = Cursors.Default;
}
private int xPos;
private int yPos;
private void pcBxGeneral_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Left)
{
xPos = e.X;
yPos = e.Y;
}
}
private void pcBxGeneral_MouseMove(object sender, MouseEventArgs e)
{
if (sender != null && sender.GetType() == typeof(PictureBox))
{
if (e.Button == System.Windows.Forms.MouseButtons.Left)
{
((PictureBox)sender).Top += (e.Y - yPos);
((PictureBox)sender).Left += (e.X - xPos);
this.Refresh();
}
}
}
private void pcBxGeneral_MouseUp(object sender, MouseEventArgs e)
{
if (sender != null && sender.GetType() == typeof(PictureBox))
{
if (e.Button == System.Windows.Forms.MouseButtons.Left)
{
PictureBox answer = (PictureBox)sender;
if (answer.Location.X < pnlAnswer.Location.X)
{
if (answer.Location.X + answer.Width > pnlAnswer.Location.X)
{
if ((answer.Location.X + answer.Width) < pnlAnswer.Location.X + pnlAnswer.Width)
{
answer.Location = pnlAnswer.Location;
}
}
}
else if (answer.Location.X > pnlAnswer.Location.X)
{
if (answer.Location.X < (pnlAnswer.Location.X + pnlAnswer.Width))
{
answer.Location = pnlAnswer.Location;
}
}
}
}
}
private void Form2_Paint(object sender, PaintEventArgs e)
{
}
private void groupBox2_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
g.DrawRectangle(new Pen(Color.Red), pnlAnswer.Location.X, pnlAnswer.Location.Y, pnlAnswer.Width, pnlAnswer.Height);
}
}
}
I have a PictureBox called pic, placed inside another PictureBox called picTrack.
My goal is to be able to let the user, at run time, change the position of pic by draging it.
This is what I have so far:
int x_offset = 0; // any better to do this without having a global variable?
int y_offset = 0;
void pic_MouseDown(object sender, MouseEventArgs e)
{
PictureBox me = (PictureBox)sender;
x_offset = me.Left - e.X;
y_offset = me.Top - e.Y;
}
void pic_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Left)
{
PictureBox me = (PictureBox)sender;
me.Left = e.X + x_offset;
me.Top = e.Y + y_offset;
picTrack.Invalidate();
}
}
This code only works at a very basic level. I have 2 problems with it:
1.) picTrack is not updated if the user does not let go of the mouse button. Ghost images of pic can be seen while pic is getting moved around (it's like pic is having a tail).
2.) pic is "giggling" (i.e. rapidly shaking left and right, up and down, around its location).
How should I solve these 2 problems and create a more smooth drag & drop? Thanks.
Here this actually works, I've written quite a few dragging things before.. it may not be perfect but this should give you something to work with.
Point dragPoint = Point.Empty;
bool dragging = false;
private void pic_MouseDown(object sender, MouseEventArgs e)
{
dragging = true;
dragPoint = new Point(e.X, e.Y);
}
private void pic_MouseMove(object sender, MouseEventArgs e)
{
if (dragging)
pic.Location = new Point(pic.Location.X + e.X - dragPoint.X, pic.Location.Y + e.Y - dragPoint.Y);
}
private void pic_MouseUp(object sender, MouseEventArgs e)
{
dragging = false;
}
See, if you were dragging a local picture that you were just rendering yourself, this wouldn't be right.. but since you are moving a control after you move it, the new move coordinates are relative to the control. Therefore, you do not need to update dragPoint to the last position on move. If you were just moving a shape/image you were rendering OnPaint, you'd have to do update the drag point each movement.
There's one improvement you could make, if desired, which is to only start dragging if the user moves the cursor a certain distance D. For example, something like this:
Point dragPoint = Point.Empty;
bool dragging = false;
bool mouseDown = false;
private void pic_MouseDown(object sender, MouseEventArgs e)
{
mouseDown = true;
dragPoint = new Point(e.X, e.Y);
}
private void pic_MouseMove(object sender, MouseEventArgs e)
{
int deltaX = e.X - dragPoint.X;
int deltaY = e.Y - dragPoint.Y;
if (!dragging && mouseDown && deltaX * deltaX + deltaY * deltaY > 100)
dragging = true;
if (dragging)
pic.Location = new Point(pic.Location.X + deltaX, pic.Location.Y + deltaY);
}
private void pic_MouseUp(object sender, MouseEventArgs e)
{
dragging = false;
mouseDown = false;
}
Which checks if the user has moved the mouse 10 pixels (sqrt of 100).
If you don't want a global, you could try implementing your own behavior system and creating a reusable piece of code that you can attach to things you want to move. Something like this:
public class Behavior<T> where T : class
{
public T AssociatedObject
{
get;
private set;
}
public Behavior(T associatedObject)
{
this.AssociatedObject = associatedObject;
}
public virtual void Attach() { }
public virtual void Detach() { }
}
public class DragBehavior : Behavior<Control>
{
Point dragPoint = Point.Empty;
bool dragging = false;
bool mouseDown = false;
public DragBehavior(Control c) : base(c)
{
}
public override void Attach()
{
AssociatedObject.MouseDown += new MouseEventHandler(control_MouseDown);
AssociatedObject.MouseMove += new MouseEventHandler(control_MouseMove);
AssociatedObject.MouseUp += new MouseEventHandler(control_MouseUp);
}
private void control_MouseUp(object sender, MouseEventArgs e)
{
dragging = false;
mouseDown = false;
}
private void control_MouseMove(object sender, MouseEventArgs e)
{
int deltaX = e.X - dragPoint.X;
int deltaY = e.Y - dragPoint.Y;
if (mouseDown && deltaX * deltaX + deltaY * deltaY > 100)
dragging = true;
if (dragging)
AssociatedObject.Location = new Point(AssociatedObject.Location.X + deltaX, AssociatedObject.Location.Y + deltaY);
}
private void control_MouseDown(object sender, MouseEventArgs e)
{
mouseDown = true;
dragPoint = new Point(e.X, e.Y);
}
public override void Detach()
{
AssociatedObject.MouseDown -= new MouseEventHandler(control_MouseDown);
AssociatedObject.MouseMove -= new MouseEventHandler(control_MouseMove);
AssociatedObject.MouseUp -= new MouseEventHandler(control_MouseUp);
}
}
public partial class Form1 : Form
{
DragBehavior dragger;
public Form1()
{
InitializeComponent();
DoubleBuffered = true;
dragger = new DragBehavior(pic);
dragger.Attach();
}
}
Maybe that is better than "creating a global variable" (or more like creating a member variable in your form. =)
Try this
int x_offset = 0; // any better to do this without having a global variable?
int y_offset = 0;
private void pic_MouseDown(object sender, MouseEventArgs e)
{
PictureBox me = (PictureBox)sender;
x_offset = e.X;
y_offset = e.Y;
}
private void pic_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Left)
{
PictureBox me = (PictureBox)sender;
me.Left = e.X + me.Left - x_offset;
me.Top = e.Y + me.Top - y_offset;
}
}