0

使用 GDI+ 和 C#,我试图在面板(称为 DrawingPanel)上用鼠标移动绘制一个矩形,我也在使用双缓冲我在 Paint Event 中进行绘图,但当我的鼠标在移动。

这是我的代码...

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;
}

知道为什么它还在发生吗?

4

0 回答 0