控件Group Box默认的边框的颜色是白色的,在很多时候显得不那么突出。但默认的属性列表里面并没有提供相应的接口。所以只能借助重绘事件。

网上很多都说使用 OnPaint 事件,但是我在事件列表中没找到,应该是隐藏得太深了(需要用 override 关键字来重写)。我这里直接使用了 Paint 事件,也可以达到其效果。

感谢:http://blog.csdn.net/haoduo123456789001/article/details/51083223

    public partial class TestForm : Form
{ public TestForm()
{
InitializeComponent(); this.groupBox1.Paint += groupBox_Paint;
this.groupBox2.Paint += groupBox_Paint;
} void groupBox_Paint(object sender, PaintEventArgs e)
{
GroupBox gBox = (GroupBox)sender; e.Graphics.Clear(gBox.BackColor);
e.Graphics.DrawString(gBox.Text, gBox.Font, Brushes.Red, , );
var vSize = e.Graphics.MeasureString(gBox.Text, gBox.Font);
e.Graphics.DrawLine(Pens.Red, , vSize.Height / , , vSize.Height / );
e.Graphics.DrawLine(Pens.Red, vSize.Width + , vSize.Height / , gBox.Width - , vSize.Height / );
e.Graphics.DrawLine(Pens.Red, , vSize.Height / , , gBox.Height - );
e.Graphics.DrawLine(Pens.Red, , gBox.Height - , gBox.Width - , gBox.Height - );
e.Graphics.DrawLine(Pens.Red, gBox.Width - , vSize.Height / , gBox.Width - , gBox.Height - ); 
} private void TestForm_Load(object sender, EventArgs e)
{ }
}

效果图:



方法二:

当然,如果你愿意,请看下面的利用 OnPaint 来实现的方法。

首先,需要创建一个自己的组件(类似于自定义控件):

添加后,切换至“代码视图”。

将继承关系,修改为:

    public partial class MyGroupBox : GroupBox  //Component
{
public MyGroupBox()
{
InitializeComponent();
} public MyGroupBox(IContainer container)
{
container.Add(this); InitializeComponent();
}
// 重写
}

然后重写 OnPaint() 方法:

        // 重写
protected override void OnPaint(PaintEventArgs e)
{
var vSize = e.Graphics.MeasureString(this.Text, this.Font); e.Graphics.Clear(this.BackColor);
e.Graphics.DrawString(this.Text, this.Font, new SolidBrush(this.ForeColor), , ); e.Graphics.DrawLine(Pens.Black, , vSize.Height / , , vSize.Height / );
e.Graphics.DrawLine(Pens.Black, vSize.Width + , vSize.Height / , this.Width - , vSize.Height / );
e.Graphics.DrawLine(Pens.Black, , vSize.Height / , , this.Height - );
e.Graphics.DrawLine(Pens.Black, , this.Height - , this.Width - , this.Height - );
e.Graphics.DrawLine(Pens.Black, this.Width - , vSize.Height / , this.Width - , this.Height - ); 
}

按 F6 生成一下,然后就可以在 工具栏里面 找到它,然后,你就不需要再用以前的 GroupBox 啦。一劳永逸

你甚至可以将 边框颜色 通过控件属性的方式自己把他公开出来。

    public partial class MyGroupBox : GroupBox//Component
{
private Color mBorderColor = Color.Black;
[Browsable(true),Description("边框颜色"),Category("自定义分组")]
public Color BorderColor
{
get { return mBorderColor; }
set { mBorderColor = value; }
} public MyGroupBox()
{
InitializeComponent();
} public MyGroupBox(IContainer container)
{
container.Add(this); InitializeComponent();
} // 重写
protected override void OnPaint(PaintEventArgs e)
{
var vSize = e.Graphics.MeasureString(this.Text, this.Font); e.Graphics.Clear(this.BackColor);
e.Graphics.DrawString(this.Text, this.Font, new SolidBrush(this.ForeColor), , );
Pen vPen = new Pen(this.mBorderColor); // 用属性颜色来画边框颜色
e.Graphics.DrawLine(vPen, , vSize.Height / , , vSize.Height / );
e.Graphics.DrawLine(vPen, vSize.Width + , vSize.Height / , this.Width - , vSize.Height / );
e.Graphics.DrawLine(vPen, , vSize.Height / , , this.Height - );
e.Graphics.DrawLine(vPen, , this.Height - , this.Width - , this.Height - );
e.Graphics.DrawLine(vPen, this.Width - , vSize.Height / , this.Width - , this.Height - ); 
}
}

[http://www.cnblogs.com/CUIT-DX037/]

最新文章

  1. eclipse 运行报java.lang.OutOfMemoryError: PermGen space解决方法
  2. WPF之TreeList的实现方法(一)
  3. 浅析Javascript
  4. Android 获取存储空间
  5. 制作自己的ros机器人(navigaion)前提--22
  6. JS认证Exchange
  7. Android(java)学习笔记73:线程组的概述和使用
  8. python数据类型以及模块的含义
  9. ASP.NET Core集成现有系统认证
  10. Java进阶篇设计模式之六 ----- 组合模式和过滤器模式
  11. vue路由传对象刷新会报错,数据丢失,用json字符串解决
  12. vue的计算属性与方法的不同
  13. 最新版本Zabbix3.4+Grafana5 整合部署
  14. Android学习笔记五:四大组件(转)
  15. C# 字符串中英文对齐
  16. 创建Jmeter中使用的jar包中的工具类方法
  17. java之yield(),sleep(),wait()区别详解-备忘笔记[转]
  18. (4.4)dbcc checkdb 数据页修复
  19. CGCS2000坐标系与其他坐标系间的差异和转换方法
  20. .net开发ActiveX控件

热门文章

  1. I - 一次元リバーシ / 1D Reversi(水题)
  2. 对接ebay,订单系统开发
  3. vue添加新属性不更新原因
  4. 洛谷P2709 BZOJ 3781 小B的询问 (莫队)
  5. 再谈hive-1.0.0与hive-1.2.1到JDBC编程忽略细节问题
  6. Job for postfix.service failed because the control process exited with error code. See "systemctl status postfix.service" and "journalctl -xe" for details.
  7. javascript 获取当前 URL 参数的两种方法
  8. JTAG与JLink说明
  9. Codeforces Round #532 (Div. 2)- A(思维)
  10. 浅谈c语言的线性表的基本操作———基于严蔚敏的数据结构(c语言版)