Panning of a map is not smooth in winforms - c#

Combining the multiple small images *(256*256)* into a large image(dimensions of picturebox). Now if I am trying to pan / move the large image it is not moving until the difference is 256 pixels. Its not panning smoothly. Here is my code
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
if (isdragging)
{
mapCenterX += (-e.X + (startingpoint.X)) * Map.scaleX;
mapCenterY -= (-e.Y + (startingpoint.Y)) * Map.scaleY;
Map.DrawMap(mapCenterX, mapCenterY, mapZoom);
startingpoint.X = e.X;
startingpoint.Y = e.Y;
pictureBox1.Invalidate();
}
}
}
private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
isdragging = false;
}
}
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
isdragging = true;
startingpoint.X = e.X;
startingpoint.Y = e.Y;
}
}

Related

Custom scrollbar for panel does not scroll smoothly

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;
}
}

Save 4 mouse positions

Hi I want to save 4 mouse positions when I click on button.
Smth like this:
ButtonClick -> 1.MouseClick / Save Mouse Position-> 2.MouseClick / Save Mouse Positon .....
private void button2_Click_1(object sender, EventArgs e)
{
if (!int.TryParse(textBox4.Text, out parsedValue))
{
MessageBox.Show("Wpsiz liczbe");
return;
}
else
{
iset = int.Parse(textBox3.Text);
ms = int.Parse(textBox4.Text);
MouseDownFunction();
}
}
private void MouseDownFunction(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
eqhelmetx = MousePosition.X;
eqhelmety = MousePosition.Y;
if (e.Button == MouseButtons.Left)
{
eqchestx = MousePosition.X;
eqchesty = MousePosition.Y;
if (e.Button == MouseButtons.Left)
{
eqleginsx = MousePosition.X;
eqleginsy = MousePosition.Y;
if (e.Button == MouseButtons.Left)
{
eqbootsx = MousePosition.X;
eqbootsy = MousePosition.Y;
}
}
}
}
}```
I tried but it doesn't work. Thanks for any help
List<Point> MousePositions = new List<Point>(); // list for saving mouse positions
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
if (MousePositions.Count == 4)
MousePositions.RemoveAt(0); // for saving last 4 positions
MousePositions.Add(e.Location); // when clicking form saving mouse location
}
private void button1_Click(object sender, EventArgs e)
{
foreach (var item in MousePositions)
MessageBox.Show(item.ToString()); // showing mouse positions
}

Dragging Objects in C#

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;
}

Moving a control with mouse-move in C#

I want to move a button by mouse using C#
I found this solution on stackoverflow
private Point MouseDownLocation;
private void MyControl_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Left)
{
MouseDownLocation = e.Location;
}
}
private void MyControl_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Left)
{
(button)sender.Left = e.X + (button)sender.Left -MouseDownLocation.X;
(button)sender.Top = e.Y + (button)sender.Top - MouseDownLocation.Y;
}
}
I hope somebody here can explain to me
what is difference between
e.X ( which is the coordinates of mousemoving eventArgs) and MouseDownLocation.X

How to draw straight line to the mouse coordinates?

When user press the left button and move the mouse it should appears a straight line(not permanent line) from previous point to the current mouse moving position. Finally a real straight line will appear when the user releases the left mouse. please help me ..how do i do it?
List<Point> points = new List<Point>();
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
points.Add(e.Location);
pictureBox1.Invalidate();
}
}
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
if (points.Count > 1)
e.Graphics.DrawLines(Pens.Black, points.ToArray());
}
This is what you're looking for
private Stack<Point> points = new Stack<Point>();
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
points.Clear();
points.Push(e.Location);
}
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
if (points.Count > 1)
{
points.Pop();
}
if (points.Count > 0 && e.Button == System.Windows.Forms.MouseButtons.Left)
{
points.Push(e.Location);
pictureBox1.Invalidate();
}
}
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
if (points.Count > 1)
e.Graphics.DrawLines(Pens.Black, points.ToArray());
}
I used Stack for ease of use, you are free to change to whatever collection of your choice.
To draw several line you can do something like this
private Stack<Line> lines = new Stack<Line>();
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
lines.Push(new Line { Start = e.Location });
}
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
if (lines.Count > 0 && e.Button == System.Windows.Forms.MouseButtons.Left)
{
lines.Peek().End = e.Location;
pictureBox1.Invalidate();
}
}
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
foreach (var line in lines)
{
e.Graphics.DrawLine(Pens.Black, line.Start, line.End);
}
}
class Line
{
public Point Start { get; set; }
public Point End { get; set; }
}
you can use mentioned code
Point currentPoint = new Point();
private void Canvas_MouseDown_1(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
if (e.ButtonState == MouseButtonState.Pressed)
currentPoint = e.GetPosition(this);
}
private void Canvas_MouseMove_1(object sender, System.Windows.Input.MouseEventArgs e)
{
if (e.LeftButton == MouseButtonState.Pressed)
{
Line line = new Line();
line.Stroke = SystemColors.WindowFrameBrush;
line.X1 = currentPoint.X;
line.Y1 = currentPoint.Y;
line.X2 = e.GetPosition(this).X;
line.Y2 = e.GetPosition(this).Y;
currentPoint = e.GetPosition(this);
paintSurface.Children.Add(line);
}
}

Categories

Resources