【问题标题】:How do I disable updating a form in Windows Forms?如何禁用更新 Windows 窗体中的窗体?
【发布时间】:2008-09-24 12:37:33
【问题描述】:

在复杂的更新期间,我可能更喜欢一次显示所有更改。我知道有一种方法可以让我做到这一点,但它是什么?

【问题讨论】:

    标签: .net winforms


    【解决方案1】:

    我认为 this.SuspendLayout() & ResumeLayout() 应该这样做

    【讨论】:

      【解决方案2】:

      我没有找到 SuspendLayout()ResumeLayout() 做你要求的事情。 LockWindowsUpdate() mentioned by moobaa 可以解决问题。但是,LockWindowUpdate 一次只能用于一个窗口。

      你也可以试试这个:

      using System;
      using System.Windows.Forms;
      using Microsoft.Win32;
      using System.Runtime.InteropServices;
      
      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();
          }
      }
      

      【讨论】:

        【解决方案3】:

        你可以使用旧的Win32LockWindowUpdate函数:

        [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);
        }
        

        【讨论】:

        【解决方案4】:

        大多数复杂的第三方 Windows 窗体组件都有 BeginUpdateEndUpdate 方法或类似方法,以执行一批更新并然后绘制控件。在表单级别,没有这样的东西,但您可以通过启用Double buffering 来感兴趣。

        【讨论】:

          【解决方案5】:

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

          【讨论】:

          • BeginUpdate 和 EndUpdate 方法在哪个?表单没有它们。
          【解决方案6】:

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

          【讨论】:

            猜你喜欢
            • 2021-03-03
            • 2011-12-19
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2010-10-18
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多