使用GDI +和C#,我试图在面板上绘制一个带有鼠标移动的矩形(称为DrawingPanel),我正在使用Double缓冲,我正在绘制Paint事件中的绘图,仍然会导致很多我的鼠标移动时闪烁。
这是我的代码......
Rectangle mRect = new Rectangle();
Boolean isDragging = false;
public Form1()
{
InitializeComponent();
this.DrawingPanel.Paint += new PaintEventHandler(this.OnPanelPaint);
this.DrawingPanel.MouseMove += new MouseEventHandler(this.OnPanelMouseMove);
this.DrawingPanel.MouseDown += new MouseEventHandler(this.OnPanelMouseDown);
this.DrawingPanel.MouseUp += new MouseEventHandler(this.OnPanelMouseUp);
this.DoubleBuffered = true;
}
private void OnPanelPaint(object sender, PaintEventArgs e)
{
using (Graphics g = this.DrawingPanel.CreateGraphics())
{
g.DrawRectangle(Pens.Black, mRect);
g.FillRectangle(Brushes.SkyBlue, mRect);
}
}
private void OnPanelMouseMove(object sender, MouseEventArgs e)
{
if(isDragging)
{
mRect = new Rectangle(mRect.Left, mRect.Top, e.X - mRect.Left, e.Y - mRect.Top);
this.DrawingPanel.Invalidate();
}
}
private void OnPanelMouseDown(object sender, MouseEventArgs e)
{
mRect = new Rectangle(e.Location, new Size(0, 0));
this.DrawingPanel.Invalidate();
isDragging = true;
}
private void OnPanelMouseUp(object sender, MouseEventArgs e)
{
isDragging = false;
}
任何想法为什么它仍在发生?