views:

188

answers:

1

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 trajectory. 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.

+2  A: 

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
}
Zach Johnson
hm. The problem is that I move labels very frequently. I have something like I I I II III II I I III II, but want to have something like I I I I I I I as traces. I'd like to know when the mouse "starts" and "stops to move" something like this. I can make also the parent.Suspend/ResumeLayout() but ... when?
serhio
@serhio: I've edited to include an example of how you might use SuspendDrawing/ResumeDrawing. If I understand you correctly, the labels are not moving "together" (you see them move separately). This solution will take care of that.
Zach Johnson
@Zach. Yes. but... the problem is that I want to do instead of doing 10 movements(say from 1 to 20) only 5. En gros, when moving the "label container" I don't need to see labels moving, only start final, and maybe one intermediate point is enough.
serhio
Do you move them with mouse down (i.e. dragging them) or with just mouse move? If so: assume the mouse starts to move at mouse down, and finishes moving at mouse up.
Sameh Serag
@Sameh Serag: with MouseDown.
serhio
@serhio: How do you know the end point while the labels are being moved? You could put the labels on a "grid" (only allowing them to be in positions divisible by 5 or some other arbitrary number, but that doesn't sound like what you are after.
Zach Johnson
@Zach: 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.
serhio
@serhio: I've edited to show a possible solution.
Zach Johnson
@Zach: Yes, but this "n" should depend of the mouse "speed" if slow n should decrease if fast - increase.
serhio
@serhio: Edited again. I think the timer solution is what you want.
Zach Johnson
your idea about timer seems to be interesting. I move(and update lastX) let's say once in 250ms...
serhio
mainly, there is no need of Timer. Just save and check the `DateTime.Now - lastXDateTime > minTimeSpan`
serhio
@serhio: That will work when the mouse is moving, but it won't work when the mouse stops moving. I.e. If the mouse stops moving and it hasn't updated to where the mouse is, it will not be updated until the mouse moves again.
Zach Johnson
@Zach: from one part, I don't clearly see the code with the timer, it seems me s little complicated. from the other, I believe there will be no really visible delta in 200-250 ms for a mouse movement. However, initially I set 400ms, but the "steps" are too evident in that case.
serhio