【问题标题】:.NET C# - WinForm border.NET C# - WinForm 边框
【发布时间】:2011-03-29 18:02:47
【问题描述】:

我有一个没有边框(无边框)的 WinForm。如何在表单中添加 1px 黑色边框?

    public MainForm()
    {
        InitializeComponent();
        this.DoubleBuffered = true;
        Region = System.Drawing.Region.FromHrgn(CreateRoundRectRgn(0, 0, Width - 5, Height - 5, 10, 10)); // adjust these parameters to get the lookyou want.
    }


    [DllImport("Gdi32.dll", EntryPoint = "CreateRoundRectRgn")]
    private static extern IntPtr CreateRoundRectRgn
    (
         int nLeftRect, // x-coordinate of upper-left corner
         int nTopRect, // y-coordinate of upper-left corner
         int nRightRect, // x-coordinate of lower-right corner
         int nBottomRect, // y-coordinate of lower-right corner
         int nWidthEllipse, // height of ellipse
         int nHeightEllipse // width of ellipse
     );

我需要一个无边框的表单,但我想添加一个 1px 的边框。

【问题讨论】:

  • 我们能看到一些代码吗?最好是designer.cs

标签: c# .net winforms visual-studio


【解决方案1】:

在表单的Paint 事件处理程序中,添加以下代码:

private void Form1_Paint(object sender, PaintEventArgs e)
{
    e.Graphics.DrawRectangle(Pens.Black, new Rectangle(0, 0, Width - 1, Height - 1));
}

祝你好运!

【讨论】:

  • 这将在表单内绘制矩形,而不是边框​​
  • 是的,但要覆盖 OnPaint()。
  • @Stecya - 你如何分辨?
  • Enable Header,你会看到
  • @Stecya:问题出在哪里?,在表单中添加1 px Padding。会好的。
【解决方案2】:

您可以添加一个完全停靠的Panel,以及另一个完全停靠的Panel 作为子控件。设置外层Panel的内边距为1,外层Panel的背景色为黑色。

然后将内部Panel的背景色设置为SystemColors.Control

【讨论】:

  • @MehdiLAMRANI 简单吗?使用 nothing 与边框无关的控件的 2 个实例我根本不会称之为简单。愚蠢的?非常。至少使用 OnPaint 覆盖,您可以清楚地看到代码在做什么。
【解决方案3】:

如果你不想画画,

添加 4 个面板宽度或高度 2 或 4 和黑色背景颜色 在将它们分别停靠在顶部、右侧、底部、左侧的 4 个侧面后

        this.FormBorderStyle = FormBorderStyle.None;

        Panel pnlTop = new Panel() { Height = 4, Dock = DockStyle.Top, BackColor = Color.Green };
        this.Controls.Add(pnlTop);

        Panel pnlRight = new Panel() { Width = 4, Dock = DockStyle.Right, BackColor = Color.Green };
        this.Controls.Add(pnlRight);

        Panel pnlBottom = new Panel() { Height = 4, Dock = DockStyle.Bottom, BackColor = Color.Green };
        this.Controls.Add(pnlBottom);

        Panel pnlLeft = new Panel() { Width = 4, Dock = DockStyle.Left, BackColor = Color.Green };
        this.Controls.Add(pnlLeft);

你也可以改变他们的鼠标指针来调整图标的大小,你也可以在鼠标事件上写一些代码来调整表单的大小。

【讨论】:

    猜你喜欢
    • 2012-11-22
    • 1970-01-01
    • 2018-03-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多