简体   繁体   English

无边框Winform,边框为1px

[英]Borderless Winform with a 1px border

This might sound like a weird question but I have C# Winform that I set the FormBorderStyle to None. 这可能听起来像一个奇怪的问题,但我有C#Winform我将FormBorderStyle设置为None。 So far everything is good but I was wondering if there was a way to add like a 1px border on around my form ? 到目前为止一切都很好,但我想知道是否有一种方法可以在我的表单周围添加1px边框? I know I could do it by creating my own image but I was wondering if there was a more natural way of doing it. 我知道我可以通过创建自己的图像来实现它,但我想知道是否有一种更自然的方式。 Thanks 谢谢

I consider using an image, or creating unnecessary controls for something that is easily paintable using GDI+ a waste of resources. 我考虑使用一个图像,或者使用GDI +创建不必要的控件来轻松绘制,这会浪费资源。

I think the simplest solution is overriding the OnPaint method of your form and drawing the border yourself: 我认为最简单的解决方案是覆盖表单的OnPaint方法并自己绘制边框:

protected override void OnPaint(PaintEventArgs e)
{
    e.Graphics.DrawRectangle(Pens.Black, this.Bounds);
}

Of course, you may also use your own Pen with your own color and width. 当然,您也可以使用自己的Pen和自己的颜色和宽度。

Use padding 1;1;1;1 to your form and set a background color to your form, and put a panel to your form. 使用填充1; 1; 1; 1到您的表单并为您的表单设置背景颜色,并将一个面板放入您的表单。 Set white or other normal background color to the panel. 将白色或其他正常背景颜色设置为面板。 And set dock in parent controller. 并在父控制器中设置dock。 The background color of the form will act as a border. 表单的背景颜色将充当边框。

如何只将Panel (并设置它的边框)添加到表单中?

Thanks for the suggestions, I've decided to create 4 1px label and just toss on the edge on each side. 感谢您的建议,我决定创建4个1px标签,并在每边的边缘上折腾。 That way: 1. They are minding their own business on the side rather than taking up the whole middle if you use use a groupbox or panel. 这样:1。如果您使用组合框或面板,他们正在关注自己的业务,而不是占据整个中间位置。 2. You are able to choose change your border color. 2.您可以选择更改边框颜色。

There is no more natural or non natural ways to do it. 没有更自然 或非自然的方式来做到这一点。 It depends on what you want. 这取决于你想要什么。

If you put a background image on the form, you have to consider a fact that in order to be able to support resizable for you have to have resizable background images. 如果您在表单上放置背景图像,则必须考虑一个事实,即为了能够支持可调整大小,您必须具有可调整大小的背景图像。

If you simply draw on the background with a Pen or Brush , you can support also resizable form, but you have to work more if you want to do something cool, instead with image it's easier. 如果您只是使用PenBrush在背景上绘图 ,您也可以支持可调整大小的形式,但如果您想要做一些很酷的事情,您必须工作更多,而使用图像则更容易。

You can embed some control inside the form and with color's of them make a feeling of the border. 你可以在表单中嵌入一些控件,并用它们的颜色来表达边框的感觉。 Like control, you can use Panel , as suggested in comment, can use GroupBox that creates thin broder arround, or something else. 与控件一样,您可以使用Panel ,如注释中所建议的,可以使用GroupBox创建瘦的broder arround或其他东西。

I created this method, so you could easily set the borderposition, color and thickness. 我创建了这个方法,因此您可以轻松设置边框,颜色和厚度。

private void customBackgroundPainter(PaintEventArgs e, int linethickness = 2, Color linecolor = new Color(), int offsetborder = 6)
{
    Rectangle rect = new Rectangle(offsetborder, offsetborder, this.ClientSize.Width - (offsetborder * 2), this.ClientSize.Height - (offsetborder * 2));

    Pen pen = new Pen(new Color());
    pen.Width = linethickness;
    if (linecolor != new Color())
    {
        pen.Color = linecolor;
    }
    else
    {
        pen.Color = Color.Black;
    }

    e.Graphics.DrawRectangle(pen, rect);
}

You could use it in the OnPaintBackground likes this: 你可以在OnPaintBackground使用它喜欢这样:

protected override void OnPaintBackground(PaintEventArgs e)
{
    base.OnPaintBackground(e);
    customBackgroundPainter(
    e,
    linethickness: 3,
    linecolor: Color.DarkOrange,
    offsetborder: 5
    );
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM