0

我正在为Windows Mobile开发C#应用程序。我有一个自定义控件,重写了 OnPaint以绘制用户使用指针移动的图像。我自己的 OnPaint 方法是这样的:


protected override void OnPaint(PaintEventArgs e)
{
    Graphics gxOff; //Offscreen graphics
    Brush backBrush;

    if (m_bmpOffscreen == null) //Bitmap for doublebuffering
    {
        m_bmpOffscreen = new Bitmap(ClientSize.Width, ClientSize.Height);
    }

    gxOff = Graphics.FromImage(m_bmpOffscreen);

    gxOff.Clear(Color.White);

    backBrush = new SolidBrush(Color.White);
    gxOff.FillRectangle(backBrush, this.ClientRectangle);

    //Draw some bitmap
    gxOff.DrawImage(imageToShow, 0, 0, rectImageToShow, GraphicsUnit.Pixel);

    //Draw from the memory bitmap
    e.Graphics.DrawImage(m_bmpOffscreen,  this.Left, this.Top);

    base.OnPaint(e);
}

imageToShow它图像。

rectImageToShow这种方式在事件 OnResize 上初始化:

rectImageToShow = 
   new Rectangle(0, 0, this.ClientSize.Width, this.ClientSize.Height);

this.Topthis.Left是在自定义控件内绘制图像的左上角。

我认为它可以正常工作,但是当我移动图像时,它永远不会清除所有控件。我总是看到上一张图的一部分。

我做错了什么?

谢谢!

4

1 回答 1

2

我认为您还没有清除控件的图像缓冲区。您只清除了后台缓冲区。在 2 个 DrawImage 调用之间试试这个:

e.Graphics.Clear(Color.White);

这应该首先清除任何剩余的图像。


或者,您可以重写它,以便将所有内容绘制到后台缓冲区,然后将后台缓冲区准确绘制到屏幕上 (0, 0),因此任何问题都将是由于后台缓冲区绘制逻辑而不是介于两者之间的某个位置。

像这样的东西:

Graphics gxOff; //Offscreen graphics
Brush backBrush;

if (m_bmpOffscreen == null) //Bitmap for doublebuffering
{
    m_bmpOffscreen = new Bitmap(ClientSize.Width, ClientSize.Height);
}

// draw back buffer
gxOff = Graphics.FromImage(m_bmpOffscreen);

gxOff.Clear(Color.White);

backBrush = new SolidBrush(Color.White);

gxOff.FillRectangle(backBrush, this.Left, this.Top,
    this.ClientRectangle.Width,
    this.ClientRectangle.Height);

//Draw some bitmap
gxOff.DrawImage(imageToShow, this.Left, this.Top, rectImageToShow, GraphicsUnit.Pixel);

//Draw from the memory bitmap
e.Graphics.DrawImage(m_bmpOffscreen,  0, 0);

base.OnPaint(e);

不确定这是否正确,但你应该明白。

于 2009-03-01T08:59:00.670 回答