c# CF, WinForms and double buffer

主宰稳场 提交于 2019-12-11 05:55:46

问题


I have a CF 2.0 app with a PictureBox on a Form. I want to move the PictureBox with mouse move and I need to add Double Buffer to the form to avoid flickering.

How can I do this?

Thanks!


回答1:


You don't need the Form double-buffered, you need the PB to be. That's not so easy to come by in CF. However, you could create your own control, PB is pretty simple. For example:

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

Hopefully, I didn't use stuff that isn't available in CF...



来源:https://stackoverflow.com/questions/574958/c-sharp-cf-winforms-and-double-buffer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!