WinForm Flicker Flash Screen Solution

Source: Internet
Author: User

The development of WinForm programs often encounter a splash screen problem, which will create a poor user experience, it is necessary to properly solve this problem.

First of all, we need to find out the cause of the splash screen, for my current problems, the reasons are very varied.

The main reasons are: the use of a lot of components resulting in slow loading, the use of poor performance components (PictureBox, button, etc.).

So, regardless of the developer's level, Microsoft has dug a hole for us.

There are two reasons for a splash screen caused by too many controls:

1. windows sends a two message to the control. The first one is wm_erasebkgnd message, it will trigger onpaintbackground method, drawing control background .

The second is a wm_paint message, which triggers the OnPaint() method to draw the control interface. When the drawing is very slow, you will see the background before you see the interface, which causes flicker.

Solution: Set controlstyles Optimizeddoublebuffer and AllPaintingInWmPaint to true in the constructor method to fully enable double buffering .

The preferred way to enable double buffering is to set the control's DoubleBuffered property to True, which results in the same result, which is not explained in detail.

This. SetStyle (Controlstyles.allpaintinginwmpaint, true); This. SetStyle (Controlstyles.optimizeddoublebuffer, true);

2. A form with many controls takes a long time to draw. Especially when it uses poorly performing PictureBox, buttons, and so on. Once you add more than 50 controls, the splash screen will begin to become apparent.

The form draws its own background and leaves a "hole" in the place where the control is located. These "holes" are usually white when you use Opacity or transparency are black.

After each control is drawn, it is populated in the hole. This visual effect is ugly, and there is no ready-made solution in WinForm.

Double buffering does not resolve it because it applies only to a single control, not to a group of composite controls.

Solution: Set the ExStyle for CreateParams to ExStyle to 0x02000000 (ws_ex_composited).

After setting this value, XP (other systems seem to be able, not verified) will turn on double buffering of the form and its subforms .

protected override CreateParams CreateParams {       get {         createparams CP = base. CreateParams;         Cp. ExStyle |= 0x02000000;         return CP;       }  

Here are some areas to note: This approach does not speed up the drawing process. When drawing occurs, the form simply stays in an invisible state and pops up on the screen when the drawing is complete.

The opacity or transparencykey of the form will not work, and the outline of the form is visible, and an ugly black rectangle will appear in the drawing area.

To solve this problem, it is best to use a timer to increase the opacity value to 99% so that it is visible after drawing so that the user does not see the black rectangle.

public partial class Formdemo:form    {        private timer timer = null;        Public Formdemo ()        {            InitializeComponent ();            Timer = new Timer () {Interval = +};            Timer.tick + = new EventHandler (Timer_tick);            Base. Opacity = 0;            Timer.start ();        }        private void Timer_tick (object sender, EventArgs e)        {            if (this. Opacity >= 1)            {                timer.stop ();            }            else            {                base. Opacity + = 0.2;}}}    

Write so much first, and slowly add it!

WinForm Flicker Flash Screen Solution

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.