c# - VC# 窗体闪烁

标签 c# windows winforms windows-7

这就是我想要做的。我正在制作一个仅使用 S.W.F 和 S.D 命名空间的游戏。当我使用间隔为 1000/30(30 帧)的计时器时,在它的滴答事件中我有一个电话InvokeGraphics()。一切都或多或少呈现得很好,除了椭圆是用 flickred 绘制的。我尝试使用双缓冲和 this.SetStyle(),但都失败了。这是代码:

public partial class MainForm : Form
{
    int x = 0;
    public MainForm()
    {

        InitializeComponent();

        var sz = SystemInformation.PrimaryMonitorSize;
        this.FormBorderStyle = FormBorderStyle.None;
        this.Size = sz;
        this.SetStyle(ControlStyles.OptimizedDoubleBuffer,true);

        Timer tmr = new Timer();
        tmr.Enabled = true;
        tmr.Interval = 1000/30;
        tmr.Tick += delegate(object sender, EventArgs e)
        { 
            x++;
            this.InvokePaint(this,new PaintEventArgs(this.CreateGraphics(),this.Bounds));
        };
    }
    protected override void OnKeyPress(KeyPressEventArgs e)
    {
        if((int)e.KeyChar == 27) Application.Exit();
        base.OnKeyPress(e);
    }
    protected override void OnPaint(PaintEventArgs e)
    {
        var g = e.Graphics;

        g.Clear(Color.Firebrick);
        // this ellipse flickrs
        g.FillEllipse(Brushes.Green,x,64,64,64);

        base.OnPaint(e);
    }
    protected override void OnMouseClick(MouseEventArgs e)
    {

        base.OnMouseClick(e);
    }

}

最佳答案

使用 this.CreateGraphics() 不会创建双缓冲绘画上下文。将窗体的 DoubleBuffered 属性设置为 true。并使用间隔为 32 毫秒的计时器强制刷新,仅在其 Tick 事件处理程序中调用 Invalidate()。

关于c# - VC# 窗体闪烁,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6726126/

相关文章:

c# - 使用反射(或其他方式)获取成员对象

c# - 在 ASP.NET Core 中嵌入 View 本地化

c# - 为 WinForm 面板添加垂直滚动条

c# - 当我在可编辑单元格中写入值时如何捕获 DataGridView 验证错误?

c# - 如何在 C# Windows 中获取文件大小、文件名、文件扩展名?

c# - Xamarin 可以处理 Android 上的振动加速度计吗?

windows - 批处理脚本变量无法正常工作

windows - 创建OpenGL上下文时如何选择特定的GPU

c++ - 屏幕截图特定窗口

c# - 将 docx 文件合并在一起,包括页眉、页脚和图片