如何禁用Windows窗体中的窗体更新?

时间:2020-03-06 14:39:32  来源:igfitidea点击:

在复杂的更新过程中,我可能希望一次显示所有更改。我知道有一种方法可以执行此操作,但是它是什么呢?

解决方案

我认为this.SuspendLayout()和ResumeLayout()应该做到这一点

大多数复杂的第三方Windows窗体组件都具有BeginUpdateEndUpdate方法或者类似方法,以执行一批更新,然后绘制控件。在表单级别,没有这样的事情,但是我们可能会对启用Double缓冲感兴趣。

更新属性时,可以在窗体或者控件中使用SuspendLayout和ResumeLayout方法。如果要将数据绑定到控件,则可以使用BeginUpdate和EndUpdate方法。

如果更新涉及控件和布局的更改,则SuspendLayout将有助于提高性能:
MSDN

我们可以使用旧的Win32 LockWindowUpdate函数:

[DllImport("user32.dll")]
private static extern long LockWindowUpdate(long Handle);

try {
    // Lock Window...
    LockWindowUpdate(frm.Handle);
    // Perform your painting / updates...
} 
finally {
    // Release the lock...
    LockWindowUpdate(0);
}

我找不到SuspendLayout()和ResumeLayout()满足要求。
moobaa提到的LockWindowsUpdate()可以解决问题。但是,LockWindowUpdate一次仅适用于一个窗口。

我们也可以尝试以下操作:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.Win32;
using System.Runtime.InteropServices;

namespace WindowsTest
{
  public partial class Form1 : Form
  {
    [DllImport("user32.dll")]
    public static extern int SendMessage(IntPtr hWnd, Int32 wMsg, bool wParam, Int32 lParam);
    private const int WM_SETREDRAW = 11; 

    public Form1()
    {
      InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
      SendMessage(this.Handle, WM_SETREDRAW, false, 0);

      // Do your thingies here

      SendMessage(this.Handle, WM_SETREDRAW, true, 0);

      this.Refresh();
    }
  }
}