This documentation is archived and is not being maintained.

Creating Mirrored Windows Forms and Controls

Windows Forms do not directly support mirroring. However, the Windows operating system itself does. You can therefore create a mirrored form in Visual Studio and set a Windows-defined extended style so that a Windows Form displayed in a mirrored fashion.

To create a mirrored Windows Form or control

  1. Create a new class for the form or control:
  2. Override the base class's CreateParams property. Because this is a read-only property, you only need to create the property's get method.
  3. In the overridden property, create an instance of the System.Windows.Forms.CreateParams class and set its ExStyle property.

    The ExStyle property is a bit field in which specific bits control various style attributes of the window.

  4. Using bit-wise OR operators, set the following bits in the CreateParams object's ExStyle property to true:
    • x400000 (WS_EX_LAYOUTRTL)   This bit setting specifies that the form or control will have a right-to-left layout (hence, will be mirrored). Set this for both forms and controls.
    • x100000 (WS_EX_NOINHERITLAYOUT)   This setting specifies that the form or control's layout is not inherited by child controls.
    • Mirroring for child controls of the mirrored form should be set using their RTL property or by other means.
      Tip   The constants used for the bit-wise settings are a Windows convention, defined in the winuser.h file.

The following example illustrates how to create a Windows form that is mirrored.

' Visual Basic
Public Class MirroredForm
    Inherits System.Windows.Forms.Form
    Const WS_EX_LAYOUTRTL = &H400000
    Const WS_EX_NOINHERITLAYOUT = &H100000
    Protected Overrides ReadOnly Property CreateParams() _
        As System.Windows.Forms.CreateParams
        Get
            Dim CP As System.Windows.Forms.CreateParams = _
                MyBase.CreateParams
            If Not MyBase.DesignMode() Then
               CP.ExStyle = CP.ExStyle Or WS_EX_LAYOUTRTL Or _
                   WS_EX_NOINHERITLAYOUT
            End If
            Return CP
        End Get
    End Property
    ' Rest of Form code here
End Class

// C#
const int WS_EX_LAYOUTRTL = 0x400000;
const int WS_EX_NOINHERITLAYOUT = 0x100000;
protected override CreateParams CreateParams
{
    get
    {
        CreateParams CP = base.CreateParams;
        if (! base.DesignMode)
            CP.ExStyle = CP.ExStyle | WS_EX_LAYOUTRTL 
                | WS_EX_NOINHERITLAYOUT;
        return CP;
    }
}
// Rest of form code here

In some situations, it is useful to create a property that allows you to turn mirroring on or off.

To create a property to control mirroring

  1. Create a Boolean property to control mirroring. For details about creating properties, see Property Procedures (Visual Basic .NET) or Properties Tutorial (Visual C# .NET).
  2. Optionally, include attributes that allow you to expose the property at design time. If you want to include attributes, you must also import the System.ComponentModel namespace. For details, see Attributes Overview.
  3. In the property's set method, call the base class's OnRightToLeftChanged method so that derived classes are notified of the change to the property.
  4. Override the base class's CreateParams property as described in the previous procedure. In the overridden property, set the extended Windows style if mirroring is true.

    The following example shows the relevant code from a mirrored TreeView control that implements a property called mirrored. The example includes some design-time attributes so that the mirrored property can be exposed in the Properties window.

    ' Visual Basic
    Imports System.ComponentModel
    Public Class MirroredTreeView
        Inherits System.Windows.Forms.TreeView
        Private _mirrored As Boolean = False
        <Description("Change to the right-to-left layout."), _
        DefaultValue(False), Localizable(True), _
        Category("Appearance"), Browsable(True)> _
        Public Property Mirrored() As Boolean
            Get
                Return _mirrored
            End Get
            Set(ByVal Value As Boolean)
                If _mirrored <> Value Then
                    _mirrored = Value
                    MyBase.OnRightToLeftChanged(EventArgs.Empty)
                End If
            End Set
        End Property
    
        Protected Overrides ReadOnly Property CreateParams() _
            As System.Windows.Forms.CreateParams
            Get
                Dim CP As System.Windows.Forms.CreateParams = _
                    MyBase.CreateParams
                If Mirrored Then
                    CP.ExStyle = CP.ExStyle Or &H400000
                End If
                Return CP
            End Get
        End Property
    
        ' Rest of control code here
    
    End Class
    
    // C#
    public class MirroredTreeView : System.Windows.Forms.TreeView
    {
        const int WS_EX_LAYOUTRTL = 0x400000;
        private bool _mirrored= false;
        [Description("Change to the right-to-left 
           layout."),DefaultValue(false), 
            Localizable(true),Category("Appearance"),Browsable(true)] 
        public bool Mirrored
        {
            get
            {
                return _mirrored;
            }
            set
            {
                if (_mirrored != value) 
                {
                    _mirrored = value;
                    base.OnRightToLeftChanged(EventArgs.Empty);
                }
            }
        }
    }
    protected override CreateParams CreateParams
    {
        get
        {
            CreateParams CP;
            CP = base.CreateParams;
            CP.ExStyle = CP.ExStyle | WS_EX_LAYOUTRTL ;
            return CP;
        }
    }
    // Rest of control code here
    

See Also

Globalizing Windows Forms | Localizing Applications | Bi-directional Support for Windows Applications | Windows Forms Inheritance | Implementing Mirror-Aware Controls for Windows Application with Visual Studio .NET (this link goes to the Microsoft.com Web site)

Show: