用C#创建PDA应用程序的柱形图控件

2016-02-19 15:24 38 1 收藏

下面,图老师小编带您去了解一下用C#创建PDA应用程序的柱形图控件,生活就是不断的发现新事物,get新技能~

【 tulaoshi.com - 编程语言 】

  VS.net本身并不提供智能设备(如PDA)应用程序的柱形图,开发智能设备应用程序时VS.net并不象Window应用程序那样提供用户自定义控件。在本文中,您将创建一个以柱形图显示的 PDAChartControl自定义控件。还将创建一个使用此 PDAChartControl自定义控件的智能设备应用程序。为了完成开发工作,您将执行这些过程:

   创建该 PDAChartControl 自定义控件的运行时版本。

   编译该 PDAChartControl 自定义控件的设计时版本。

(本文来源于图老师网站,更多请访问https://www.tulaoshi.com/bianchengyuyan/)

   将该控件添加到工具箱中。

   创建一个使用该 PDAChartControl 自定义控件的智能设备应用程序。

   在智能设备应用程序中测试该控件。

  本文的重点不在于编写控件的代码,而在于如何创建设计时自定义控件以及如何将它添加到"工具箱"中。

  生成自定义控件

  第一步是使用智能设备类库模板创建新项目并生成控件。

  创建自定义控件

  1. 在"文件"菜单上指向"新建",然后单击"项目"。

  2. 在"新建项目"对话框中的"项目类型"下,单击"Visual C# 项目",并在"模板"下单击"智能设备应用程序"。

  3. 在"名称"框中,键入"PDAChartControlControl",然后单击"确定"。

  4. 在"智能设备应用程序向导"中,单击上窗格中的"Pocket PC"和下窗格中的"类库",然后单击"确定"。

  创建了一个新项目,Class1.cs 在代码编辑器中打开。

  由于已经创建用于该控件的项目,接下来可以向项目中添加引用、更改代码以及编译 PDAChartControl 自定义控件的运行时版本。

  编译自定义控件的运行时版本

(本文来源于图老师网站,更多请访问https://www.tulaoshi.com/bianchengyuyan/)

  1. 在解决方案资源管理器中,右击 Class1.cs 并单击"重命名"。

  2. 重命名文件 PDAChartControlControl.cs。

  注意 如果没有打开解决方案资源管理器,请单击"视图"菜单上的"解决方案资源管理器"。

  用下列代码替换 PDAChartControlControl.cs 中的代码:

  

//*****************************************************************************// PDAChartControlControlusing 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./// /summarypublic 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("设置/读取滚动条是否可见")]#endifpublic 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("设置/读取图形类型")]#endifpublic 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("设置/读取饼图高")]#endifpublic 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("设置/读取文本字体")]#endifpublic 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("设置/读取文本字体")]#endifpublic 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("设置/读取数据表")]#endifpublic DataTable dataTable{ get {  return mDataTable; } set {  mDataTable=(DataTable)value;  this.Invalidate(); }}private string mShowColumnName;#if NETCFDESIGNTIME[System.ComponentModel.Category("PDAChart")][System.ComponentModel.Description("设置/读取显示列")]#endifpublic string ShowColumnName{ get {  return mShowColumnName; } set {  mShowColumnName=value;  this.Invalidate(); }}private string mDataColumnName;#if NETCFDESIGNTIME[System.ComponentModel.Category("PDAChart")][System.ComponentModel.Description("设置/读取数据列")]#endifpublic 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("设置/读取标题")]#endifpublic 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 graphpublic 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); }}#endifprivate Color mBackColor=System.Drawing.SystemColors.ControlLight;#if NETCFDESIGNTIME[System.ComponentModel.Category("PDAChart")][System.ComponentModel.DefaultValueAttribute(0)][System.ComponentModel.Description("设置/读取背景颜色")]#endifpublic 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轴颜色")]#endifpublic 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轴颜色")]#endifpublic 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 PDAChartpublic 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 PDAChartpublic 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 PDAChartpublic 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 PDAChartpublic 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 PDAChartpublic 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./// /summaryprotected 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./// /summaryprivate 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.Width246)   this.hScrollBar1.Width= rcClient.Width;  else   this.hScrollBar1.Width=246; } else {  this.hScrollBar1.Left=0;  this.hScrollBar1.Top=0 ; } //从此分别画图 if (this.mMaxYValue10) {  //水平网络线  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.CountOnePageCubeCount)  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.CurrentPagethis.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;ithis.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));/
                        

来源:https://www.tulaoshi.com/n/20160219/1609556.html

延伸阅读
WINDOWS应用程序是靠消息驱动的,在VC中我们通过CLASSWIZARD可以为某窗口类添加消息处理函数,CLASSWIZARD将为你添加消息映射,对于WINDOWS消息,生成的消息处理函数重载了基类的虚拟方法。而在C#中如何处理消息呢?本文针对VS.NET BETA1环境下的C#简单地介绍WINDOWS消息以及自定义消息的处理方法。 示例代码下载 17K 一、生成一个名为MSGAppl...
标签: autocad教程
    实例1 最简单的VBA程序—“Hello.dvb”     Step 1 创建新文件     运行Auto CAD 2002系统,以“acadiso.dwt”为样板创建图形文件,并调用“vbaide”命令进入VBA环境;     Step 2 创建窗体     (1) 选择菜单【Insert(插...
在Windows系统一般都安装了至少三种输入法,在输入数据时常常会切换输入法,虽然Windows系统提供了切换快捷健,但对输入工作还是带来了不少麻烦。如果在应用程序中为用户提供智能输入法自动切换,那么这样的应用程序就显得更加专业、更加具有竞争力。不知你可用过Access,在表数据输入时Access自动切换输入法,很酷吧,现在你也可以实现这一...
这儿就是昨天说的那个程序主要部分的源代码,如果要全部代码请到我的主页去下载。 贴不开,分两次。 //-------------------------------------------------------------------------- // // file name : form1.cs // // description : 主form // // date: 2001/1/3 // // author : bigeagle@163.net // http://bigeagle.yeah.net // // hist...
在C#是一种类似 Java 的完全面向对象的高级编程语言,其处理过程采用事件驱动方式,但在实际的使用过程中,有时候通过调用系统原有的消息处理起来会更简单,特别是在处理与DLL文件的交互时,实践证明的确是非常方便的。 在C#中使用自定义消息 在C#中使用自定义消息非常简单,只需要下面几个简单的步骤就可以了: 1、定义消...

经验教程

516

收藏

23
微博分享 QQ分享 QQ空间 手机页面 收藏网站 回到头部