如何在Windows窗体中禁用更新表单?

时间:2021-12-24 03:57:54

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?

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

6 个解决方案

#1


13  

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

我认为这个.SpendpendLayout()和ResumeLayout()应该这样做

#2


13  

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

我没有找到SuspendLayout()和ResumeLayout()做你要求的。 moobaa提到的LockWindowsUpdate()可以解决问题。但是,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();
    }
}

#3


5  

You can use the old Win32 LockWindowUpdate function:

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

#4


4  

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

大多数复杂的第三方Windows窗体组件都具有BeginUpdate和EndUpdate方法或类似方法,以执行一批更新然后绘制控件。在表单级别,没有这样的东西,但你可以通过启用双缓冲感兴趣。

#5


1  

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方法。

#6


0  

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

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

#1


13  

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

我认为这个.SpendpendLayout()和ResumeLayout()应该这样做

#2


13  

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

我没有找到SuspendLayout()和ResumeLayout()做你要求的。 moobaa提到的LockWindowsUpdate()可以解决问题。但是,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();
    }
}

#3


5  

You can use the old Win32 LockWindowUpdate function:

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

#4


4  

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

大多数复杂的第三方Windows窗体组件都具有BeginUpdate和EndUpdate方法或类似方法,以执行一批更新然后绘制控件。在表单级别,没有这样的东西,但你可以通过启用双缓冲感兴趣。

#5


1  

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方法。

#6


0  

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

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