[英]Center a Panel within a Panel, which is in a TableLayoutPanel, in C#
所以基本上我有一个游戏板,由TableLayoutPanel代表。 TableLayoutPanel的每个单元格代表板上的空间并包含一个Panel。 每个面板都有一个标签,用于显示当前在该空间中的内容(例如角色或建筑物),其背景颜色代表空间所处的地形(例如,土地,水等)。 当玩家尝试移动角色时,该角色可以移动的每个可能空间将变为“突出显示”。 然后,玩家将双击其中一个突出显示的空格以将角色移动到那里。
为了突出显示空格,我将Panel添加到现有Panel(已经有一个Label的那个)中。
所以回顾一下,TableLayoutPanel - > Panel - > Label,Panel。
我遇到的主要问题是我不能让“高亮 - 面板”在其父面板中居中; 它始终以左上角为中心。 我希望能够看到父面板的部分BackColor,以便让玩家更容易决定去哪里。 因此,将Dock设置为Fill不是一种选择。 高亮显示面板略小于父面板,从而允许父面板的BackColor边缘可见。 是的,Anchor设置为None。
第二个问题是我无法更改任何标签的文本颜色。 我尝试使用“tableLayoutPanel.GetControlFromPosition(16,4).Controls [0] .ForeColor = Color.White;”尝试在初始化时或更直接地使用特定位置进行更改。 但似乎没什么用。
说实话,我对超复杂的黑客修复不感兴趣。 但如果有一些简单或我错过的东西,我真的很感激一些投入。 谢谢。
这是创建TableLayoutPanel,其中的Panel以及每个Panel中的标签的代码:
// Create TableLayoutPanel to hold Panels
tableLayoutPanel = new TableLayoutPanel()
{
RowCount = 1,
ColumnCount = 1,
AutoSize = true,
AutoSizeMode = AutoSizeMode.GrowAndShrink,
Location = new Point(12, 12),
Dock = DockStyle.Fill,
AutoScroll = true,
};
// Add tableLayoutPanel to the form
this.Controls.Add(tableLayoutPanel);
// Reset and add rows/columns + styles
tableLayoutPanel.RowStyles.Clear();
tableLayoutPanel.ColumnStyles.Clear();
for (int i = 0; i < rows; i++)
{
tableLayoutPanel.RowCount++;
tableLayoutPanel.RowStyles.Add(new RowStyle(SizeType.Percent, 50F));
}
tableLayoutPanel.RowCount--;
for (int j = 0; j < cols; j++)
{
tableLayoutPanel.ColumnCount++;
tableLayoutPanel.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 50F));
}
tableLayoutPanel.ColumnCount--;
// Add Panels to TableLayoutPanel
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
// Create new Panel
Panel space = new Panel()
{
BackColor = SystemColors.ActiveCaption,
BorderStyle = BorderStyle.FixedSingle,
Margin = new Padding(0),
Size = new Size(45, 45)
};
space.MouseClick += new MouseEventHandler(clickOnSpace);
// Create new Label
Label info = new Label()
{
Size = new Size(93, 93),
Text = "Info",
Dock = DockStyle.Fill,
TextAlign = ContentAlignment.MiddleCenter,
Enabled = false,
Font = new Font("Microsoft Sans Serif", 6),
ForeColor = Color.White
};
// Add Label to Panel
space.Controls.Add(info);
// Add Panel to TableLayoutPanel
tableLayoutPanel.Controls.Add(space, j, i);
}
}
以及创建高亮显示面板的代码:
// Highlight potential positions using possibleMoves
private void Highlight(List<Tuple<int, int>> possibleMoves, int remaining)
{
int r = 0;
int c = 0;
foreach (Tuple<int, int> pair in possibleMoves)
{
r = pair.Item1;
c = pair.Item2;
// If highlight-Panel doesn't already exist
if (tableLayoutPanel.GetControlFromPosition(c, r).Controls.Count == 1)
{
// Create a Panel to highlight the space
Panel highlight = new Panel()
{
Name = "highlight",
Size = new Size(30, 30),
BackColor = Color.Yellow,
Anchor = AnchorStyles.None
};
highlight.MouseDoubleClick += new MouseEventHandler(doubleClickOnSpace);
// Add highlight Panel to space Panel
tableLayoutPanel.GetControlFromPosition(c, r).Controls.Add(highlight);
// Bring highlight Panel to front
tableLayoutPanel.GetControlFromPosition(c, r).Controls[1].BringToFront();
}
}
}
...因此,将Dock设置为Fill不是一种选择。
事实上,我认为设置Dock
to Fill
确实是一个不错的选择!
看看这张图片。 这是一个有2列2行的TableLayoutPanel
。 每个单元格都包含一个Pannel
,其中包含一个包含Label
的Panel
。 尽管您期望所有控件的Dock
属性已设置为Fill
!
我告诉你第一个单元格的设置,你可以在其中看到Red
, Black
和White
。
该单元格包含:
红色区域,一个Panel
:
○ BackColor
属性设置为Red
○ Margin
属性设为全0
○ Padding
属性设置为全部5
○ Dock
属性设置为Fill
黑色区域, Panel
具有:
○ BackColor
属性设置为Black
○ Margin
属性设为全0
○ Padding
属性设置为全部5
○ Dock
属性设置为Fill
白色标签, Label
有:
○ BackColor
属性设置为White
○ Margin
属性设为全0
○ Padding
属性设置为全0
○ Dock
属性设置为Fill
其他单元格的结构完全相同,但Label
的BackColor
及其父级(如黑色的),为Transparent
,所以你看到最内层Panel
的BackColor
(就像红色的一样)。
注意
答案完全基于将Dock
属性设置为Fill
并为停靠控件设置合适的Padding
值。 但是还有另一个优雅的解决方案,用于将控件自动保持在容器的中心,这基于使用具有1行1列而不是面板的TableLayoutPanel
,然后将child的Anchor
属性设置为None
。 这样,子项将在TableLayoutPanel
水平和垂直居中。 你会发现这篇文章有用它:
所以我实际上达成了一种不太复杂的方法 我没有在Panel中使用Panel,而是选择保留原始Panel,并创建一个自定义Label,其外部边框的厚度和颜色可以更改。 这样我就不必跟踪太多的控件,而CustomLabel会显示所需的一切。
CustomLabel的代码:
public class CustomLabel : Label
{
// Constructors
// Default Constructor
public CustomLabel() : base() { }
public CustomLabel(bool drawBorder, int borderThickness, Color borderColor, Color textColor) : base()
{
if (drawBorder)
{
BorderThickness = borderThickness;
BorderColor = borderColor;
}
Size = new Size(36, 36);
Text = "Info";
Anchor = (AnchorStyles.Left | AnchorStyles.Right);
AutoSize = false;
TextAlign = ContentAlignment.MiddleCenter;
Enabled = false;
Font = new Font("Microsoft Sans Serif", 6);
ForeColor = TextColor;
BorderStyle = BorderStyle.FixedSingle;
Dock = DockStyle.Fill;
}
// Creates a border of specified thickness and color
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
if (BorderStyle == BorderStyle.FixedSingle)
{
int halfThickness = BorderThickness / 2;
using (Pen p = new Pen(BorderColor, BorderThickness))
{
e.Graphics.DrawRectangle(p, new Rectangle(halfThickness,
halfThickness,
ClientSize.Width - BorderThickness, ClientSize.Height - BorderThickness));
}
}
}
public int BorderThickness { get; set; }
public Color BorderColor { get; set; }
}
将CustomLabel添加到面板的代码:
// Add Panels to TableLayoutPanel
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
// Create new Panel
Panel space = new Panel()
{
Size = new Size(45, 45),
Dock = DockStyle.Fill,
Margin = new Padding(0),
ForeColor = Color.Red
};
space.MouseClick += new MouseEventHandler(MouseDownOnSpace);
CustomLabel info = new CustomLabel(false, 0, Color.Empty, Color.Red); // Create new CustomLabel
space.Controls.Add(info); // Add CustomLabel to Panel
tlp.Controls.Add(space, j, i); // Add Panel to TableLayoutPanel
}
}
调整CustomLabel的代码:
((CustomLabel)tlp.GetControlFromPosition(col, row).Controls[0]).BorderThickness = 6;
((CustomLabel)tlp.GetControlFromPosition(col, row).Controls[0]).BorderColor = Color.Yellow;
tlp.GetControlFromPosition(col, row).Controls[0].Text = tlp.GetControlFromPosition(col, row).Controls[0].Text; // Transfer space information
tlp.GetControlFromPosition(col, row).Refresh(); // Refresh Panel to show changes
这是最终结果:(如您所见,ForeColor仍然没有变化。)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.