代码之家  ›  专栏  ›  技术社区  ›  MarkP

C#标签自动调整大小添加填充

  •  7
  • MarkP  · 技术社区  · 15 年前

    2 回复  |  直到 15 年前
        1
  •  12
  •   Leniel Maccaferri    15 年前

    当你只使用填充和边距的时候是没有办法的。这是默认行为。

    alt text

    在上面 Window 我已经定好了 Padding Margin [0,0,0,0] . 那5个像素还在。

    如果你设置 FlatStyle = System AutoSize = False 你可以得到:

    alt text

    窗口 你已经没有那5个像素了。

        2
  •  1
  •   Lee Richardson    13 年前

    好的,那么FlastStyle=System;AutoSize=false;然后设置一个属性来计算宽度,如下所示:

    public string LabelText
    {
        set
        {
            _label.Text = value;
            using (Graphics g = CreateGraphics()) {
                _label.Width = (int)g.MeasureString(_label.Text, _label.Font).Width;
            }
        }