在c#winform中设置面板边框的厚度 [英] set panel border thickness in c# winform
问题描述
我正在搜索,结果无法解决我的情况. 实际上,我有一个面板,并且我希望该面板的边框比Windows的边框厚. 我需要BorderStyle
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
BorderStyle.FixedSingle
更厚.. 之前谢谢
推荐答案
您必须使用一些自定义绘画来自定义自己的Panel
:
You have to customize your own Panel
with a little custom painting:
//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));
}
}
}
这是厚度为30
的面板的屏幕截图:
Here is the screen shot of panel with thickness of 30
:
注意:Rectangle
的大小是在绘图线的中间计算的,假设您绘制的线条的厚度为4
,则外部偏移量为2,内部偏移量为2
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.
我没有测试Hans
先生给出的情况,要解决该问题,只需为您的panel1
处理事件SizeChanged
,如下所示:
I didn't test the case given by Mr Hans
, to fix it simply handle the event SizeChanged
for your panel1
like this:
private void panel1_SizeChanged(object sender, EventArgs e){
panel1.Invalidate();
}
您还可以使用Reflection
设置ResizeRedraw = true
,而不必像上面这样处理SizeChanged
事件:
You can also setting ResizeRedraw = true
using Reflection
without having to handle the SizeChanged
event as above like this:
typeof(Control).GetProperty("ResizeRedraw", BindingFlags.NonPublic | BindingFlags.Instance)
.SetValue(panel1, true, null);
调整大小时,您可能会看到一点闪烁,只需添加以下代码即可为panel1启用doubleBuffered:
You may see a little flicker when resizing, just add this code to enable doubleBuffered for your panel1:
typeof(Panel).GetProperty("DoubleBuffered",
BindingFlags.NonPublic | BindingFlags.Instance)
.SetValue(panel1,true,null);
这篇关于在c#winform中设置面板边框的厚度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!