为什么鼠标快速拖动用GDI+画的图形时,图形不能快速移动,各位大侠帮帮忙啊,急!!! WinFrom窗体上加载时,用GDI+产生一个圆形,然后鼠标点住该圆形,进行拖动,一般速度拖动时该该图形还会跟着鼠标移动,不过我用非常快的速度移动鼠标,图形往往不能快速移动了,请问这个问题该怎么解决啊!!!!!我原先是每次在 OnMouseMove方法中画一个移动后的圆,然后this.Invalidate()的。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 不知道我下面连接中的代码对lz有没有用处?http://blog.csdn.net/wzuomin/archive/2006/12/13/1441007.aspx 可以把圆做成一个对象,让它进行自绘,这样效率就会比较高了,你试试下面的代码,我移动非常快也能跟上鼠标的动作的示例代码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; }} http://www.abab123.com/bbs/down.asp?html=1330336 这个网站也不错 参考下面的代码: 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); } } 建议你用C++写,C#的速度再快也赶不到C++,但是C++的开发难度较大! 你把画图代码写到一个方法中 比如draw(){ graphics.drawLine(...); ...}然后你在你的鼠标事件代码最后一句再加上重绘方法事件方法 XX_mousemove(){ ... draw();} 一个圆应该问题不大 我做过一个座位拖动的,支持多选,图片不超过2000*2000的话,是不会出现拖不动的。应该是你算法有问题。Ps:再次对那些无知而迷信C++速度的人进行鄙视。 c# 如何使用WinAPI 拦截其它窗口大化改成其他操作 循环插入DataGridViewRow,报错 跳转先后执行顺序问题。 在c#中调用DLL中的函数 谁是 箫心IT乐园会员啊?给我发个邀请码哎,或者帮下一个文件 如果说C#比别的语方更出色,请问他好在哪里? 如何把focus锁定在textbox上不动? 写个取硬盘号的注册程序,测试了IDE可以,但串口硬盘不行,能否给点思路,谢谢了 关于安装程序的问题.互相帮助,共享第一啊. 这段设置frame框架的javascript代码能改成C#来实现吗? 关于combobox 下拉列表绑定数据问题 如果我想做一个Excel模板 并生成 我要怎么做
http://blog.csdn.net/wzuomin/archive/2006/12/13/1441007.aspx
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;
}
}
{
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);
}
}
draw()
{
graphics.drawLine(...);
...
}然后你在你的鼠标事件代码最后一句再加上重绘方法
事件方法 XX_mousemove()
{
...
draw();
}
Ps:
再次对那些无知而迷信C++速度的人进行鄙视。