程序|创建|控件|柱形图
VS.net本身并不提供智能设备(如PDA)应用程序的柱形图,开发智能设备应用程序时VS.net并不象Window应用程序那样提供用户自定义控件。在本文中,您将创建一个以柱形图显示的 PDAChartControl自定义控件。还将创建一个使用此 PDAChartControl自定义控件的智能设备应用程序。为了完成开发工作,您将执行这些过程:
· 创建该 PDAChartControl 自定义控件的运行时版本。
· 编译该 PDAChartControl 自定义控件的设计时版本。
· 将该控件添加到工具箱中。
· 创建一个使用该 PDAChartControl 自定义控件的智能设备应用程序。
· 在智能设备应用程序中测试该控件。
本文的重点不在于编写控件的代码,而在于如何创建设计时自定义控件以及如何将它添加到"工具箱"中。
生成自定义控件
第一步是使用智能设备类库模板创建新项目并生成控件。
创建自定义控件
1. 在"文件"菜单上指向"新建",然后单击"项目"。
2. 在"新建项目"对话框中的"项目类型"下,单击"Visual C# 项目",并在"模板"下单击"智能设备应用程序"。
3. 在"名称"框中,键入"PDAChartControlControl",然后单击"确定"。
4. 在"智能设备应用程序向导"中,单击上窗格中的"Pocket PC"和下窗格中的"类库",然后单击"确定"。
创建了一个新项目,Class1.cs 在代码编辑器中打开。
由于已经创建用于该控件的项目,接下来可以向项目中添加引用、更改代码以及编译 PDAChartControl 自定义控件的运行时版本。
编译自定义控件的运行时版本
1. 在解决方案资源管理器中,右击 Class1.cs 并单击"重命名"。
2. 重命名文件 PDAChartControlControl.cs。
注意 如果没有打开解决方案资源管理器,请单击"视图"菜单上的"解决方案资源管理器"。
用下列代码替换 PDAChartControlControl.cs 中的代码:
//*************************************
// PDAChartControlControl
using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
#if NETCFDESIGNTIME
[assembly: System.CF.Design.RuntimeAssemblyAttribute("PDAChartControl, Version=1.10.0.0, _
Culture=neutral, PublicKeyToken=null")]
namespace PDAChartControl
{
/// <summary>
/// Summary description for UserControl1.
/// </summary>
public class PDAChart : System.Windows.Forms.Control
{
public System.Windows.Forms.HScrollBar hScrollBar1;
/// <summary>
/// Required designer variable.
/// </summary>
// Delegate declaration.
// public delegate void EventHandler(string text,Color BackColor,int Height);
//
// //声明事件的委托:
// //public delegate void MyEventHandler(string text,Color BackColor,int Height);
// //定义一个公共事件成员
// public event EventHandler AddCube;
// protected virtual void OnAddCube(EventArgs e)
// {
//
// }
//
private PDAChartControl.MyGraph objGraph=new MyGraph();
private Point mBeginPoint=new Point(0,0) ;
private System.ComponentModel.Container components = null;
public PDAChart()
{
InitializeComponent();
}
public enum ChartTypeEnum { PillarChart, CakeChart ,BreakLinkChart};
#region Windows 属性定义
private bool mhScrollBarVisible=true;
#if NETCFDESIGNTIME
[System.ComponentModel.Category("PDAChart")]
[System.ComponentModel.DefaultValueAttribute(0)]
[System.ComponentModel.Description("设置/读取滚动条是否可见")]
#endif
public bool hScrollBarVisible
{
get
{
return mhScrollBarVisible;
}
set
{
mhScrollBarVisible =value;
this.Invalidate();
}
}
private ChartTypeEnum mChartType=ChartTypeEnum.PillarChart;
#if NETCFDESIGNTIME
[System.ComponentModel.Category("PDAChart")]
[System.ComponentModel.DefaultValueAttribute(0)]
[System.ComponentModel.Description("设置/读取图形类型")]
#endif
public ChartTypeEnum ChartType
{
get
{
return mChartType;
}
set
{
mChartType =value;
this.Invalidate();
}
}
private int mPicHeight=20;
#if NETCFDESIGNTIME
[System.ComponentModel.Category("PDAChart")]
[System.ComponentModel.DefaultValueAttribute(0)]
[System.ComponentModel.Description("设置/读取饼图高")]
#endif
public int PicHeight
{
get
{
return mPicHeight;
}
set
{
mPicHeight =value;
this.Invalidate();
}
}
private Font mTitleFont =new Font("Arial", 9, FontStyle.Regular);
#if NETCFDESIGNTIME
[System.ComponentModel.Category("PDAChart")]
[System.ComponentModel.Description("设置/读取文本字体")]
#endif
public Font TitleFont
{
get
{
return mTitleFont;
}
set
{
mTitleFont=value;
this.Invalidate();
}
}
private Font mTextFont =new Font("Arial", 8, FontStyle.Regular);
#if NETCFDESIGNTIME
[System.ComponentModel.Category("PDAChart")]
[System.ComponentModel.Description("设置/读取文本字体")]
#endif
public Font TextFont
{
get
{
return mTextFont;
}
set
{
mTextFont=value;
this.Invalidate();
}
}
private static DataTable mDataTable=new DataTable() ;
#if NETCFDESIGNTIME
[System.ComponentModel.Category("PDAChart")]
[System.ComponentModel.Description("设置/读取数据表")]
#endif
public DataTable dataTable
{
get
{
return mDataTable;
}
set
{
mDataTable=(DataTable)value;
this.Invalidate();
}
}
private string mShowColumnName;
#if NETCFDESIGNTIME
[System.ComponentModel.Category("PDAChart")]
[System.ComponentModel.Description("设置/读取显示列")]
#endif
public string ShowColumnName
{
get
{
return mShowColumnName;
}
set
{
mShowColumnName=value;
this.Invalidate();
}
}
private string mDataColumnName;
#if NETCFDESIGNTIME
[System.ComponentModel.Category("PDAChart")]
[System.ComponentModel.Description("设置/读取数据列")]
#endif
public string DataColumnName
{
get
{
return mDataColumnName;
}
set
{
mDataColumnName=value;
this.Invalidate();
}
}
private string mTitle="统计图";
#if NETCFDESIGNTIME
[System.ComponentModel.Category("PDAChart")]
[System.ComponentModel.DefaultValueAttribute("图表")]
[System.ComponentModel.Description("设置/读取标题")]
#endif
public string Title
{
get
{
return mTitle;
}
set
{
mTitle=value;
this.Invalidate();
}
}
private ArrayList mCubeData;
#if !NETCFDESIGNTIME
//The actual Data used to draw the line on the graph
public ICollection CubeData
{
get
{
return mCubeData;
}
set
{
mCubeData = new ArrayList(value);
Rectangle rcClient = this.ClientRectangle;
Rectangle rcGraphClient = new Rectangle(rcClient.X + 21, rcClient.Y + 5, rcClient.Width - 21, rcClient.Height - 21);
this.Invalidate(rcGraphClient);
}
}
#endif
private Color mBackColor=System.Drawing.SystemColors.ControlLight;
#if NETCFDESIGNTIME
[System.ComponentModel.Category("PDAChart")]
[System.ComponentModel.DefaultValueAttribute(0)]
[System.ComponentModel.Description("设置/读取背景颜色")]
#endif
public override Color BackColor
{
get
{
return mBackColor;
}
set
{
mBackColor =value;
this.Invalidate();
}
}
private Color mAxesXColor=System.Drawing.SystemColors.HighlightText;
#if NETCFDESIGNTIME
[System.ComponentModel.Category("PDAChart")]
[System.ComponentModel.DefaultValueAttribute(0)]
[System.ComponentModel.Description("设置/读取X轴颜色")]
#endif
public Color AxesXColor
{
get
{
return mAxesXColor;
}
set
{
mAxesXColor =value;
this.Invalidate();
}
}
private Color mAxesYColor=System.Drawing.SystemColors.Info;
#if NETCFDESIGNTIME
[System.ComponentModel.Category("PDAChart")]
[System.ComponentModel.DefaultValueAttribute(0)]
[System.ComponentModel.Description("设置/读取Y轴颜色")]
#endif
public Color AxesYColor
{
get
{
return mAxesYColor;
}
set
{
mAxesYColor =value;
this.Invalidate();
}
}
private int mLenght = 4;
#if NETCFDESIGNTIME
// These design time attributes affect appearance of this property in the property grid.
[System.ComponentModel.DefaultValueAttribute(5)]
[System.ComponentModel.Description("立体长")]
#endif
//The lower Y bound of the PDAChart
public int Lenght
{
get
{
return mLenght;
}
set
{
mLenght = value;
this.Invalidate();
}
}
private int mMaxYValue ;//图表Y轴最大值
private int mMaxXValue ;//图表X轴最大值
private Color mGridLineColor=System.Drawing.Color.Cyan;
#if NETCFDESIGNTIME
[System.ComponentModel.Category("PDAChart")]
[System.ComponentModel.Description("网格线的颜色.")]
#endif
//The color of the line of the PDAChart.
public Color GridLineColor
{
get
{
return mGridLineColor;
}
set
{
mGridLineColor =value;
this.Invalidate();
}
}
private bool mShowXText = true;
#if NETCFDESIGNTIME
// These design time attributes affect appearance of this property in the property grid.
[System.ComponentModel.Category("Chart")]
[System.ComponentModel.DefaultValueAttribute(true)]
[System.ComponentModel.Description("是否显示X轴的文本")]
#endif
// If true, shows the Y-Values on the left of the PDAChart
public bool IsShowXText
{
get
{
return mShowXText;
}
set
{
mShowXText = value;
this.Invalidate();
}
}
private bool mShowYText = true;
#if NETCFDESIGNTIME
// These design time attributes affect appearance of this property in the property grid.
[System.ComponentModel.Category("Chart")]
[System.ComponentModel.DefaultValueAttribute(true)]
[System.ComponentModel.Description("是否显示Y轴的数字")]
#endif
// If true, shows the Y-Values on the left of the PDAChart
public bool IsShowYText
{
get
{
return mShowYText;
}
set
{
mShowYText = value;
this.Invalidate();
}
}
private bool mShowXScale = true;
#if NETCFDESIGNTIME
// These design time attributes affect appearance of this property in the property grid.
[System.ComponentModel.Category("Chart")]
[System.ComponentModel.DefaultValueAttribute(true)]
[System.ComponentModel.Description("是否显示X轴的刻度.")]
#endif
// If true, shows the X-Values on the bottom of the PDAChart
public bool IsShowXScale
{
get
{
return mShowXScale;
}
set
{
mShowXScale = value;
this.Invalidate();
}
}
private bool mShowYScale = true;
#if NETCFDESIGNTIME
// These design time attributes affect appearance of this property in the property grid.
[System.ComponentModel.Category("Chart")]
[System.ComponentModel.DefaultValueAttribute(true)]
[System.ComponentModel.Description("是否显示Y轴的刻度")]
#endif
// If true, shows the Y-Values on the left of the PDAChart
public bool IsShowYScale
{
get
{
return mShowYScale;
}
set
{
mShowYScale = value;
this.Invalidate();
}
}
private bool mShowGrid = true;
#if NETCFDESIGNTIME
// These design time attributes affect appearance of this property in the property grid.
[System.ComponentModel.Category("Chart")]
[System.ComponentModel.DefaultValueAttribute(false)]
[System.ComponentModel.Description("是否显示网格线")]
#endif
// If true, shows horiztonal grid lines on the PDAChart.
public bool IsShowGrid
{
get
{
return mShowGrid;
}
set
{
mShowGrid = value;
this.Invalidate();
}
}
#endregion
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if( components != null )
components.Dispose();
}
base.Dispose( disposing );
}
#region Component Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the Code Editor.
/// </summary>
private void InitializeComponent()
{
this.Paint += new System.Windows.Forms.PaintEventHandler(this.OnPaint);
this.hScrollBar1 = new System.Windows.Forms.HScrollBar();
this.hScrollBar1.Location = new System.Drawing.Point(1, 100);
this.hScrollBar1.Maximum = 1;
this.hScrollBar1.LargeChange = 1;
this.hScrollBar1.Size = new System.Drawing.Size(100, 16);
this.hScrollBar1.ValueChanged += new System.EventHandler(this.hScrollBar1_ValueChanged);
this.Controls.Add(this.hScrollBar1);
}
#endregion
//
// private ArrayList mCudeData;
protected override void OnResize(EventArgs e)
{
//this.Refresh();
}
Graphics mGraphics;
Pen mBlackPen=new Pen(Color.Black);
//偏差
int TopHeightWarp=16; //顶偏差(文本高)
int LeftWidthWarp=0; //左偏差(最大数据文本宽)
int UnderHeightWarp=10; //底偏差(底文本高)
int BetweenLineHeight=10;//水平线的高
int LineCount=10;//水平线数
// //This Paint function uses routines common to both platforms.
int ClientHeight;
int mWidth;
int YHeight;
Rectangle rcClient;
System.Drawing.Region Region1;
public void OnPaint(object sender, System.Windows.Forms.PaintEventArgs e)
{
//base.Paint(null,e);
mGraphics=e.Graphics;
//读取数据
this.rcClient = this.ClientRectangle;
Region1=new Region ( this.rcClient);
Region1=Region1.Clone();
ClientHeight=rcClient.Height;
objGraph.mGraphics=e.Graphics; //mGraphics.MeasureString//
//计算最大的x轴、y轴坐标
CountMaxScaleXScaleY();
UnderHeightWarp=(int)objGraph.mGraphics.MeasureString("文本",this.mTextFont).Height+5;
UnderHeightWarp+=this.hScrollBar1.Height;
//作图的范围区(开始点、长、宽、高)
mBeginPoint.X =this.ClientRectangle.X+LeftWidthWarp ;
mBeginPoint.Y =this.ClientRectangle.Y+rcClient.Height-UnderHeightWarp ;
//写标题
DrawTitle(rcClient);
int Width=rcClient.Width-this.mLenght-LeftWidthWarp;
mWidth=Width;
int Height=rcClient.Height-this.mLenght-TopHeightWarp-UnderHeightWarp;
this.YHeight= Height;
int Lenght=this.mLenght;
//设置流动条
// this.hScrollBar1.Location = new System.Drawing.Point(0, rcClient.Y+rcClient.Height);
InitPage();
if (this.hScrollBarVisible)
{
this.hScrollBar1.Left=0;
this.hScrollBar1.Top=rcClient.Height-this.hScrollBar1.Height ;
this.hScrollBar1.Maximum=this.PageCount-1;
if(rcClient.Width<246)
this.hScrollBar1.Width= rcClient.Width;
else
this.hScrollBar1.Width=246;
}
else
{
this.hScrollBar1.Left=0;
this.hScrollBar1.Top=0 ;
}
//从此分别画图
if (this.mMaxYValue>10)
{
//水平网络线
this.BetweenLineHeight=(int)objGraph.mGraphics.MeasureString("文本",this.mTextFont).Height;
this.LineCount=Height/BetweenLineHeight;
}
else
{
this.BetweenLineHeight=Height/10;
//线数应该是能整
this.LineCount=10;
}
//画与它父相同背景颜色的区域,以隐藏角边
Color ParentBackColor=Color.Brown;
objGraph.DrawRectangle( ParentBackColor,new Point(0,0),rcClient.Width,rcClient.Height);
e.Graphics.DrawRectangle(new Pen(ParentBackColor), rcClient);
objGraph.DrawPDAChart(this.mGridLineColor, this.mAxesXColor,this.mAxesYColor,_
this.mBackColor,mBeginPoint,Lenght,Width,Height, this.mShowXScale,this.mShowYScale);
//画线和左文本(内部左下点)
Point p=mBeginPoint;
p.X+=this.mLenght;
p.Y-=this.mLenght;
DrawGridLineAndTexts(p,Width);
//mBeginPoint
//画矩形与写文本
//CreateCubes(mBeginPoint,Width,rcClient.Height);
//mBeginPoint.X+=10;
CreatePageCubes(mBeginPoint,Width,ClientHeight);
DrawTitle(rcClient);
}
}
//以左下坐标p,颜色color,长Lenght,宽Width,高Height,x轴文本textX,画立体图
public void AddOneCube(string textX,Point p,Color color,int Lenght,int Width, int Height)
{
try
{
objGraph.DrawCube (color,p,Lenght,Width,Height);
//文本
int txtWidth=(int)objGraph.mGraphics.MeasureString(textX,mTextFont).Width;
int txtHeight=(int)objGraph.mGraphics.MeasureString(textX,mTextFont).Height;
int x=(p.X+Width/2)-txtWidth/2;
int y=p.Y+txtHeight/2;
this.objGraph.DrawText(textX,Color.Black,this.mTextFont,x,y-5);
}
catch(Exception ex)
{
string str=ex.Message;
}}
//一页立方体图形个数
int OnePageCubeCount=10;
int CurrentPage=0;//当前页
int PageCount=0;//多少页
//水平轴的相对值
int XScale;
//Y轴的相对值
double YScale=0.2;
Color[] color={Color.Red,Color.Blue,Color.Green,Color.Yellow,Color.YellowGreen,Color.Magenta,_
Color.Cyan,Color.Coral,Color.SlateGray,Color.Pink,Color.Crimson,Color.DodgerBlue,Color.Chartreuse };
//计算页
private void InitPage()
{
if(this.OnePageCubeCount==0) return;
if (mDataTable.Rows.Count<OnePageCubeCount)
this.OnePageCubeCount=mDataTable.Rows.Count ;
if(this.OnePageCubeCount==0) return;
PageCount=mDataTable.Rows.Count/this.OnePageCubeCount;
//水平轴的相对值
XScale=Width/this.OnePageCubeCount ;
//Y轴的相对值
if(this.mMaxYValue<=0) return;
{
if(this.mMaxYValue==0) return;
this.YScale=double.Parse(this.YHeight.ToString())/double.Parse( this.mMaxYValue.ToString() ) ;//System.Math.
//this.YScale=double.Parse(this.YScale.ToString())/double.Parse(this.LineCount.ToString() );
}
// this.YScale=double.Parse(System.Convert.ToString(1))/double.Parse( this.mMaxYValue.ToString() ) ;//System.Math.
}
private void hScrollBar1_ValueChanged(object sender, System.EventArgs e)
{
//OnPaint(object sender, System.Windows.Forms.PaintEventArgs e)
//清画出的图
this.CurrentPage=hScrollBar1.Value;
// if (mGraphics.Clip==null)
// {
mGraphics=this.CreateGraphics ();
this.objGraph.mGraphics=mGraphics;
// }
//mGraphics.Clip=this.Region1;
//画矩形与写文本,最多一屏
//mGraphics.Clear(this.mBackColor) ;
//mGraphics.ResetClip();
//CreatePageCubes(mBeginPoint,Width,ClientHeight);
System.Windows.Forms.PaintEventArgs e1=new PaintEventArgs( mGraphics,this.rcClient);
OnPaint(null,e1);
}
public void NextPage()
{
this.objGraph.mGraphics=this.CreateGraphics ();
this.CurrentPage++;
Bitmap bm = new Bitmap(10,10);
Graphics g = Graphics.FromImage(bm);
if (this.CurrentPage>this.PageCount)
this.CurrentPage--;
//画矩形与写文本,最多一屏
//mGraphics.Clear(Color.Red) ;
//mGraphics.ResetClip();
CreatePageCubes(mBeginPoint,Width,ClientHeight);
}
//在左下顶点,宽Width,高Height建立立方体
private void CreatePageCubes(Point BeginP ,int Width,int Height)
{
if(mDataTable.Rows.Count==0) return;
int Between=10;
switch(this.OnePageCubeCount)
{
case 1:
Between= mWidth/2;
break;
case 2:
Between= mWidth/3;
break;
case 3:
Between= mWidth/4;
break;
case 4:
Between= mWidth/5;
break;
case 5:
Between= mWidth/6;
break;
case 6:
Between= mWidth/7;
break;
case 7:
Between= mWidth/8-1;
break;
case 8:
Between= mWidth/9-2;
break;
case 9:
Between=mWidth/9-5;
break;
case 10:
Between=mWidth/10-5;
break;
}
int RowIndex=this.OnePageCubeCount*this.CurrentPage;
Point p=BeginP;//dr.
p.X-=8;
for ( int i=0;i<this.OnePageCubeCount;i++ )
{
//p.X= this.XScale*(i+1)+10;
p.X+=Between+this.mLenght;
double CubeHeight=this.YScale*System.Convert.ToInt32(mDataTable.Rows[RowIndex][this.mDataColumnName]);
//if ((p.X >= 0) && (p.X <= Width) && (p.Y >= 0) && (p.Y <= Height))
//{
string text=mDataTable.Rows[RowIndex][this.mShowColumnName].ToString() ;
string Data=mDataTable.Rows[RowIndex][this.mDataColumnName].ToString() ;
int ColorIndex=RowIndex;
if (ColorIndex>=color.Length)
ColorIndex=color.Length-1;
if (this.mShowXText==false) text=" ";
AddOneCube(text,p,color[i],this.mLenght,this.mLenght+4,System.Convert.ToInt32(CubeHeight));
/