第一步:
确定这个控件需要用来做什么的,我们想对这个控件进行什么样的操作,这个控件需要什么属性;
第二步:
明确我们要做的控件之后就要开始进行实质性的技术攻关了,就是你要定义好各种要做的方法,明确这些方法是否是技术可及的;
第三步:
方法实现阶段,将原先已经解决的技术关键点和应处理的各个小范围处理应用到整个工程;
以下我做的一个控件的代码:
///
///此控件达到的功能是可以随意控制控件的大小,所在容器的位置,并且此控件实现了
///在鼠标放上去的时候能够自动变大(需要改变大小可以自己再添加属性,本控件默认长宽增长30个像素),并且会产生标签显示控件的信息(可控)
///在鼠标离开后会自动变回原来默认的大小,并有画字画图功能(这部分是根据传入有限集合进行智能绘画功能,出入集合自行解决,本控件将其省略)
///
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
namespace AnalysisSystem.Object
{
public partial class DeviceControl : UserControl
{
#region 定义各种属性
private bool _showToolTip;//默认false
private bool _InControl;//鼠标是否放在控件上的标志位
//设置窗口的大小
private Size _SetSize;
public Size SetSize
{
get { return _SetSize; }
set { _SetSize = value; }
}
//将此控件放入容器的大小
private Point _ContainerSize;
public Point ContainerSize
{
get { return _ContainerSize; }
set { _ContainerSize = value; }
}
//设置控件在容器中的位置
private Point _showLocation;
public Point ShowLocation
{
get { return _showLocation; }
set { _showLocation = value; }
}
//传入控制属性类(有限集合,省略)
...........................................
#endregion
public DeviceControl()
{
InitializeComponent();
//透明设置(在本控件中由于使用了g.clear()方法而失效,可用画布叠加解决,暂未实现)
SetStyle(ControlStyles.SupportsTransparentBackColor
| ControlStyles.UserPaint
| ControlStyles.AllPaintingInWmPaint
| ControlStyles.Opaque, true);
BackColor = Color.Transparent;
}
#region 定义一些执行函数中需要用到的方法
//生成信息标签
private ToolTip Get_Tooltip(string Title)
{
ToolTip Node_Box = new ToolTip();
Node_Box.AutoPopDelay = 5000;
Node_Box.InitialDelay = 100;
Node_Box.ReshowDelay = 300;
Node_Box.ShowAlways = true;
Node_Box.IsBalloon = true;
Node_Box.OwnerDraw = true;
Node_Box.ToolTipIcon = ToolTipIcon.Info;
Node_Box.ToolTipTitle = Title;
Node_Box.UseFading = true;
return Node_Box;
}
//产生三角形的各个点
private PointF[] GetTrianglePointF()
{
PointF point1 = new PointF(ClientSize.Width / 2, 0.0F);
PointF point2 = new PointF(0.0F, ClientSize.Height);
PointF point3 = new PointF(ClientSize.Width, ClientSize.Height);
PointF[] curvePoints =
{
point1,
point2,
point3,
};
return curvePoints;
}
//产生多边形的各个点
private PointF[] GetPolygonPointF()
{
PointF point1 = new PointF(ClientSize.Width / 2, 0.0F);
PointF point2 = new PointF(0.0F, ClientSize.Height / 3);
PointF point3 = new PointF(ClientSize.Width / 4, ClientSize.Height);
PointF point4 = new PointF(ClientSize.Width / 5 + ClientSize.Width / 5 + ClientSize.Width / 5 + ClientSize.Width / 5, ClientSize.Height);
PointF point5 = new PointF(ClientSize.Width, ClientSize.Height / 3);
PointF[] curvePoints =
{
point1,
point2,
point3,
point4,
point5
};
return curvePoints;
}
/// <summary>
/// 画字
/// </summary>
/// <param name="g">图对象</param>
/// <param name="Word">要画的字符串</param>
/// <returns>画好的图对象</returns>
private Graphics DrawWord(Graphics g, string Word)
{
// Create font and brush.
Font drawFont = new Font("Arial", 8);
SolidBrush drawBrush = new SolidBrush(Color.Black);
// Create point for upper-left corner of drawing.
float x = 0.0F;
float y = ClientSize.Height / 2;
// Draw string to screen.
g.DrawString(Word, drawFont, drawBrush, x, y);//往控件里面写字
return g;
}
#endregion
#region 控件使用到的一些消息响应方法
private void DeviceControl_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
Pen mypen = new Pen(Color.Black, 2);
g.Clear(Color.White);//将背景清除并且填充白色
//g.DrawImage(this.BackgroundImage, 0, 0, this.Width, this.Height);
//依照不同的属性类(集合)画不同的图案
...................................................................................
// 图像变大超出容器范围则必须对其位置进行调整
if (this._ContainerSize != null)//如果有设置此控件放入的容器大小
{
if ((base.Left + this.ClientSize.Width) > this._ContainerSize.X)//超出右边边界
{
base.Left = this._ContainerSize.X - this.ClientSize.Width - 10;
}
else
{
if (base.Left != _showLocation.X && !this._InControl)
base.Left = _showLocation.X;
}
if ((base.Top + this.ClientSize.Height) > this._ContainerSize.Y)//超出下边边界
{
base.Top = this._ContainerSize.Y - this.ClientSize.Height - 10;
}
else
{
if (base.Top != _showLocation.Y && !this._InControl)
base.Top = _showLocation.Y;
}
}
}
//鼠标放到控件上面时要做的动作
private void DeviceControl_MouseHover(object sender, EventArgs e)
{
if (!_showToolTip)
{
ToolTip t = this.Get_Tooltip(...);
string info="";//标签要显示的内容
t.SetToolTip(this, info);//this即是绑定到本身
this._showToolTip = true;
}
this.Invalidate();//清除已画的画布上的图形
this.Size = new System.Drawing.Size(_SetSize.Width + 30, _SetSize.Height + 30);//重绘控件的大小,默认是扩大30个像素
this._InControl = true;
}
//鼠标移开时要做的动作
private void DeviceControl_MouseLeave(object sender, EventArgs e)
{
this.Invalidate();//清除已画的画布上的图形
this.Size = new System.Drawing.Size(_SetSize.Width , _SetSize.Height );//重绘控件的大小
this._InControl = false;
}
#endregion
}
}