Home » C# ProgrammingRSS

Line between draggable controls flickering

Hi all,

I am prototyping a project that required draggable controls with connecting lines.

So far I have used two buttons which i have made draggable and a connecting line, however the line doesn't appear straight and when it does it flickers when you move the mouse over the buttons or drag them. I have enabled double buffering on the form but that hasn't made a difference. 

Can anyone suggest why this might be and how to fix it, code and link below.

Thanks in advance.

Chris

 

I have zipped my project up (2010) here if anyone wants a look: http://dl.dropbox.com/u/889316/DraggableObjects.zip

My code is this:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D;

namespace ElementManagerTest
{
  publicpartialclass Form1 : Form
  {
    bool mousemove;   // whether the mouse is being moved
    Point mouseorigin; // the original mouse location before the move startedpublic Form1()
    {
      InitializeComponent();
      UpdateLine();
    }

    privatevoid button1_MouseDown(object sender, MouseEventArgs e)
    {
      ControlDown();
    }

    privatevoid button1_MouseMove(object sender, MouseEventArgs e)
    {
      ControlMove(button1);
    }

    privatevoid button1_MouseUp(object sender, MouseEventArgs e)
    {
      ControlUp();
    }

    privatevoid ControlDown()
    {
      if (mousemove == false)
      {
        mousemove = true; // we are now moving a control
        mouseorigin = MousePosition;
        //controlorigin = this.Location;
      }
    }

    privatevoid ControlMove(Control cont)
    {
      if ((mousemove == true) && (MousePosition != mouseorigin))
      {
        cont.Left += (MousePosition.X - mouseorigin.X);
        cont.Top += (MousePosition.Y - mouseorigin.Y);
        mouseorigin = MousePosition;
      }
      UpdateLine();
    }

    privatevoid UpdateLine()
    {
      Pen pen = new Pen(Color.Blue, 2);
      this.Refresh();
      Graphics g = this.splitContainer2.Panel1.CreateGraphics();
      g.SmoothingMode = SmoothingMode.HighQuality;
      g.DrawLine(pen, button1.Left + (button1.Width / 2), button1.Top + (button1.Height / 2), button2.Left + (button2.Width / 2), button2.Top + (button2.Height / 2));
      g.Dispose();
    }

    privatevoid ControlUp()
    {
      mousemove = false;
    }

    privatevoid button2_MouseMove(object sender, MouseEventArgs e)
    {
      ControlMove(button2);
    }
  }
}

 

2 Answers Found

 

Answer 1

Hi Grovesy:

Welcome to the MSDN Forum.

I downloaded your project and have fixed it. Please see this:

privatevoid UpdateLine()

{

    Graphics g = this.splitContainer2.Panel1.CreateGraphics();

Pen pen = newPen(Color.Blue, 2);

//this.Refresh();   --- this may cause flickers

    g.Clear(this.splitContainer1.Panel1.BackColor);

    g.SmoothingMode = SmoothingMode.HighQuality;

    g.DrawLine(pen, button1.Left + (button1.Width / 2), button1.Top + (button1.Height / 2), button2.Left + (button2.Width / 2), button2.Top + (button2.Height / 2));

    g.Dispose();

}

If you have any questions, please feel free to tell us.

Best Regards

 

Answer 2

Also, you should not use CreateGraphics in that manner.  Instead you should handle the Paint event of your splitContainer.Panel2 and do your drawing there.

See this link:

http://bobpowell.net/creategraphics.htm

 
 
 

<< Previous      Next >>


Microsoft   |   Windows   |   Visual Studio   |   Follow us on Twitter