简体   繁体   中英

C# WinForms Controls Rendering

I have a WinForms application which uses four panels in one form to hold and show information, controls etc.. Those panels are hidden or shown depending on the button pressed on the form - I hope you get the idea :) The panels are transparent and the forms holds the background image.

Now to the problem - if the background of the form is an image the controls on a panel that changes it's state to shown need too much time too render - there is kind of a blink and you can see how the controls render one after another. Has anyone encountered this before?

ADDITIONAL INFO

  • the problem disappears when I fill the background with a solid color (not image!)
  • I already tried using different kinds of images (png, bmp, jpg, low res, small color palette etc. with no effect)
  • I really need the background image
  • I would really want to avoid converting to WPF - simply because I don't have too much time.

I will be grateful for any help.

add a panel on your form and Dock it to middle, Use your background image to this panel... and also try the following code

MainPanel.SuspendLayout();
panel1.Visible= true;
panel2.Visible= false;
MainPanel.ResumeLayout();

if your okay with win32 API, solution 1)

[DllImport("user32.dll")]
public static extern bool LockWindowUpdate(IntPtr hWndLock);

on button click:

try
{
      LockWindowUpdate(this.Handle);
      //code here
}
finally
{
      LockWindowUpdate(IntPtr.Zero);
}

solution 2) Use SendMessage() with WM_SETREDRAW (better one)

private const int WM_SETREDRAW      = 0x000B;
private const int WM_USER           = 0x400;
private const int EM_GETEVENTMASK   = (WM_USER + 59);
private const int EM_SETEVENTMASK   = (WM_USER + 69);

[DllImport("user32", CharSet = CharSet.Auto)]
private extern static IntPtr SendMessage(IntPtr hWnd, int msg, int wParam, IntPtr lParam);

IntPtr eventMask = IntPtr.Zero;

on button click:

try
{
      // Stop redrawing:
      SendMessage(panel1.Handle, WM_SETREDRAW, 0, IntPtr.Zero);
      // Stop sending of events:
      eventMask = SendMessage(panel1.Handle, EM_GETEVENTMASK, 0, IntPtr.Zero);

      // code here
}
finally
{
      // turn on events
      SendMessage(panel1.Handle, EM_SETEVENTMASK, 0, eventMask);
      // turn on redrawing
      SendMessage(panel1.Handle, WM_SETREDRAW, 1, IntPtr.Zero);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM