使用Paint事件重画GroupBox来修改边框颜色
2014-01-02 11:37
531 查看
在.Net下进行WinForm开发,GroupBox是经常要用到的一个控件。但是GroupBox自身的边框是灰白色的,其样式很难令开发者满意。在不借用第三方控件的情况下,通过其的Paint事件对GroupBox进行重画,也可以很方便的修改其边框颜色/样式。
简要说一下实现思路。首先用Clear方法清除GroupBox的显示,接着再用合适的样式把GroupBox画出来。把GroupBox拆分为4个圆角,一行标题文字和数段直线等元素,把这些元素摆放在合适的位置,拼凑出GroupBox的外框。定义4个Rectangle(用于后面的画圆角),确定它们的大小和位置;然后在确切的位置上画4个弧段,按圆角在GroupBox上的位置确定其角度;接着在对应的位置画出GroupBox标题和几条直线,就大功告成了。只需简单修改代码中的设置颜色语句,便可以完成GroupBox边框颜色的更换了。
using System;
usingSystem.Collections.Generic;
usingSystem.ComponentModel;
usingSystem.Data;
usingSystem.Drawing;
usingSystem.Drawing.Drawing2D;
usingSystem.Text;
usingSystem.Windows.Forms;
namespaceWindowsApplication2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void groupBox1_Paint(objectsender, PaintEventArgs e
{
e.Graphics.Clear(groupBox1.BackColor);
Rectangle Rtg_LT = newRectangle();
Rectangle Rtg_RT = newRectangle();
Rectangle Rtg_LB = newRectangle();
Rectangle Rtg_RB = newRectangle();
Rtg_LT.X = 0; Rtg_LT.Y = 7; Rtg_LT.Width= 10; Rtg_LT.Height = 10;
Rtg_RT.X = e.ClipRectangle.Width -11; Rtg_RT.Y = 7; Rtg_RT.Width = 10; Rtg_RT.Height = 10;
Rtg_LB.X = 0; Rtg_LB.Y =e.ClipRectangle.Height - 11; Rtg_LB.Width = 10; Rtg_LB.Height = 10;
Rtg_RB.X = e.ClipRectangle.Width - 11;Rtg_RB.Y = e.ClipRectangle.Height - 11; Rtg_RB.Width = 10; Rtg_RB.Height =10;
Color color = Color.FromArgb(51,94, 168);
Pen Pen_AL = new Pen(color,1);
Pen_AL.Color = color;
Brush brush = newHatchBrush(HatchStyle.Divot, color);
e.Graphics.DrawString(groupBox1.Text, groupBox1.Font, brush, 6, 0);
e.Graphics.DrawArc(Pen_AL, Rtg_LT,180, 90);
e.Graphics.DrawArc(Pen_AL, Rtg_RT,270, 90);
e.Graphics.DrawArc(Pen_AL, Rtg_LB,90, 90);
e.Graphics.DrawArc(Pen_AL, Rtg_RB,0, 90);
e.Graphics.DrawLine(Pen_AL, 5, 7,6, 7);
e.Graphics.DrawLine(Pen_AL,e.Graphics.MeasureString(groupBox1.Text, groupBox1.Font).Width + 3, 7,e.ClipRectangle.Width - 7, 7);
e.Graphics.DrawLine(Pen_AL, 0, 13,0, e.ClipRectangle.Height - 7);
e.Graphics.DrawLine(Pen_AL, 6,e.ClipRectangle.Height - 1, e.ClipRectangle.Width - 7, e.ClipRectangle.Height -1);
e.Graphics.DrawLine(Pen_AL,e.ClipRectangle.Width - 1, e.ClipRectangle.Height - 7, e.ClipRectangle.Width -1, 13);
}
}
} 运行效果图如下:
代码稍显繁琐,但也很好的实现了修改GroupBox边框样式/颜色的效果。
简要说一下实现思路。首先用Clear方法清除GroupBox的显示,接着再用合适的样式把GroupBox画出来。把GroupBox拆分为4个圆角,一行标题文字和数段直线等元素,把这些元素摆放在合适的位置,拼凑出GroupBox的外框。定义4个Rectangle(用于后面的画圆角),确定它们的大小和位置;然后在确切的位置上画4个弧段,按圆角在GroupBox上的位置确定其角度;接着在对应的位置画出GroupBox标题和几条直线,就大功告成了。只需简单修改代码中的设置颜色语句,便可以完成GroupBox边框颜色的更换了。
using System;
usingSystem.Collections.Generic;
usingSystem.ComponentModel;
usingSystem.Data;
usingSystem.Drawing;
usingSystem.Drawing.Drawing2D;
usingSystem.Text;
usingSystem.Windows.Forms;
namespaceWindowsApplication2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void groupBox1_Paint(objectsender, PaintEventArgs e
{
e.Graphics.Clear(groupBox1.BackColor);
Rectangle Rtg_LT = newRectangle();
Rectangle Rtg_RT = newRectangle();
Rectangle Rtg_LB = newRectangle();
Rectangle Rtg_RB = newRectangle();
Rtg_LT.X = 0; Rtg_LT.Y = 7; Rtg_LT.Width= 10; Rtg_LT.Height = 10;
Rtg_RT.X = e.ClipRectangle.Width -11; Rtg_RT.Y = 7; Rtg_RT.Width = 10; Rtg_RT.Height = 10;
Rtg_LB.X = 0; Rtg_LB.Y =e.ClipRectangle.Height - 11; Rtg_LB.Width = 10; Rtg_LB.Height = 10;
Rtg_RB.X = e.ClipRectangle.Width - 11;Rtg_RB.Y = e.ClipRectangle.Height - 11; Rtg_RB.Width = 10; Rtg_RB.Height =10;
Color color = Color.FromArgb(51,94, 168);
Pen Pen_AL = new Pen(color,1);
Pen_AL.Color = color;
Brush brush = newHatchBrush(HatchStyle.Divot, color);
e.Graphics.DrawString(groupBox1.Text, groupBox1.Font, brush, 6, 0);
e.Graphics.DrawArc(Pen_AL, Rtg_LT,180, 90);
e.Graphics.DrawArc(Pen_AL, Rtg_RT,270, 90);
e.Graphics.DrawArc(Pen_AL, Rtg_LB,90, 90);
e.Graphics.DrawArc(Pen_AL, Rtg_RB,0, 90);
e.Graphics.DrawLine(Pen_AL, 5, 7,6, 7);
e.Graphics.DrawLine(Pen_AL,e.Graphics.MeasureString(groupBox1.Text, groupBox1.Font).Width + 3, 7,e.ClipRectangle.Width - 7, 7);
e.Graphics.DrawLine(Pen_AL, 0, 13,0, e.ClipRectangle.Height - 7);
e.Graphics.DrawLine(Pen_AL, 6,e.ClipRectangle.Height - 1, e.ClipRectangle.Width - 7, e.ClipRectangle.Height -1);
e.Graphics.DrawLine(Pen_AL,e.ClipRectangle.Width - 1, e.ClipRectangle.Height - 7, e.ClipRectangle.Width -1, 13);
}
}
} 运行效果图如下:
代码稍显繁琐,但也很好的实现了修改GroupBox边框样式/颜色的效果。
相关文章推荐
- 使用Paint事件重画GroupBox来修改边框颜色
- C# WinForm窗体控件GroupBox修改边框颜色控件
- EditText 使用细节(输入内容改变时的监听事件、透明背景、修改提示文字颜色、输入内容密文显示)
- MFC修改button的颜色、背景、边框
- 修改静态文本框的字体颜色和边框背景颜色
- 修改UINavigationBar (导航栏)上NavigationBarItem 的字体大小和颜色的使用方法
- EditText使用Selector设置选中与边框颜色
- Asp.net之使用GridView控件的RowCommand事件实现修改和删除
- ios使用 KVC 修改textField的placeholder的字体颜色和大小
- android ToolBar使用默认返回键,修改颜色
- 使用CSS修改HTML5 input placeholder颜色
- 易语言使用颜色选择器修改窗口背景颜色
- [Mapbox GL]使用按钮修改layer的颜色
- LISTBOX控件:vc修改列表框LISTBOX的背景颜色及字体颜色及其使用方法
- 使用SpannableString设置部分文字大小、颜色、超链接、点击事件
- MFC对话框如何使用工具栏并修改工具栏的背景颜色与自绘对话框统一(二)
- 在使用shape的同时,用代码修改shape的颜色属性
- 使用storyboard设置按钮边框属性(颜色,宽度,圆角)
- 修改CMyEdit(派生自CEdit)边框的颜色的方法
- 使用java代码修改背景图片、背景颜色