BUG: TransparencyKey does not work for double-buffered controls in 32-bit color mode

Symptoms

You may have a Microsoft Windows Form with a user control that has the DoubleBuffer control style bit set to TRUE. Or, you may have a Microsoft Windows Forms control (such as a Button, a PictureBox, a CheckBox, or a Label) with the DoubleBuffer property that is automatically set. When you set the TransparencyKey property of the Windows Form to the value of the BackColor property, and then run the application, the Form appears transparent and the control appears in color.

Note The color is set in the BackColor property of the Windows Form.

Note You must have the display Color quality set to 32-bit color mode.

Resolution

To resolve this problem in a Microsoft Windows application with a Windows Forms control, follow these steps:
  1. Right-click Label1, and then click
    Properties.
  2. In the Properties window, set the
    FlatStyle property to System.
  3. On the Debug menu, click
    Start.

    You may notice that both the Form and the Windows Forms control are transparent. This problem is mentioned in the "More Information" section of this article.
To resolve the problem in a Windows application with a user control, follow these steps:
  1. In Solution Explorer, right-click
    UserControl1.vb and then click View Code.
  2. Replace the existing
    SetStyle(ControlStyles.DoubleBuffer,
    true)
    statement with the following statement:

    Visual Basic .NET or Visual Basic 2005 Code
     SetStyle(ControlStyles.DoubleBuffer, False)
    Visual C# .NET or Visual C# 2005 Code
    SetStyle(ControlStyles.DoubleBuffer,false);
  3. On the Debug menu, click
    Start.

    You may notice that both the Form and the user control are transparent. This problem is mentioned in the "More Information" section of this article.

Status

Microsoft has confirmed that this is a problem in the Microsoft products that are listed in the "Applies to" section.

More Information

Note Before you start the "Steps to Reproduce the Problem", follow these steps:
  1. Right-click Desktop, and then click
    Properties.
  2. Click the Settings tab.

    Make sure that Color quality is set to Highest (32 bit). Some computers may not support 32-bit color quality because the color quality depends on the computer video card.

Steps to Reproduce the Problem

Windows Form with Windows Forms Control

  1. In Microsoft Visual Studio .NET or in Microsoft Visual Studio 2005, create a new Windows application by using either Visual Basic .NET, Visual Basic 2005, Visual C# 2005, or Visual C# .NET.

    By default, Form1 is created.
  2. From the Toolbox, drag a
    Label control to Form1.
  3. Right-click Form1, and then click
    Properties.
  4. In the Properties window, select the
    BackColor property, and then click the Customtab.

    Select any color other than white.
  5. In the Properties window, set the
    TransparencyKey property to the same value as the
    BackColor property.
  6. On the Debug menu, click
    Start.

    You may see that the form is transparent. However, the label appears in the color that is set in the BackColor property.

Steps to Reproduce the Problem

Windows Form with a User Control

  1. In Visual Studio .NET or ijn Visual Studio 2005, create a new Windows Control Library by using either Visual Basic .NET, Visual Basic 2005, Visual C# 2005, or Visual C# .NET.

    By default,
    UserControl1 is created.
  2. On the View menu, click
    Code. In the Windows Form Designer generated code region, add the following statements in the constructor of UserControl1 after the
    InitializeComponent()
    statement:

    Visual Basic .NET or Visual Basic 2005 Code
    'These Control Style bits enable double-buffering.
    SetStyle(ControlStyles.UserPaint,True)
    SetStyle(ControlStyles.AllPaintingInWmPaint, True)
    SetStyle(ControlStyles.DoubleBuffer, True)
    Visual C# .NET or Visual C# 2005 Code
    //These Control Style bits enable double-buffering.
    SetStyle(ControlStyles.UserPaint,true);
    SetStyle(ControlStyles.AllPaintingInWmPaint, true);
    SetStyle(ControlStyles.DoubleBuffer, true);
  3. On the Build menu, click Build Solution.
  4. On the File menu, point to Add Project, and then click New Project. Under the
    Project Types section, select Visual Basic Projects. Under the Templates section, select
    Windows Application.

    Note In Visual Studio 2005, click Visual Basic under Project Types.

    By default,
    Form1 is created.
  5. From the Toolbox, drag a
    UserControl1 control to Form1.
  6. Right-click Form1, and then click
    Properties.
  7. In the Properties window, select the
    BackColor property, and then click the Customtab. Select any color other than white.
  8. In the Properties window, set the
    TransparencyKey property to the same value as the BackColor property.
  9. In Solution Explorer, right-click
    WindowsApplication1, and then click Set as StartUp Project.
  10. On the Debug menu, click
    Start.

    You may see that the form is transparent. However, the user control appears in the color that is set in the BackColor property.

References

For more information about the Form.TransparencyKey property and ControlStyles enumeration, visit the following Microsoft Web sites:
http://msdn2.microsoft.com/en-us/library/system.windows.forms.form.transparencykey(vs.71).aspx

http://msdn2.microsoft.com/en-us/library/system.windows.forms.controlstyles(vs.71).aspx
Propriedades

ID do Artigo: 820640 - Última Revisão: 11 de jul de 2008 - Revisão: 1

Comentários