Windows Forms control hosted in a WindowsFormsHost flickers when resized

Article ID: 969728
Expand all | Collapse all
Source: Microsoft Support

RAPID PUBLISHING

RAPID PUBLISHING ARTICLES PROVIDE INFORMATION DIRECTLY FROM WITHIN THE MICROSOFT SUPPORT ORGANIZATION. THE INFORMATION CONTAINED HEREIN IS CREATED IN RESPONSE TO EMERGING OR UNIQUE TOPICS, OR IS INTENDED SUPPLEMENT OTHER KNOWLEDGE BASE INFORMATION.

Action

A Windows Forms control that is hosted in a System.Windows.Forms.Integration.WindowsFormsHost control will flicker when resized. 

Cause



When a WindowsFormsHost control is resized, it calls the SetWindowPos function with the SWP_NOCOPYBITS flag to resize the hosted Windows Forms control. This flag causes the Windows Forms control to repaint its entire area.

Most of Windows Forms controls paint the background using the control's background color before painting the foreground. Repainting the entire control can result in flickering.

Resolution



Override WindowsFormsHost's OnWindowPositionChanged as shown below:

class NoFlickerWindowsFormsHost : WindowsFormsHost
{
    const uint SWP_NOZORDER = 0x0004;
    const uint SWP_NOACTIVATE = 0x0010;
    const uint SWP_ASYNCWINDOWPOS = 0x4000;

    [DllImport("user32.dll")]
    extern static bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags);

    protected override void OnWindowPositionChanged(Rect rcBoundingBox)
    {
        if (Handle != IntPtr.Zero)
        {
            SetWindowPos(Handle,
                IntPtr.Zero,
                (int)rcBoundingBox.X,
                (int)rcBoundingBox.Y,
                (int)rcBoundingBox.Width,
                (int)rcBoundingBox.Height,
                SWP_ASYNCWINDOWPOS
                | SWP_NOZORDER
                | SWP_NOACTIVATE);
        }
    }
}

The TextBox control flickers even without the SWP_NOCOPYBITS flag because the control repaints the entire window whenever it is resized. Use RichTextBox instead to avoid the flickering.

DISCLAIMER

MICROSOFT AND/OR ITS SUPPLIERS MAKE NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY, RELIABILITY OR ACCURACY OF THE INFORMATION CONTAINED IN THE DOCUMENTS AND RELATED GRAPHICS PUBLISHED ON THIS WEBSITE (THE “MATERIALS”) FOR ANY PURPOSE. THE MATERIALS MAY INCLUDE TECHNICAL INACCURACIES OR TYPOGRAPHICAL ERRORS AND MAY BE REVISED AT ANY TIME WITHOUT NOTICE.

TO THE MAXIMUM EXTENT PERMITTED BY APPLICABLE LAW, MICROSOFT AND/OR ITS SUPPLIERS DISCLAIM AND EXCLUDE ALL REPRESENTATIONS, WARRANTIES, AND CONDITIONS WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING BUT NOT LIMITED TO REPRESENTATIONS, WARRANTIES, OR CONDITIONS OF TITLE, NON INFRINGEMENT, SATISFACTORY CONDITION OR QUALITY, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, WITH RESPECT TO THE MATERIALS.

Properties

Article ID: 969728 - Last Review: March 31, 2009 - Revision: 1.1
Keywords: 
kbrapidpub kbnomt KB969728

Give Feedback

 

Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from smallbusiness.support.microsoft.com