MouseMove sensibility
I use MouseMove event to move objects(say labels).
simple principle(schematic):
OnMouseMove(e MouseEventArgs)
if e.Button == Left
deltaX = e.X - lastX
foreach label in labels
label.Location.X += deltaX
lastX = e.X
Once the labels number increase, I start to see the labels traces along the moving traj开发者_运维问答ectory. I have something like I I I II III II I I III II, but want to have something like I I I I as traces. I'd like to know when the mouse "starts" and "stops to move" something like this.
I move labels along a horizontal axe. MouseDown
(set LastX) and go on. Nobody knows when stops, only mouse move sensibility. Surely I can use MouseUp
to know when the movement ends, but however if user maintains the button down and stop moving I want to reflect the latest label position.
Is there a way to prevent this kind of traces?
tried
label.Visible = false
label.Location.X += deltaX
label.Visible = true
does not help.
parent.SuspendLayout and ResumeLayout does not help a lot, because I need doing this at every mouse movement, so any effect.
Edit: I've just noticed your edit about when the mouse stops. You could use a timer to help you. Have one set to the interval you want to update the positions on and auto reset when it elapses. Start it on mouse down and stop it on mouse up. When the timer elapses, update the locations of the labels.
--Original answer for context:--
Yes, you can suspend drawing on the parent control, move the labels, and then resume drawing and refresh.
From this SO question:
[DllImport("user32.dll")]
public static extern int SendMessage(IntPtr hWnd, Int32 wMsg, bool wParam, Int32 lParam);
private const int WM_SETREDRAW = 11;
public static void SuspendDrawing(Control parent)
{
SendMessage(parent.Handle, WM_SETREDRAW, false, 0);
}
public static void ResumeDrawing(Control parent)
{
SendMessage(parent.Handle, WM_SETREDRAW, true, 0);
parent.Refresh();
}
Example Usage:
private void OnMouseMove(MouseEventArgs e)
{
int deltaX = e.X - lastX;
// Suspend drawing here
foreach (Label label in labels)
{
label.Location.X += deltaX;
}
lastX = e.X;
// Resume drawing here
}
Edit: If you only want to show the change in position if the difference is greater than n pixels, then you should use the Pythagorean Theorem to calculate the distance between the old position and the new position and only move it if the difference is greater than n. When the mouse button comes up, move the labels to the location they are supposed to be according to the mouse.
Pseudo-code:
difference = Math.Sqrt(x * x, y * y);
if (difference > n) // n is whatever number you want
{
// move the labels
// set the old position to the new position
}
精彩评论