How do I suspend painting for a control and its children?

前端 未结 10 1932
小鲜肉
小鲜肉 2020-11-22 01:34

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 eno

10条回答
  •  广开言路
    2020-11-22 01:47

    Here is a combination of ceztko's and ng5000's to bring a VB extensions version that doesn't use pinvoke

    Imports System.Runtime.CompilerServices
    
    Module ControlExtensions
    
    Dim WM_SETREDRAW As Integer = 11
    
    ''' 
    ''' A stronger "SuspendLayout" completely holds the controls painting until ResumePaint is called
    ''' 
    ''' 
    ''' 
    
    Public Sub SuspendPaint(ByVal ctrl As Windows.Forms.Control)
    
        Dim msgSuspendUpdate As Windows.Forms.Message = Windows.Forms.Message.Create(ctrl.Handle, WM_SETREDRAW, System.IntPtr.Zero, System.IntPtr.Zero)
    
        Dim window As Windows.Forms.NativeWindow = Windows.Forms.NativeWindow.FromHandle(ctrl.Handle)
    
        window.DefWndProc(msgSuspendUpdate)
    
    End Sub
    
    ''' 
    ''' Resume from SuspendPaint method
    ''' 
    ''' 
    ''' 
    
    Public Sub ResumePaint(ByVal ctrl As Windows.Forms.Control)
    
        Dim wparam As New System.IntPtr(1)
        Dim msgResumeUpdate As Windows.Forms.Message = Windows.Forms.Message.Create(ctrl.Handle, WM_SETREDRAW, wparam, System.IntPtr.Zero)
    
        Dim window As Windows.Forms.NativeWindow = Windows.Forms.NativeWindow.FromHandle(ctrl.Handle)
    
        window.DefWndProc(msgResumeUpdate)
    
        ctrl.Invalidate()
    
    End Sub
    
    End Module
    

提交回复
热议问题