【问题标题】:Buttons are two pixels too small按钮太小了两个像素
【发布时间】:2019-11-13 05:38:15
【问题描述】:

我使用 Windows 窗体已经有一段时间了,并注意到按钮控件在每个方向上总是比我试图制作的小一个像素。

为了说明,下图中的TextBoxesButton 设置为完全相同的大小,但大小不同。

wrong size buttons 这是我用来生成按钮的代码:

public Form1() {

    InitializeComponent();

    this.Size = new Size(216, 239)

    TB1 = new TextBox();
    TB1.Multiline = true;
    TB1.Size = new Size(100, 100);
    TB1.Location = new Point(100, 0);
    Controls.Add(TB1);

    TB2 = new TextBox();
    TB2.Multiline = true;
    TB2.Size = new Size(100, 100);
    TB2.Location = new Point(0, 100);
    Controls.Add(TB2);

    TestButton = new Button();
    TestButton.Text = "sdfsdf";
    TestButton.Size = new Size(100, 100);
    TestButton.Location = new Point(100, 100);
    Controls.Add(TestButton);

}

从图片中您可以看到按钮周围有空白区域。我尝试更改 Control.MarginControl.Padding,但按钮周围的额外空间不受这些影响。

为了使按钮显示为 100x100(我想要的方式),您必须将其向上和向左移动一个像素,并使其宽和高两个像素。 (TextButton.Size = new Size(102, 102); TextButton.Location = new Point(99, 99);)

我想做的是让按钮的大小实际上是我设置的大小。由于我的程序中的按钮数量众多,手动增加每个按钮的大小是不可取的,我正在寻找一个更优雅的长期解决方案,我可以继续使用。

我试图围绕名为 MyButton 的按钮类创建一个包装类,但它不适用于多态性(解释如下):

class MyButton : Button {

    public MyButton() : base() {}

    public new Size Size {

        get;
        set {
            int width = value.Width + 2; // make it two wider
            int height = value.Height + 2; // make it two taller
            Size size = new Size(width, height);
            base.Size = size;
        }
    }

    public new Point Location {

        get;
        set {
            Console.WriteLine("running"); // used to make sure this is actually being run
            int x = value.X - 1; // move it one left
            int y = value.Y - 1; // move it one up
            Point point = new Point(x, y);
            base.Location = point;
        }
    }
}

当我创建一个 MyButton 对象并使用 myButtonObject.Size = ... 时,它可以完美地工作,并且按钮的大小和位置都可以解决。但是,在我的代码的另一个地方,我有一个函数,它接受一个 Control 对象作为输入,这里我的 MyButton 类的代码没有被使用。

MyButton btn1 = new MyButton();
btn1.Size = new Size(100, 100);
btn.Location = new Point(100, 100);
// ^ this works great and the button is the right size

public void ControlFormatter(Control control) {
    control.Size = new Size(100, 100);
    control.Location = new Point(100, 100);
}

MyButton btn2 = new MyButton();
ControlFormatter(btn2);
// ^ this doesn't work

使用我在MyButton.Location.Set 中输入的Console.WriteLine("running") 打印语句,我可以看出当control.LocationControlFormatter() 中被调用时,我编写的代码没有运行(我只能假设它正在使用默认Control.Location 属性,从而使按钮大小错误。

我想我在问两件事

  1. 是否有更简单/更好/更简洁的方法来使按钮尺寸合适?
  2. 我怎样才能让ControlFormatter() 使用MyButton.Size 而不是Control.Size

谢谢,我对 C# 还很陌生,所以很感激。

【问题讨论】:

  • 您没有将围绕某些控件绘制的焦点矩形计算为按钮控件。 TextBox 控件没有围绕其内部边缘绘制的焦点矩形。我建议将控件添加到 TableLayoutPanel,而不是尝试手动 定位它们。您可以使用子控件Margin 和容器的Padding 属性来平滑地分隔它们。
  • @Jimi 按钮的焦点矩形绘制在其边框内,而不是围绕控件本身。我认为 OP 描述的 2 像素差异实际上是由于视觉样式。如果禁用视觉样式并允许控件以经典 Win32 样式呈现,多余的间距就会消失。
  • @Bradley Smith 你看到的边界和实际的边界是两个不同的东西。当然,焦点矩形是在控件的边界内绘制的;当 Button 未被选中时,您看到的是内部边框。如果您设置Button.FlatStyle = Flat 并将焦点移入/移出按钮,它会更加明显。正常边框是透明的(与父母的颜色相同)。你可以用ControlPaint.DrawBorder修改它。

标签: c# winforms button


【解决方案1】:

我选择了在我的 ControlFormatter() 函数中测试控件是否是按钮的更快和更脏的修复方法。

public void ControlFormatter(Control control) {
    int width = 100;
    int height = 100;

    if (control is Button) {
        width -= 2;
        height -= 2;
    }

    control.Size = new Size(width, height);
    control.Position = new Point(); // you get the jist
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-02-09
    • 2020-06-04
    • 1970-01-01
    • 2010-10-05
    • 2011-06-12
    • 2014-07-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多