Jump to content
  • Advertisement
Sign in to follow this  
yadango

C# Flashing Problem

This topic is 4449 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
Advertisement
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!