c#CF,WinForms和双缓冲区

时间:2009-02-22 13:58:06

标签: c# winforms compact-framework doublebuffered

我在表单上有一个带有PictureBox的CF 2.0应用程序。我想用鼠标移动移动PictureBox,我需要在表单中添加Double Buffer以避免闪烁。

我该怎么做?

谢谢!

1 个答案:

答案 0 :(得分:5)

你不需要双缓冲表格,你需要PB。这在CF中并不容易。但是,您可以创建自己的控件,PB非常简单。例如:

using System;
using System.Drawing;
using System.Windows.Forms;

public class MyPictureBox : Control {
  private Image mImage;
  public Image Image {
    get { return mImage; }
    set { mImage = value; Invalidate(); }
  }
  protected override void OnPaintBackground(PaintEventArgs pevent) {
    // Do nothing
  }
  protected override void OnPaint(PaintEventArgs e) {
    using (Bitmap bmp = new Bitmap(this.ClientSize.Width, this.ClientSize.Height)) {
      using (Graphics bgr = Graphics.FromImage(bmp)) {
        bgr.Clear(this.BackColor);
        if (mImage != null) bgr.DrawImage(mImage, 0, 0);
      }
      e.Graphics.DrawImage(bmp, 0, 0);
    }
    base.OnPaint(e);
  }
}

希望我没有使用CF中没有的东西......