Resizing Controls When Resizing Controls for Windows Forms

I have a TableLayoutPanel that contains a dynamic number of controls inside a SplitterPanel. The user may want to resize the panel to fit these controls in order to avoid using the scroll bar. This creates jitter on the size of the container, as well as the controls in the container. Sometimes the parent container lags significantly behind mouse movement during resizing (up to 3 second delay).

Is there a way to prevent the controls from being redrawn while resizing the parent container, for example, hiding all elements during resizing or stopping the resize event that occurs during mousedrag, only fires on the onMouseUp event?

+1
c # winforms vsto
source share
3 answers

As Hans noted, SuspendLayout and ResumeLayout work well in this situation, while also pausing the drawing of the control for the container:

 public static class Win32 { public const int WM_SETREDRAW = 0x0b; [DllImport("user32.dll")] public static extern IntPtr SendMessage(IntPtr hWnd, int msg, IntPtr wParam, IntPtr lParam); public static void SuspendPainting(IntPtr hWnd) { SendMessage(hWnd, WM_SETREDRAW, (IntPtr)0, IntPtr.Zero); } public static void ResumePainting(IntPtr hWnd) { SendMessage(hWnd, WM_SETREDRAW, (IntPtr)1, IntPtr.Zero); } } 

Then you can resize the events:

 private void Form1_ResizeBegin(object sender, EventArgs e) { tableLayoutPanel1.SuspendLayout(); } private void Form1_ResizeEnd(object sender, EventArgs e) { Win32.SuspendPainting(tableLayoutPanel1.Handle); tableLayoutPanel1.ResumeLayout(); Win32.ResumePainting(tableLayoutPanel1.Handle); this.Refresh(); } 
+3
source share

For me, this worked to make TableLayoutPanel double-buffered, then I didn't even need to pause the layout. I found a solution in this thread: How to avoid flickering in a TableLayoutPanel in C # .net using a class with two TableLayoutPanel buffers from this site: https://www.richard-banks.org/2007/09/how-to-create- flicker-free.html

After that, all I had to do was to restore the project and restart Visual Studio a couple of times (to get InitializeComponent () to work), go to the .Designer.cs file and change System.Windows. Class Forms.TableLayoutPanel for the new DBLayoutPanel class. To go into the Designer file and change the class, this helped me save time because I already had many controls inside the TableLayoutPanel.

+1
source share

You can hook up the parent container's resize event and set the e.Handled or e.Cancel property to true, and then manually rewind onMouseUp.

0
source share

All Articles