English 中文(简体)
如何为面板做出双重缓冲?
原标题:How to make double buffering for a panel?

我试图双倍缓冲来摆脱闪烁器,但重新绘制图像闪烁器。 我需要在新地点的酒吧里用一个周期来重新绘制图像, 这对我有用。 但是当重新绘制非常显著的闪烁器时, 请帮助 。

namespace CockroachRunning
{
    public partial class Form1 : Form
    {
        Random R = new Random();
        Semaphore s1 = new Semaphore(2, 4);
        Bitmap cockroachBmp = new Bitmap(Properties.Resources.cockroach, new Size(55, 50));
        List<Point> cockroaches = new List<Point>();
        public Form1()
        {
            InitializeComponent();
            this.DoubleBuffered = true;
            cockroaches.Add(new Point(18,13));
            Thread t1 = new Thread(Up);
            t1.Start();
        }
        public void Up()
        {
            while (true) 
            {
                s1.WaitOne();
                int distance = R.Next(1, 6);
                for (int i = 0; i < distance; i++)
                {
                    if (cockroaches[0].Y - 1 > -1)
                    {
                        cockroaches[0] = new Point(cockroaches[0].X, cockroaches[0].Y - 1);
                        panel1.Invalidate();                        
                    }
                }
                s1.Release();
                Thread.Sleep(100);
            }
        }
        private void panel1_Paint(object sender, PaintEventArgs e)
        {
            Image i = new Bitmap(panel1.ClientRectangle.Width, panel1.ClientRectangle.Height);
            Graphics g = Graphics.FromImage(i);
            Graphics displayGraphics = e.Graphics;
            g.DrawImage(cockroachBmp, cockroaches[0]);
            displayGraphics.DrawImage(i, panel1.ClientRectangle);
        }
    }
}
最佳回答

要摆脱闪烁, 我使用以下设置来配置控制行为 :

base.DoubleBuffered = true;

SetStyle(ControlStyles.AllPaintingInWmPaint, true);
SetStyle(ControlStyles.ResizeRedraw, true);
SetStyle(ControlStyles.UserPaint, true);
SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
UpdateStyles();

我在的构建器中称之为“http://msdn.microsoft.com/en-us/library/system.windows.forms.forms.control.aspx” rel = “nofollows.control.aspx” rel = “nofollows” {code> control -派生类。我不知道这是否也适用于表格, 但我可以想象它是否也适用于表格。

然后在 lese OnPaintBackground(PaintEventArgs e) (消除客户区域)和 leace OnPaint (PaintEventArgs e) (实际绘图) 下绘制。

问题回答

暂无回答




相关问题
Bring window to foreground after Mutex fails

I was wondering if someone can tell me what would be the best way to bring my application to the foreground if a mutex was not able to be created for a new instance. E.g.: Application X is running ...

How to start WinForm app minimized to tray?

I ve successfully created an app that minimizes to the tray using a NotifyIcon. When the form is manually closed it is successfully hidden from the desktop, taskbar, and alt-tab. The problem occurs ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

Handle DataTable.DataRow cell change event

I have a DataTable that has several DataColumns and DataRow. Now i would like to handle an event when cell of this DataRow is changed. How to do this in c#?

Apparent Memory Leak in DataGridView

How do you force a DataGridView to release its reference to a bound DataSet? We have a rather large dataset being displayed in a DataGridView and noticed that resources were not being freed after the ...

ALT Key Shortcuts Hidden

I am using VS2008 and creating forms. By default, the underscore of the character in a textbox when using an ampersand is not shown when I run the application. ex. "&Goto Here" is not ...

WPF-XAML window in Winforms Application

I have a Winforms application coded in VS C# 2008 and want to insert a WPF window into the window pane of Winforms application. Could you explain me how this is done.

热门标签