首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在TableLayoutPanel中的面板中以C#居中

在TableLayoutPanel中的面板中以C#居中
EN

Stack Overflow用户
提问于 2016-08-16 14:18:42
回答 2查看 2.3K关注 0票数 4

所以基本上,我有一个游戏板,代表一个TableLayoutPanel。TableLayoutPanel的每个单元格表示板上的一个空格,并包含一个面板。每个面板都有一个标签来显示当前在该空间中的内容(例如,字符或建筑物),其BackColor表示空间是什么样的地形(例如土地、水等)。当玩家试图移动一个角色时,该角色可以移动的每一个可能的空间都会变成“突出显示”。然后,播放机将双击突出显示的空格之一,以将字符移动到那里。

为了突出显示一个空间,我在现有的面板(已经有标签的面板)中添加了一个Panel。

所以回顾一下,TableLayoutPanel -> Panel -> Label,Panel.

我遇到的主要问题是,我无法将“突出显示面板”集中在它的父面板中;它总是以左上角为中心。我希望能够看到部分的BackColor的父面板,以使玩家更容易决定去哪里。因此,将Dock设置为Fill不是一个选项。突出显示-面板比父面板略小,从而允许父面板的BackColor边缘可见。是的,锚被设置为无。

第二个问题是,我不能让任何标签的文本颜色改变。我尝试在初始化时或更直接地使用"tableLayoutPanel.GetControlFromPosition(16,4).Controls.ForeColor =Color.White“对其进行更改,但似乎没有什么效果。

老实说,我对超级复杂的黑客修复不感兴趣。但是,如果有一些简单的东西或我错过的东西,我会非常感谢一些投入。谢谢。

这是创建TableLayoutPanel、其中的面板和每个面板中的标签的代码:

代码语言:javascript
运行
复制
// 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);
    }
}

以及创建突出显示面板的代码:

代码语言:javascript
运行
复制
// 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();
        }
    }
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-08-31 12:55:25

所以我实际上找到了一个不那么复杂的方法。而不是在面板中有一个面板,我选择保留原来的面板,并创建一个自定义标签,它的外部边框的厚度和颜色可以改变。这样,我就不必跟踪太多的控件,CustomLabel会显示所需的一切。

CustomLabel代码:

代码语言:javascript
运行
复制
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添加到面板的代码:

代码语言:javascript
运行
复制
// 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的代码:

代码语言:javascript
运行
复制
((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仍然没有改变。)

票数 0
EN

Stack Overflow用户

发布于 2016-08-19 21:01:35

..。因此,将Dock设置为Fill不是一个选项。

事实上,我相信将Dock设置为Fill确实是一个不错的选择!

看看这张照片。它是一个有2列和2行的TableLayoutPanel。每个单元格都包含一个Pannel,它包含一个Panel,它包含一个Label。尽管您期望如此,但所有控件的Dock属性已设置为Fill

我告诉您第一个单元格的设置,您可以在其中看到RedBlackWhite颜色。

该单元格包含:

  • 红色区域,具有:Panel: 0 BackColor属性设置为Red 0 Margin属性设置为所有0 0 Padding属性设置为所有5 0 Dock属性设置为Fill
  • 黑色区域,具有:Panel: 0 BackColor属性设置为Black 0 Margin属性设置为所有0 0 Padding属性设置为所有5 0 Dock属性设置为Fill
  • 白色标签,具有:Label: 0 BackColor属性设置为White 0 Margin属性设置为所有0 0 Padding属性设置为所有0 0 Dock属性设置为Fill

其他单元格的结构完全相同,但是BackColor of Label及其父单元(如黑色的)与Transparent的结构完全相同,因此您可以看到最内部的PanelBackColor (比如红色的)。

Note

答案完全基于将Dock属性设置为Fill和为停靠控件设置合适的Padding值。但是,还有另一种优雅的解决方案,可以自动将控件保持在容器的中心,这是基于使用具有1行和1列的TableLayoutPanel,而不是面板,然后将子控件的Anchor属性设置为None。这样,子节点将在TableLayoutPanel中以水平和垂直方向居中。你会发现这篇文章很有用:

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38977383

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档