WinFrom窗体上加载时,用GDI+产生一个圆形,然后鼠标点住该圆形,进行拖动,一般速度拖动时该该图形还会跟着鼠标移动,不过我用非常快的速度移动鼠标,图形往往不能快速移动了,请问这个问题该怎么解决啊!!!!!我原先是每次在
 OnMouseMove方法中画一个移动后的圆,然后this.Invalidate()的。

解决方案 »

  1.   

    不知道我下面连接中的代码对lz有没有用处?
    http://blog.csdn.net/wzuomin/archive/2006/12/13/1441007.aspx
      

  2.   

    可以把圆做成一个对象,让它进行自绘,这样效率就会比较高了,你试试下面的代码,我移动非常快也能跟上鼠标的动作的示例代码
    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;namespace WindowsFormsApplication3
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }        protected override void OnLoad(EventArgs e)
            {
                base.OnLoad(e);
                m_DraggableGDIObjects = new List<ADraggableGDIObject>();            // Add circle object
                DraggableCircle draggableCircle = new DraggableCircle();
                m_DraggableGDIObjects.Add(draggableCircle);
            }        protected override void OnPaint(PaintEventArgs e)
            {
                base.OnPaint(e);            foreach (ADraggableGDIObject item in m_DraggableGDIObjects)
                {
                    item.OnPaint(e);
                }
            }        protected override void OnMouseDown(MouseEventArgs e)
            {
                base.OnMouseDown(e);
                foreach (ADraggableGDIObject item in m_DraggableGDIObjects)
                {
                    if (item.Region.Contains(e.Location))
                    {
                        item.IsDragging = true;
                        item.DraggingPoint = e.Location;
                    }
                }
            }        protected override void OnMouseMove(MouseEventArgs e)
            {
                base.OnMouseMove(e);
                foreach (ADraggableGDIObject item in m_DraggableGDIObjects)
                {
                    if (item.IsDragging)
                    {
                        item.Region = new Rectangle(
                            item.Region.Left + e.X - item.DraggingPoint.X,
                            item.Region.Top + e.Y - item.DraggingPoint.Y,
                            item.Region.Width,
                            item.Region.Height);
                        item.DraggingPoint = e.Location;
                        Invalidate();
                    }
                }        }        protected override void OnMouseUp(MouseEventArgs e)
            {
                base.OnMouseUp(e);            foreach (ADraggableGDIObject item in m_DraggableGDIObjects)
                {
                    if (item.IsDragging)
                    {
                        item.IsDragging = false;
                        item.DraggingPoint = Point.Empty;
                    }
                }
            } 
            private List<ADraggableGDIObject> m_DraggableGDIObjects;
        }
        public abstract class ADraggableGDIObject
        {
            public abstract Rectangle Region { get; set; }        public abstract bool IsDragging { get; set; }        public abstract Point DraggingPoint { get; set; }        public abstract void OnPaint(PaintEventArgs e);
        }    public class DraggableCircle : ADraggableGDIObject // 同样的方式可以从ADraggableGDIObject生成其它图形的继承类,比如矩形,三角形等等。。
        {
            public DraggableCircle()
            {
                m_Region = new Rectangle(0, 0, 20, 20);
            }
            public override Rectangle Region
            {
                get { return m_Region; }
                set { m_Region = value; }
            }        public override void OnPaint(PaintEventArgs e)
            {
                e.Graphics.DrawEllipse(Pens.Black, m_Region);
            }        public override bool IsDragging
            {
                get { return m_IsDragging; }
                set { m_IsDragging = value; }
            }        public override Point DraggingPoint
            {
                get { return m_DraggingPoint; }
                set { m_DraggingPoint = value; }
            }        private bool m_IsDragging;
            private Point m_DraggingPoint; 
            private Rectangle m_Region;
        }
    }
      

  3.   

    http://www.abab123.com/bbs/down.asp?html=1330336  这个网站也不错
      

  4.   

    参考下面的代码: public partial class frmDrawObj : Form
    {
    private Point m_DownPoint;
    private Point m_LastPoint;
    private Rectangle m_RectObj;
    private bool m_CaptureRect;
    public frmDrawObj()
    {
    InitializeComponent();
    this.DoubleBuffered = true;
    m_RectObj = Rectangle.FromLTRB(0, 0, 100, 100);
    }
    protected override void OnMouseDown(MouseEventArgs e)
    {
    base.OnMouseDown(e);
    if (e.Button == MouseButtons.Left)
    {
    m_CaptureRect = false;
    if (this.m_RectObj.Contains(e.Location))
    {
    this.m_DownPoint = e.Location;
    this.m_LastPoint = m_RectObj.Location;
    m_CaptureRect = true;
    }
    }
    }
    protected override void OnMouseMove(MouseEventArgs e)
    {
    base.OnMouseMove(e);
    if (e.Button == MouseButtons.Left && m_CaptureRect)
    {
    Point point = e.Location;
    m_RectObj.X = this.m_LastPoint.X + (point.X - this.m_DownPoint.X);
    m_RectObj.Y= this.m_LastPoint.Y + (point.Y - this.m_DownPoint.Y);
    this.Invalidate();
    }
    }
    protected override void OnPaint(PaintEventArgs e)
    {
    base.OnPaint(e);
    e.Graphics.DrawRectangle(SystemPens.ControlText, this.m_RectObj);
    }
    }
      

  5.   

    建议你用C++写,C#的速度再快也赶不到C++,但是C++的开发难度较大!
      

  6.   

    你把画图代码写到一个方法中  比如
    draw()
    {
        graphics.drawLine(...);
        ...
    }然后你在你的鼠标事件代码最后一句再加上重绘方法
    事件方法  XX_mousemove()
    {
        ...
        draw();
    }
      

  7.   

    一个圆应该问题不大 我做过一个座位拖动的,支持多选,图片不超过2000*2000的话,是不会出现拖不动的。应该是你算法有问题。
    Ps:
    再次对那些无知而迷信C++速度的人进行鄙视。