C#Winform 双缓冲不知道如何下手,虚心求教~!

xggllc 2013-04-01 02:48:22


C# 求救:窗体上有N多按钮,当更换按钮上图片时,闪屏,肿么办?双缓存不知道如何下手...

网上找了一些例子,有很多不能用:

private void Form1_Load(object sender, EventArgs e)//这里写这些 对不对呀?肿么感觉效果不明显啊
{
this.SetStyle(ControlStyles.OptimizedDoubleBuffer | ControlStyles.UserPaint |
ControlStyles.AllPaintingInWmPaint,
true);
this.UpdateStyles();



还有在 OnPaint下面写的代码:这样写后 那个按钮图片更新得更慢了

protected override void OnPaint(PaintEventArgs e)
{

Rectangle rect = e.ClipRectangle;
Bitmap bufferimage = new Bitmap(this.Width, this.Height);
Graphics g = Graphics.FromImage(bufferimage);
//这中间是不是少了什么东西?那个显示在显示器上的怎么让它先绘制到bufferimage里面啊?
this.CreateGraphics().DrawImage(bufferimage, 0, 0);
}
...全文
259 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
PiKaQiuPiKaPi 2014-04-10
  • 打赏
  • 举报
回复
放到构造函数里面而不是load函数里面
xggllc 2013-04-02
  • 打赏
  • 举报
回复
C#中是不是有什么方法,把原本绘制到屏幕上的按钮Image,先绘制到内存中的贴图上,然后再把内存中贴图绘制到屏幕上。
xggllc 2013-04-02
  • 打赏
  • 举报
回复
先谢谢,上面两位兄弟。
是不是我哪里阐述的不清楚啊?
这个我已经纠结一天了....

一个窗体中有很多按钮,有一个事件触发是,一下更改这所有按钮Image,然后程序一运行,你可以看到那按钮上贴图它一个一个从右向左慢慢变的,我想要的是整个画面一下子出来,不要这样一个个出来。小弟新手请各位多多指教~!先谢谢啦~


引用 2 楼 bdmh 的回复:
OnPaint中绘制大图像会慢的,你可以在内存中先绘制I个bitmap,然后将这个bitmap作为控件的背景图
C# code?1234 Bitmap bmp = new Bitmap(100, 100); Graphics g = Graphics.FromImage(bmp); g.DrawStrin……
xggllc 2013-04-01
  • 打赏
  • 举报
回复
请问我这样写对么?

namespace Boundary032201
{
    public class cilpButton : Button  //这个是我照这你写的这个套上来的
    {                                 //这个按钮类是不是这样直接创建的?
        public cilpButton()
        {
            InitializeComponent();
            this.SetStyle(ControlStyles.OptimizedDoubleBuffer |ControlStyles.UserPaint |ControlStyles.AllPaintingInWmPaint,
                              true);
            this.UpdateStyles();
        }
        private IContainer components;

        private void InitializeComponent()
        {
            this.SuspendLayout();
            this.ResumeLayout(false);

        }
    }
    public partial class Form1 : Form
    {
      public Form1()
        {
            InitializeComponent();
          //我是在这里初始化 按钮上面的图片的,像下面这样
            btn_stop.Image = bmpNormal;
            btn_start.Image = bmpNormal;
          }
      //切换按钮图片是 在Click事件中 切换的
      
      }
//这样写是不是有问题啊?

引用 1 楼 gxingmin 的回复:
双缓存代码不是放在form里,是放在你要绘制对象所在的类里,看你的代码,双缓存应该放在button类的构造函数里 public class 你的按钮类:Button { public 你的按钮类() { InitializeComponent(); this.SetStyle(ControlStyles.OptimizedDoubleBuf……
bdmh 2013-04-01
  • 打赏
  • 举报
回复
OnPaint中绘制大图像会慢的,你可以在内存中先绘制I个bitmap,然后将这个bitmap作为控件的背景图

            Bitmap bmp = new Bitmap(100, 100);
            Graphics g = Graphics.FromImage(bmp);
            g.DrawString("sssss", this.Font, Brushes.Red,10,10);
            this.BackgroundImage = bmp;
gxingmin 2013-04-01
  • 打赏
  • 举报
回复
双缓存代码不是放在form里,是放在你要绘制对象所在的类里,看你的代码,双缓存应该放在button类的构造函数里 public class 你的按钮类:Button { public 你的按钮类() { InitializeComponent(); this.SetStyle(ControlStyles.OptimizedDoubleBuffer | ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint, true); this.UpdateStyles(); } }

110,502

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧