创建第一个的控件(用GDI+画一条线)
2006-06-10 01:28
603 查看
如何利用GDI+画一条线,并把它作成一个控件
用了VS那么久,总感觉缺少一个控件,就是一条线,你也许会说,可以用Label / GroupBox 代替,没错,我以前在用VB6中也是用GroupBox代替, 用VC++的人都知道,可以随意让线条漫天飞....
前一段时间学习写控件,顺便用C#写了一个简单的"线"控件,没有容器(GroupBox)及事件(Label)侦听功能,从本质上减少了系统开销.
不太熟悉操作,请按照如下步骤实现代码描述:
打开Vs.net2003,新建C# Windows项目,然后选择添加一个用户控件,删除设计器生成的代码,然后复制下面的代码,粘贴到你的项目中.然后选中"我的控件"选项卡中的 Line 控件,拖放到 Form1中便可调试.
using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
namespace YA.Sail.Controls
{
/// <summary>
/// Line 2006.3.13(C)Zouyi.net
/// </summary>
public class Line : System.Windows.Forms.UserControl
{
private System.ComponentModel.Container components = null;
private StringFormat m_format = new StringFormat ();
private SolidBrush m_brushForeText;
private SolidBrush m_brushBackText;
public Line()
{
InitializeComponent();
m_format.FormatFlags = StringFormatFlags.NoWrap;
//m_format.LineAlignment = StringAlignment.Center;
m_format.Trimming = StringTrimming.EllipsisCharacter;
Font = new Font ("宋体",9.0F);
m_brushForeText = new SolidBrush (Color.GhostWhite);
m_brushBackText = new SolidBrush (Color.DimGray);
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region 组件设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器
/// 修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
//
// Line
//
this.Name = "Line";
this.Size = new System.Drawing.Size(264, 24);
}
#endregion
#region 方法
protected override void OnPaint(PaintEventArgs e)
{
//这里就是绘制控件的地方了
//这里写的很差,沿用C的风格,变量名为 i,j,k, 呵呵,我已经看不懂了,考验初学者的时候
int j = 0;
if (m_Bolection) j=2;
for (int i=8; i<8+m_Number*2; i+=2)
{
e.Graphics.DrawLine(SystemPens.ControlDark, 0,i-1+j,this.Width ,i-1+j);
e.Graphics.DrawLine(SystemPens.ControlLightLight,0,i,this.Width ,i);
}
//优化文本以图形方式显示
//e.Graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
if (m_Text.Trim().Length > 0)
{
//绘制文本背景
int k = (int) e.Graphics.MeasureString (m_Text,Font).Width;
int h = (int)Font.Height;
e.Graphics.FillRectangle (new SolidBrush (this.BackColor),((int)m_TextLeft - 2),0,k,h);
//绘制文本
if (m_Bolection)
{
e.Graphics.DrawString (m_Text,this.Font,m_brushBackText,m_TextLeft,1,m_format);
e.Graphics.DrawString(m_Text,this.Font,m_brushForeText,m_TextLeft,0,m_format);
}
else
{
e.Graphics.DrawString(m_Text,this.Font,m_brushForeText,m_TextLeft,1,m_format);
e.Graphics.DrawString (m_Text,this.Font,m_brushBackText,m_TextLeft,0,m_format);
}
}
base.OnPaint (e);
}
#endregion
#region 属性
private float m_TextLeft = 15;
[CategoryAttribute("外观"),
DescriptionAttribute("设置要显示的文字的左边距"),
DefaultValueAttribute(15)]
public float Caption_Left
{
get { return m_TextLeft;}
set
{
if (value > 0)
m_TextLeft = value;
else
m_TextLeft = 15;
Invalidate();
}
}
private string m_Text = "Caption";
[CategoryAttribute("外观"),
DescriptionAttribute("设置要显示的文字"),
DefaultValueAttribute("Caption")]
public string Caption
{
get { return m_Text; }
set
{
m_Text = value;
Invalidate();
}
}
private int m_Number = 1;
[CategoryAttribute("外观"),
DescriptionAttribute("设置线的数目,最多允许20条线"),
DefaultValueAttribute(1)]
public int Number
{
get { return m_Number; }
set
{
if (value > 20 || value < 1)
value = 1;
m_Number = value;
Invalidate();
}
}
private bool m_Bolection = false;
[CategoryAttribute("外观"),
DescriptionAttribute("设置是否显示为凸起线"),
DefaultValueAttribute(false)]
public bool Bolection
{
get { return m_Bolection; }
set
{
m_Bolection =value;
Invalidate();
}
}
#endregion
}
}
用了VS那么久,总感觉缺少一个控件,就是一条线,你也许会说,可以用Label / GroupBox 代替,没错,我以前在用VB6中也是用GroupBox代替, 用VC++的人都知道,可以随意让线条漫天飞....
前一段时间学习写控件,顺便用C#写了一个简单的"线"控件,没有容器(GroupBox)及事件(Label)侦听功能,从本质上减少了系统开销.
不太熟悉操作,请按照如下步骤实现代码描述:
打开Vs.net2003,新建C# Windows项目,然后选择添加一个用户控件,删除设计器生成的代码,然后复制下面的代码,粘贴到你的项目中.然后选中"我的控件"选项卡中的 Line 控件,拖放到 Form1中便可调试.
using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
namespace YA.Sail.Controls
{
/// <summary>
/// Line 2006.3.13(C)Zouyi.net
/// </summary>
public class Line : System.Windows.Forms.UserControl
{
private System.ComponentModel.Container components = null;
private StringFormat m_format = new StringFormat ();
private SolidBrush m_brushForeText;
private SolidBrush m_brushBackText;
public Line()
{
InitializeComponent();
m_format.FormatFlags = StringFormatFlags.NoWrap;
//m_format.LineAlignment = StringAlignment.Center;
m_format.Trimming = StringTrimming.EllipsisCharacter;
Font = new Font ("宋体",9.0F);
m_brushForeText = new SolidBrush (Color.GhostWhite);
m_brushBackText = new SolidBrush (Color.DimGray);
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region 组件设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器
/// 修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
//
// Line
//
this.Name = "Line";
this.Size = new System.Drawing.Size(264, 24);
}
#endregion
#region 方法
protected override void OnPaint(PaintEventArgs e)
{
//这里就是绘制控件的地方了
//这里写的很差,沿用C的风格,变量名为 i,j,k, 呵呵,我已经看不懂了,考验初学者的时候
int j = 0;
if (m_Bolection) j=2;
for (int i=8; i<8+m_Number*2; i+=2)
{
e.Graphics.DrawLine(SystemPens.ControlDark, 0,i-1+j,this.Width ,i-1+j);
e.Graphics.DrawLine(SystemPens.ControlLightLight,0,i,this.Width ,i);
}
//优化文本以图形方式显示
//e.Graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
if (m_Text.Trim().Length > 0)
{
//绘制文本背景
int k = (int) e.Graphics.MeasureString (m_Text,Font).Width;
int h = (int)Font.Height;
e.Graphics.FillRectangle (new SolidBrush (this.BackColor),((int)m_TextLeft - 2),0,k,h);
//绘制文本
if (m_Bolection)
{
e.Graphics.DrawString (m_Text,this.Font,m_brushBackText,m_TextLeft,1,m_format);
e.Graphics.DrawString(m_Text,this.Font,m_brushForeText,m_TextLeft,0,m_format);
}
else
{
e.Graphics.DrawString(m_Text,this.Font,m_brushForeText,m_TextLeft,1,m_format);
e.Graphics.DrawString (m_Text,this.Font,m_brushBackText,m_TextLeft,0,m_format);
}
}
base.OnPaint (e);
}
#endregion
#region 属性
private float m_TextLeft = 15;
[CategoryAttribute("外观"),
DescriptionAttribute("设置要显示的文字的左边距"),
DefaultValueAttribute(15)]
public float Caption_Left
{
get { return m_TextLeft;}
set
{
if (value > 0)
m_TextLeft = value;
else
m_TextLeft = 15;
Invalidate();
}
}
private string m_Text = "Caption";
[CategoryAttribute("外观"),
DescriptionAttribute("设置要显示的文字"),
DefaultValueAttribute("Caption")]
public string Caption
{
get { return m_Text; }
set
{
m_Text = value;
Invalidate();
}
}
private int m_Number = 1;
[CategoryAttribute("外观"),
DescriptionAttribute("设置线的数目,最多允许20条线"),
DefaultValueAttribute(1)]
public int Number
{
get { return m_Number; }
set
{
if (value > 20 || value < 1)
value = 1;
m_Number = value;
Invalidate();
}
}
private bool m_Bolection = false;
[CategoryAttribute("外观"),
DescriptionAttribute("设置是否显示为凸起线"),
DefaultValueAttribute(false)]
public bool Bolection
{
get { return m_Bolection; }
set
{
m_Bolection =value;
Invalidate();
}
}
#endregion
}
}
相关文章推荐
- Swift基础教程(swift代码创建第一个控件之UILabel
- 用swift创建第一个控件UILabel
- 自己动手写客户端UI库——创建第一个控件
- Swift基础教程(swift代码创建第一个控件之UILabel)
- swift中来创建一些常用控件。跟我们的第一个swift应用
- 创建的第一个Listbox 控件
- 创建您的第一个 Word 应用程序级外接程序
- 用Maven创建第一个web项目(2)servlet演示
- aspx页面中动态创建控件
- 解决“从不是创建控件的线程访问它”问题。
- [翻译]WP7 QuickStart-第一篇-创建第一个Windows Phone应用程序
- 在DLL中动态创建控件应注意的。。
- 使用GDI+实现圆形进度条控件的平滑效果
- 为DataGrid创建自定义列控件(一)
- 线程间操作无效: 从不是创建控件"Form1"的线程访问它
- VC动态创建控件之CButton类
- 一步一步创建Visual Basic .NET 控件[附源码]
- 代码创建控件注意事项
- 线程间操作无效: 从不是创建控件“”的线程访问它~~~的解决方法~
- 创建第一个MVC项目