-1

我在 Windows 窗体中使用以下代码来移动按钮。但是,按钮在移动时会闪烁。即使我使用的是 SmoothingMode.HighQuality 和 DoubleBuffer。

如何减少按钮的闪烁?

    public Form1()
    {
        InitializeComponent();

        this.DoubleBuffered = true;
        this.SetStyle(ControlStyles.DoubleBuffer | ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint, true);
        this.UpdateStyles();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        timer1.Enabled = true;
    }

    private void timer1_Tick(object sender, EventArgs e)
    {
        this.DoubleBuffered = true;
        if (button1.Location.X > 10 && button1.Location.X < 550)
        {
            Point osp = new Point(button1.Location.X + 1, button1.Location.Y);                
            button1.Location = osp;
        }
        else
        {
            Point osp = new Point(11, button1.Location.Y);
            button1.Location = osp;
        }  
     }
4

1 回答 1

0

尝试使用 Timer 的 Interval 属性来减少每个刻度之间的间隔;

private void Form1_Load(object sender, EventArgs e)
{
    timer1.Enabled = true;
    timer1.Interval = 400;
}

还要从timer1_tick方法中删除以下代码,因为您已经在表单的初始化中使用它

this.DoubleBuffered = true;
于 2012-10-17T18:59:27.900 回答