在Panel上绘制了一些圆,又连接这些圆构成一棵树.我想点击拖动一个圆时这个圆会跟随指针移动.这个效果我实现了,不过拖动的时候屏幕闪的厉害.请问如何解决?
相关代码如下:
        private void panel1_Paint(object sender, PaintEventArgs e)
        {
            if (nodeCreator == null)
                return;            Graphics g = e.Graphics;
            g.SmoothingMode = SmoothingMode.HighQuality;            drawLines(g, nodeCreator.Root);
            drawPoints(g, nodeCreator.Root);        }        private void drawLines(Graphics g, Node node)
        {
            foreach (Node child in node.Children)
            {
                Pen GreenPen = new Pen(Color.YellowGreen, 5);
                g.DrawLine(GreenPen, node.NodeUtil.X, node.NodeUtil.Y, child.NodeUtil.X, child.NodeUtil.Y);                drawLines(g, child);
            }        }
        private void drawPoints(Graphics g, Node node)
        {
            Brush redBrush;
            switch (node.Layer)
            {
                case 1:
                    redBrush = new SolidBrush(Color.Red);
                    break;
                case 2:
                    redBrush = new SolidBrush(Color.Blue);
                    break;
                case 3:
                    redBrush = new SolidBrush(Color.Green);
                    break;
                case 4:
                    redBrush = new SolidBrush(Color.Fuchsia);
                    break;
                case 5:
                    redBrush = new SolidBrush(Color.DarkTurquoise);
                    break;
                default:
                    redBrush = new SolidBrush(Color.Coral);
                    break;
            }
            if (node.IsOther)
            {
                g.DrawEllipse(new Pen(Color.DarkRed, 4),  node.NodeUtil.X - Radius - 2 , node.NodeUtil.Y - Radius - 2, 2 * Radius + 4, 2 * Radius + 4);
            }
            g.FillEllipse(redBrush, node.NodeUtil.X - Radius, node.NodeUtil.Y - Radius, 2 * Radius, 2 * Radius);
            g.DrawString(node.Name, new Font("新宋体", 12, FontStyle.Bold), new SolidBrush(Color.Orange), node.NodeUtil.X - 10, node.NodeUtil.Y - 10); 
            redBrush.Dispose();
            foreach (Node child in node.Children)
            {
                drawPoints(g, child);
            }
        }        private void panel1_MouseMove(object sender, MouseEventArgs e)
        {
            //非拖动节点时显示ToopTip
            if (!trackMouse)
            {
                if (nodeCreator != null)
                {
                    Node node = nodeCreator.Root;                    tip.Active = CreateToolTip(e, node);
                }
            }
            //拖动节点
            else
            {
                Node node = fideNode(e, nodeCreator.Root);
                if (node != null)
                {
                    node.NodeUtil.X = e.X;
                    node.NodeUtil.Y = e.Y;
                    panel1.Invalidate();
                }
            }
        }

解决方案 »

  1.   

    this.SetStyle(ControlStyles.DoubleBuffer,true);//设置双缓冲,防止图像抖动
    this.SetStyle(ControlStyles.AllPaintingInWmPaint,true);// 忽略系统消息,防止图像闪烁
    this.SetStyle(ControlStyles.UserMouse,true);//控制鼠标完成事件
      

  2.   

    楼上的红星同志帮我看看这个问题好伐http://community.csdn.net/Expert/TopicView1.asp?id=4503513
      

  3.   

    多谢边城浪子,还有一个小问题请教一下,是不是只有Form可以用SetStyle,而我在Panel中无法调用呢?
    这是不是就意味着只能在Form上直接画图,而不能在Panel上画这种无闪烁的图呢?
      

  4.   

    要生成一个继承自Control或者Panel的控件,然后可以使用SetStyle,因为它是保护型的.