在 c# winform 中设置面板边框厚度

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19163809/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 14:16:41  来源:igfitidea点击:

set panel border thickness in c# winform

c#winformsborderformborderstylethickness

提问by rifleon

I have searching and the result cannot solve my case. Actually I have a panel and I want the panel have thicker border than Windows given. I need BorderStyle

我进行了搜索,结果无法解决我的情况。实际上我有一个面板,我希望面板的边框比 Windows 给出的边框更厚。我需要边框样式

BorderStyle.FixedSingle

thicker.. Thanks before

更厚。。谢谢之前

采纳答案by King King

You have to customize your own Panelwith a little custom painting:

您必须Panel使用一些自定义绘画来自定义自己的绘画:

//Paint event handler for your Panel
private void panel1_Paint(object sender, PaintEventArgs e){ 
  if(panel1.BorderStyle == BorderStyle.FixedSingle){
     int thickness = 3;//it's up to you
     int halfThickness = thickness/2;
     using(Pen p = new Pen(Color.Black,thickness)){
       e.Graphics.DrawRectangle(p, new Rectangle(halfThickness,
                                                 halfThickness,
                                                 panel1.ClientSize.Width-thickness,
                                                 panel1.ClientSize.Height-thickness));
     }
  }
}

Here is the screen shot of panel with thickness of 30:

这是厚度为 的面板的屏幕截图30

Screen shot of panel with border thickness of 30

边框厚度为 30 的面板的屏幕截图

NOTE: The Size of Rectangleis calculated at the middle of the drawing line, suppose you draw line with thickness of 4, there will be an offset of 2 outside and 2 inside.

注意: 的大小Rectangle是在绘制线的中间计算的,假设您绘制的线的厚度为4,则外部偏移量为 2,内部偏移量为 2。

I didn't test the case given by Mr Hans, to fix it simply handle the event SizeChangedfor your panel1like this:

我没有测试先生给出的情况下Hans,解决它简单地处理这个事件SizeChangedpanel1是这样的:

private void panel1_SizeChanged(object sender, EventArgs e){
   panel1.Invalidate();
}

You can also setting ResizeRedraw = trueusing Reflectionwithout having to handle the SizeChangedevent as above like this:

您还可以设置ResizeRedraw = trueusingReflection而不必SizeChanged像上面这样处理事件:

typeof(Control).GetProperty("ResizeRedraw", BindingFlags.NonPublic | BindingFlags.Instance)
               .SetValue(panel1, true, null);

You may see a little flicker when resizing, just add this code to enable doubleBuffered for your panel1:

调整大小时您可能会看到一些闪烁,只需添加此代码即可为您的 panel1 启用 doubleBuffered:

typeof(Panel).GetProperty("DoubleBuffered",
                          BindingFlags.NonPublic | BindingFlags.Instance)
             .SetValue(panel1,true,null);

回答by dik leatherdale

Create a new, slightly larger panel and set the background colour to Black (or whatever). Place the original panel INSIDE the larger panel.

创建一个新的、稍大的面板并将背景颜色设置为黑色(或其他颜色)。将原始面板放在较大的面板内。