2

Using the WS_EX_LAYOUTRTL flag is causing black bars to be visible on the edges of controls. The black bars are on the left edge of each button. How can the black bars be prevented while still using the WS_EX_LAYOUTRTL flag?

black bars on edges of controls

public class MyForm : Form {

    Button btn1 = new Button { Text = "Button1" };
    ComboBox combo1 = new ComboBox();
    Button btn2 = new Button { Text = "Button2" };

    public MyForm() : base() {
        Size = new Size(600, 100);
        StartPosition = FormStartPosition.CenterScreen;
        var mc = new MyControl();

        int x = 0;
        btn1.Location = new Point(x, 0);
        x += btn1.Size.Width + 10;
        combo1.Location = new Point(x, 0);
        x += combo1.Size.Width + 10;
        btn2.Location = new Point(x, 0);

        mc.Controls.AddRange(new Control[] { btn1, combo1, btn2 });
        Controls.Add(mc);
    }

    private class MyControl : UserControl {
        private const int WS_EX_LAYOUTRTL = 0x00400000;
        public MyControl() : base() {
            this.Dock = DockStyle.Top;
            this.AutoSize = true;
            this.BackColor = Color.LightPink;
        }

        protected override CreateParams CreateParams {
            get {
                var cp = base.CreateParams;
                cp.ExStyle |= WS_EX_LAYOUTRTL;
                return cp;
            }
        }
    }
}
Loathing
  • 5,109
  • 3
  • 24
  • 35
  • 1
    Add `/*WS_EX_NOINHERITLAYOUT*/ 0x100000` style as well. Look at this [RTL Panel](https://stackoverflow.com/a/35113453/3110834) which doesn't have such problem. – Reza Aghaei Feb 25 '21 at 18:00
  • This is how [`TabControl`](https://referencesource.microsoft.com/?WT.mc_id=DT-MVP-5003235#System.Windows.Forms/winforms/Managed/System/WinForms/TabControl.cs,386) or [`Form`](https://referencesource.microsoft.com/?WT.mc_id=DT-MVP-5003235#System.Windows.Forms/winforms/Managed/System/WinForms/Form.cs,2c200ff264f0120b,references) implemented RTL in their source code. – Reza Aghaei Feb 25 '21 at 18:07
  • @RezaAghaei Adding `WS_EX_NOINHERITLAYOUT` fixed the problem. Thanks! – Loathing Feb 25 '21 at 18:48

1 Answers1

2

Add WS_EX_NOINHERITLAYOUT (0x100000) style as well. This is how containers like TabControl or Form implemented RTL in their source code.

For example:

using System;
using System.ComponentModel;
using System.Windows.Forms;
public class ExPanel : Panel
{
    const int WS_EX_LAYOUTRTL = 0x400000;
    const int WS_EX_NOINHERITLAYOUT = 0x100000;
    private bool rightToLeftLayout = false;

    [Localizable(true)]
    public bool RightToLeftLayout
    {
        get { return rightToLeftLayout; }
        set
        {
            if (rightToLeftLayout != value)
            {
                rightToLeftLayout = value;
                this.RecreateHandle();
            }
        }
    }
    protected override CreateParams CreateParams
    {
        get
        {
            CreateParams CP;
            CP = base.CreateParams;
            if (this.RightToLeftLayout &&
                this.RightToLeft == System.Windows.Forms.RightToLeft.Yes)
                CP.ExStyle = CP.ExStyle | WS_EX_LAYOUTRTL | WS_EX_NOINHERITLAYOUT;
            return CP;
        }
    }
}
Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398