如何暂停控件及其子项的绘画? [英] How do I suspend painting for a control and its children?

查看:19
本文介绍了如何暂停控件及其子项的绘画?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个控件,我必须对其进行大量修改.当我这样做时,我想完全防止它重绘 - SuspendLayout 和 ResumeLayout 是不够的.如何暂停控件及其子项的绘制?

I have a control which I have to make large modifications to. I'd like to completely prevent it from redrawing while I do that - SuspendLayout and ResumeLayout aren't enough. How do I suspend painting for a control and its children?

推荐答案

在我之前的工作中,我们一直在努力让丰富的 UI 应用程序能够立即流畅地进行绘制.我们使用了标准的 .Net 控件、自定义控件和 devexpress 控件.

At my previous job, we struggled with getting our rich UI app to paint instantly and smoothly. We were using standard .Net controls, custom controls and devexpress controls.

经过大量的谷歌搜索和反射器使用后,我遇到了 WM_SETREDRAW win32 消息.这确实会在您更新控件时停止绘制,并且可以将 IIRC 应用到父/包含面板.

After a lot of googling and reflector usage, I came across the WM_SETREDRAW win32 message. This really stops controls drawing whilst you update them and can be applied, IIRC to the parent/containing panel.

这是一个非常非常简单的类,演示如何使用此消息:

This is a very very simple class demonstrating how to use this message:

class DrawingControl
{
    [DllImport("user32.dll")]
    public static extern int SendMessage(IntPtr hWnd, Int32 wMsg, bool wParam, Int32 lParam);

    private const int WM_SETREDRAW = 11; 
    
    public static void SuspendDrawing( Control parent )
    {
        SendMessage(parent.Handle, WM_SETREDRAW, false, 0);
    }

    public static void ResumeDrawing( Control parent )
    {
        SendMessage(parent.Handle, WM_SETREDRAW, true, 0);
        parent.Refresh();
    }
}

对此有更全面的讨论 - google for C# 和 WM_SETREDRAW,例如

There are fuller discussions on this - google for C# and WM_SETREDRAW, e.g.

C# 抖动

暂停布局

对于它可能关心的人来说,这是 VB 中的一个类似示例:

And to whom it may concern, this is a similar example in VB:

Public Module Extensions
    <DllImport("user32.dll")>
    Private Function SendMessage(ByVal hWnd As IntPtr, ByVal Msg As Integer, ByVal wParam As Boolean, ByVal lParam As IntPtr) As Integer
    End Function

    Private Const WM_SETREDRAW As Integer = 11

    ' Extension methods for Control
    <Extension()>
    Public Sub ResumeDrawing(ByVal Target As Control, ByVal Redraw As Boolean)
        SendMessage(Target.Handle, WM_SETREDRAW, True, IntPtr.Zero)
        If Redraw Then
            Target.Refresh()
        End If
    End Sub

    <Extension()>
    Public Sub SuspendDrawing(ByVal Target As Control)
        SendMessage(Target.Handle, WM_SETREDRAW, False, IntPtr.Zero)
    End Sub

    <Extension()>
    Public Sub ResumeDrawing(ByVal Target As Control)
        ResumeDrawing(Target, True)
    End Sub
End Module

这篇关于如何暂停控件及其子项的绘画?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆