简体   繁体   English

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

[英]set panel border thickness in c# winform

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.实际上我有一个面板,我希望面板的边框比 Windows 给出的边框更厚。 I need BorderStyle我需要边框样式

BorderStyle.FixedSingle

thicker.. Thanks before更厚。。谢谢之前

You have to customize your own Panel with 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的面板的屏幕截图:

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

NOTE : The Size of Rectangle is 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 SizeChanged for your panel1 like this:我没有测试Hans先生给出的案例,要修复它,只需像这样为您的panel1处理SizeChanged事件:

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

You can also setting ResizeRedraw = true using Reflection without having to handle the SizeChanged event as above like this:您还可以使用Reflection设置ResizeRedraw = true而不必像上面这样处理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);

To create a panel with border I place a panel in a panel.要创建带边框的面板,我将面板放置在面板中。 The "border panel" has the background color of the wanted border color and a padding, while the padding size is the wanted border thickness . “边框面板”具有所需边框颜色和填充的背景颜色,而padding大小是所需边框thickness

The advantage of this solution is that there is no flickering and no problems with resize.这种解决方案的优点是没有闪烁,也没有调整大小的问题。

在此处输入图片说明

在此处输入图片说明

This can be very simple be created in the designer or in code behind.这可以非常简单地在设计器中或在代码后面创建。

Code behind:后面的代码:

Panel panel_Border = new Panel();
Panel panel_Embedded = new Panel();

panel_Border.BackColor = Color.Green;
panel_Border.Controls.Add(panel_Embedded);
// this is the border thickness
panel_Border.Padding = new System.Windows.Forms.Padding(6);
panel_Border.Size = new System.Drawing.Size(200, 100);
        
panel_Embedded.BackColor = System.Drawing.SystemColors.Control;
panel_Embedded.Dock = System.Windows.Forms.DockStyle.Fill;

Create a new, slightly larger panel and set the background colour to Black (or whatever).创建一个新的、稍大的面板并将背景颜色设置为黑色(或其他颜色)。 Place the original panel INSIDE the larger panel.将原始面板放在较大的面板内。

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

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