【问题标题】:WS_EX_LAYOUTRTL causing black bars on edges of controlsWS_EX_LAYOUTRTL 导致控件边缘出现黑条
【发布时间】:2021-02-25 17:47:05
【问题描述】:

使用WS_EX_LAYOUTRTL 标志会导致控件边缘出现黑条。黑条位于每个按钮的左边缘。如何在仍然使用WS_EX_LAYOUTRTL 标志的同时防止出现黑条?

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;
            }
        }
    }
}

【问题讨论】:

  • 也添加/*WS_EX_NOINHERITLAYOUT*/ 0x100000 样式。看看这个RTL Panel没有这个问题。
  • 这就是TabControlForm 在其源代码中实现RTL 的方式。
  • @RezaAghaei 添加WS_EX_NOINHERITLAYOUT 解决了这个问题。谢谢!

标签: c# winforms .net-4.5


【解决方案1】:

同时添加WS_EX_NOINHERITLAYOUT (0x100000) 样式。这就是 TabControlForm 等容器在其源代码中实现 RTL 的方式。

例如:

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;
        }
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-11-02
    • 2014-01-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-17
    • 1970-01-01
    相关资源
    最近更新 更多