我想让“巴拉兹·提汉伊”在这里做同样的事情:https://stackoverflow.com/a/9772020/8458887但使用TableLayoutPanel。我测试了他的代码一个工作。但当我改变了TableLayoutPanel的TextBox不工作。
图片:第一个是“Balazs Tihanyi”TextBox,第二个是我的TableLayoutPanel
图2:这是添加TextBox时TableLayoutPanel的外观
更改后的代码:
public class BorderedPanel : UserControl
{
TableLayoutPanel tableLayoutPanel;
public BorderedPanel()
{
tableLayoutPanel = new TableLayoutPanel()
{
BackColor = SystemColors.Window,
//AutoSize = true,
//CellBorderStyle = TableLayoutPanelCellBorderStyle.None, //Single
BorderStyle = BorderStyle.None, //FixedSingle
ColumnCount = 1,
RowCount = 1,
Location = new Point(-1, -1),
//Dock = DockStyle.Fill,
Anchor = AnchorStyles.Top | AnchorStyles.Bottom |
AnchorStyles.Left | AnchorStyles.Right
};
Control container = new ContainerControl()
{
Dock = DockStyle.Fill,
Padding = new Padding(-1)
};
container.Controls.Add(tableLayoutPanel);
//Controls.Add(tableLayoutPanel);
DefaultBorderColor = SystemColors.ControlDark;
FocusedBorderColor = Color.Red;
BackColor = DefaultBorderColor;
Padding = new Padding(1);
Size = tableLayoutPanel.Size;
}
public Color DefaultBorderColor { get; set; }
public Color FocusedBorderColor { get; set; }
//public override string Text
//{
// get { return textBox.Text; }
// set { textBox.Text = value; }
//}
protected override void OnEnter(EventArgs e)
{
BackColor = FocusedBorderColor;
base.OnEnter(e);
}
protected override void OnLeave(EventArgs e)
{
BackColor = DefaultBorderColor;
base.OnLeave(e);
}
//protected override void SetBoundsCore(int x, int y,
// int width, int height, BoundsSpecified specified)
//{
// base.SetBoundsCore(x, y, width, height, specified);
//}
}
如何使用代码:
public Form1()
{
InitializeComponent();
BorderedPanel borderedPanel = new BorderedPanel();
borderedPanel.Location = new Point(73, 150); //73, 150 //12, 10
borderedPanel.Size = new Size(319, 25);
Controls.Add(borderedPanel);
//borderedPanel.Controls.Add(txtPath);
1条答案
按热度按时间f0ofjuux1#
作为在面板的非工作区添加自定义边框的选项,您可以处理以下本机消息:
WM_NCCALCSIZE
:更改非工作区的大小WM_NCPAINT
:在非工作区上绘制。在下面的屏幕截图中,您可以看到一个带有
BorderColor
和BorderWidth
自定义属性的面板,其中的实现工作在从右到左模式和自动滚动模式下:你可以克隆或下载代码:
我已经在Panel border size and border color – Customize nonclient area文章中解释了更多关于处理消息的细节,但在这里你也可以看到代码: