如何更改组框边框的颜色?
在 C#.NET 中,我尝试以编程方式更改组框中边框的颜色。
更新:在我们切换到.NET之前,当我在winforms系统上工作时,有人问了这个问题。
In C#.NET I am trying to programmatically change the color of the border in a group box.
Update: This question was asked when I was working on a winforms system before we switched to .NET.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
对 Jim Fell 代码的这种调整使边框对我来说更好了一些,但作为注释添加太长了
......
This tweak to Jim Fell's code placed the borders a little better for me, but it's too long to add as a comment
...
我已经实现了相同的边框,对于新手来说可能更容易理解:
我使用 TopLeft (0,7) 是因为我想尊重控件的边框,并围绕其文本绘制线条。
要获取有关坐标系的更多信息,请访问此处: https ://learn.microsoft.com/en-us/dotnet/framework/winforms/windows-forms-coordinates
我不知道,可能它可以帮助那些想要实现边界调整的人。
I have achieved same border with something which might be simpler to understand for newbies:
I used for TopLeft (0,7) because I want to respect the borders of the control, and draw the line about the its text.
To get more information about the coordinates system walk here : https://learn.microsoft.com/en-us/dotnet/framework/winforms/windows-forms-coordinates
I do not know, may be it helps someone looking to achieve this border adjustment thing.
我不确定这是否适用于所有情况,但多亏了这个线程,我们很快就以编程方式连接到 Paint 事件,使用:
干杯!
I'm not sure this applies to every case, but thanks to this thread, we quickly hooked into the Paint event programmatically using:
Cheers!
只需将任何对象(不仅仅是按钮)上的绘制操作设置为此方法即可绘制边框。
它仍然不会像原来那样漂亮和圆润,但它要简单得多。
Just set the paint action on any object (not just buttons) to this method to draw a border.
It still wont be pretty and rounded like the original, but it is much simpler.
FWIW,这是我使用的实现。 它是 GroupBox 的子项,但不仅允许设置 BorderColor,还允许设置边框的厚度和圆角的半径。 此外,您还可以设置 GroupBox 标签所需的缩进量,并使用负缩进从右侧缩进。
为了使其工作,您还必须扩展基本 Graphics 类(注意:这是从我在尝试创建圆角面板控件时在这里找到的一些代码派生的,但我找不到原始帖子链接到这里):
FWIW, this is the implementation I used. It's a child of GroupBox but allows setting not only the BorderColor, but also the thickness of the border and the radius of the rounded corners. Also, you can set the amount of indent you want for the GroupBox label, and using a negative indent indents from the right side.
To make it work, you also have to extend the base Graphics class (Note: this is derived from some code I found on here once when I was trying to create a rounded-corners Panel control, but I can't find the original post to link here):
基于前面的答案,一个更好的解决方案包括组框的标签:
您可能想要调整文本的 x/y,但对于我的使用来说,这是正确的。
Building on the previous answer, a better solution that includes the label for the group box:
You might want to adjust the x/y for the text, but for my use this is just right.
只需添加绘画事件即可。
Just add paint event.