第一步:
確定這個(gè)控件需要用來(lái)做什么的,我們想對(duì)這個(gè)控件進(jìn)行什么樣的操作,這個(gè)控件需要什么屬性;
第二步:
明確我們要做的控件之后就要開始進(jìn)行實(shí)質(zhì)性的技術(shù)攻關(guān)了,就是你要定義好各種要做的方法,明確這些方法是否是技術(shù)可及的;
第三步:
方法實(shí)現(xiàn)階段,將原先已經(jīng)解決的技術(shù)關(guān)鍵點(diǎn)和應(yīng)處理的各個(gè)小范圍處理應(yīng)用到整個(gè)工程;
以下我做的一個(gè)控件的代碼:
///
///此控件達(dá)到的功能是可以隨意控制控件的大小,所在容器的位置,并且此控件實(shí)現(xiàn)了
///在鼠標(biāo)放上去的時(shí)候能夠自動(dòng)變大(需要改變大小可以自己再添加屬性,本控件默認(rèn)長(zhǎng)寬增長(zhǎng)30個(gè)像素),并且會(huì)產(chǎn)生標(biāo)簽顯示控件的信息(可控)
///在鼠標(biāo)離開后會(huì)自動(dòng)變回原來(lái)默認(rèn)的大小,并有畫字畫圖功能(這部分是根據(jù)傳入有限集合進(jìn)行智能繪畫功能,出入集合自行解決,本控件將其省略)
///
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;//默認(rèn)false
private bool _InControl;//鼠標(biāo)是否放在控件上的標(biāo)志位
//設(shè)置窗口的大小
private Size _SetSize;
public Size SetSize
{
get { return _SetSize; }
set { _SetSize = value; }
}
//將此控件放入容器的大小
private Point _ContainerSize;
public Point ContainerSize
{
get { return _ContainerSize; }
set { _ContainerSize = value; }
}
//設(shè)置控件在容器中的位置
private Point _showLocation;
public Point ShowLocation
{
get { return _showLocation; }
set { _showLocation = value; }
}
//傳入控制屬性類(有限集合,省略)
...........................................
#endregion
public DeviceControl()
{
InitializeComponent();
//透明設(shè)置(在本控件中由于使用了g.clear()方法而失效,可用畫布疊加解決,暫未實(shí)現(xiàn))
SetStyle(ControlStyles.SupportsTransparentBackColor
| ControlStyles.UserPaint
| ControlStyles.AllPaintingInWmPaint
| ControlStyles.Opaque, true);
BackColor = Color.Transparent;
}
#region 定義一些執(zhí)行函數(shù)中需要用到的方法
//生成信息標(biāo)簽
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;
}
//產(chǎn)生三角形的各個(gè)點(diǎn)
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;
}
//產(chǎn)生多邊形的各個(gè)點(diǎn)
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">圖對(duì)象</param>
/// <param name="Word">要畫的字符串</param>
/// <returns>畫好的圖對(duì)象</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 控件使用到的一些消息響應(yīng)方法
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);
//依照不同的屬性類(集合)畫不同的圖案
...................................................................................
// 圖像變大超出容器范圍則必須對(duì)其位置進(jìn)行調(diào)整
if (this._ContainerSize != null)//如果有設(shè)置此控件放入的容器大小
{
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;
}
}
}
//鼠標(biāo)放到控件上面時(shí)要做的動(dòng)作
private void DeviceControl_MouseHover(object sender, EventArgs e)
{
if (!_showToolTip)
{
ToolTip t = this.Get_Tooltip(...);
string info="";//標(biāo)簽要顯示的內(nèi)容
t.SetToolTip(this, info);//this即是綁定到本身
this._showToolTip = true;
}
this.Invalidate();//清除已畫的畫布上的圖形
this.Size = new System.Drawing.Size(_SetSize.Width + 30, _SetSize.Height + 30);//重繪控件的大小,默認(rèn)是擴(kuò)大30個(gè)像素
this._InControl = true;
}
//鼠標(biāo)移開時(shí)要做的動(dòng)作
private void DeviceControl_MouseLeave(object sender, EventArgs e)
{
this.Invalidate();//清除已畫的畫布上的圖形
this.Size = new System.Drawing.Size(_SetSize.Width , _SetSize.Height );//重繪控件的大小
this._InControl = false;
}
#endregion
}
}
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注