WS_EX_LAYOUTRTL 导致控件边缘出现黑条
WS_EX_LAYOUTRTL causing black bars on edges of controls
使用 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) style as well. This is how containers like TabControl or Form 实现的 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;
}
}
}
使用 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) style as well. This is how containers like TabControl or Form 实现的 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;
}
}
}