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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/126876/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 10:03:52  来源:igfitidea点击:

How do I disable updating a form in Windows Forms?

.netwinforms

提问by Tomas Pajonk

During a complicated update I might prefer to display all the changes at once. I know there is a method that allows me to do this, but what is it?

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

回答by gil

I think this.SuspendLayout() & ResumeLayout() should do it

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

回答by Magnus Johansson

I don't find SuspendLayout()and ResumeLayout()do what you are asking for. LockWindowsUpdate()mentioned by moobaadoes the trick. However, LockWindowUpdateonly works for one window at a time.

我没有找到SuspendLayout()ResumeLayout()做你所要求的。 LockWindowsUpdate()moobaa 提到的诀窍。但是,一次 LockWindowUpdate仅适用于一个窗口。

You can also try this:

你也可以试试这个:

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

回答by moobaa

You can use the old Win32LockWindowUpdatefunction:

您可以使用旧的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);
}

回答by Romain Verdier

Most complex third-party Windows Forms components have BeginUpdateand EndUpdatemethods or similar, to perform a batch of updates and thendrawing the control. At the form level, there is no such a thing, but you could be interested by enabling Double buffering.

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

回答by Eduardo Campa?ó

You can use SuspendLayout and ResumeLayout methods in the form or controls while updating properties. If you're binding data to controls you can use BeginUpdate and EndUpdate methods.

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

回答by JPrescottSanders

SuspendLayout will help performance if the updates involve changes to controls and layout: MSDN

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