   C# Flashing Problem
  Hey there, I've got the following C# code below, which I'm coverting from my Win32 template collection, so I can click on a child window and drag it around the parent's client area. When dragging the child window around, it flickers and shakes really badly, even when I'm dragging but not moving the mouse. The mouse and child window also don't move in the same increments. Is there something I'm doing wrong?using System;using System.Windows.Forms;namespace SampleProgram{ public class CMainWindow : Form {  private CChildWindow child;  public CMainWindow()  {   // setup layout   this.SuspendLayout();   this.Text = "Main Window";   this.StartPosition = FormStartPosition.WindowsDefaultBounds;   this.BackColor = Color.White;   this.ResumeLayout(false);   // setup child form   child = new CChildWindow();   child.BackColor = Color.Black;   child.Width = 320;   child.Height = 240;   child.Left = 15;   child.Top = 15;   this.Controls.Add(child);  } } public class CChildWindow : Control {  #region FIELDS  private bool dragging = false;  private int xPos = 0;  private int yPos = 0;  #endregion  #region CONSTRUCTORS  public CChildWindow()  {   this.MouseDown += new MouseEventHandler(this.OnMouseDown);   this.MouseUp   += new MouseEventHandler(this.OnMouseUp);   this.MouseMove += new MouseEventHandler(this.OnMouseMove);  }  #endregion  #region MOUSE_EVENTS  private void OnMouseDown(object sender, MouseEventArgs e)  {   // capture mouse   if(e.Button == MouseButtons.Left && !dragging) { dragging = true; xPos = e.X; yPos = e.Y;}  }  private void OnMouseUp(object sender, MouseEventArgs e)  {   // release mouse   if(e.Button == MouseButtons.Left && dragging) { dragging = false; xPos = 0; yPos = 0;}  }  private void OnMouseMove(object sender, MouseEventArgs e)  {   // drag child window   if(dragging){ // calculate increment int xPrev = xPos; int yPrev = yPos; xPos = e.X; yPos = e.Y; int dx = xPos - xPrev; int dy = yPos - yPrev; // move window (if actually moved) if(dx == 0 && dy == 0) return; this.SuspendLayout(); this.Left += dx; this.Top += dy; this.ResumeLayout(true);}  }  #endregion }}
  I'd set a breakpoint in the OnMouseMove() event to see if this event is being triggered unexpectedly. That's what it seems like is happening.   ;  Well, debugging it with a break-point is kind of hard, because it will fire at the slightest mouse move, and this shaking behavior manifests itself best with large moves. (Incidentally, conditional breakpoints would actually work nicely here. Stick one in there with the condition (dx < -100 || dx > 100).)yadango, if you put a Console.WriteLine("{0},{1}", dx, dy); after you get your deltas, it's quite illuminating. The mouse motion event fires twice, with opposite deltas. What I suspect happens is that when you move the control, the new mouse position over it registers, and hence, the new mouse move event is fired, this putting the control back where it was, which causes a new mouse event to fire, etc ad infinitum, until you let go of the button.The quick and dirty solution is to unregister the OnMouseMove event handler before this.SuspendLayout(), and reregister it after this.ResumeLayout(). A better solution would be to register to listen for the Move event as well, and handle the this.xPos, and this.yPos for the mouse there, so that when the new mouse move event fires, it doesn't send the control flying back to where it was before. Vovan   