创建智能设备应用程序的图控件.doc_第1页
创建智能设备应用程序的图控件.doc_第2页
创建智能设备应用程序的图控件.doc_第3页
创建智能设备应用程序的图控件.doc_第4页
创建智能设备应用程序的图控件.doc_第5页
已阅读5页,还剩17页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

开发PDA设备应用程序的饼图控件天宇 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 中的代码: /*/ PDAChartControlControlusing System;using System.Collections;using System.ComponentModel;using System.Drawing;using System.Data;using System.Windows.Forms;using System.Drawing.Drawing2D; #if NETCFDESIGNTIMEassembly: System.CF.Design.RuntimeAssemblyAttribute(PDAChartControl, Version=, Culture=neutral, PublicKeyToken=null)namespace PDAChartControl/ / Summary description for UserControl1./ public class PDAChart : System.Windows.Forms.Controlpublic System.Windows.Forms.HScrollBar hScrollBar1;private PDAChartControl.MyGraph objGraph=new MyGraph(); private Point mBeginPoint=new Point(0,0) ; private System.ComponentModel.Container components = null;public PDAChart()InitializeComponent();#region Windows 属性定义private Font mTitleFont =new Font(Arial, 9, FontStyle.Regular);private Font mTextFont =new Font(Arial, 8, FontStyle.Regular);#if NETCFDESIGNTIME System.ComponentModel.Category(PDAChart) System.ComponentModel.Description(设置/读取文本字体)#endifpublic Font TextFontgetreturn mTextFont;setmTextFont=value;this.Invalidate();private static DataTable mDataTable=new DataTable() ;#if NETCFDESIGNTIME System.ComponentModel.Category(PDAChart) System.ComponentModel.Description(设置/读取数据表)#endifpublic DataTable dataTablegetreturn mDataTable;setmDataTable=(DataTable)value;this.Invalidate();private string mShowColumnName;#if NETCFDESIGNTIME System.ComponentModel.Category(PDAChart) System.ComponentModel.Description(设置/读取显示列)#endifpublic string ShowColumnNamegetreturn mShowColumnName;setmShowColumnName=value;this.Invalidate();private string mDataColumnName;#if NETCFDESIGNTIME System.ComponentModel.Category(PDAChart) System.ComponentModel.Description(设置/读取数据列)#endifpublic string DataColumnNamegetreturn mDataColumnName;setmDataColumnName=value;this.Invalidate();private string mTitle=统计图;#if NETCFDESIGNTIME System.ComponentModel.Category(PDAChart) System.ComponentModel.DefaultValueAttribute(图表) System.ComponentModel.Description(设置/读取标题)#endifpublic string Titlegetreturn mTitle;setmTitle=value;this.Invalidate();private Color mBackColor=System.Drawing.SystemColors.ControlLight;#if NETCFDESIGNTIME System.ComponentModel.Category(PDAChart) System.ComponentModel.DefaultValueAttribute(0) System.ComponentModel.Description(设置/读取背景颜色)#endifpublic override Color BackColorgetreturn mBackColor;setmBackColor =value;this.Invalidate();/ / Clean up any resources being used./ protected override void Dispose( bool disposing )if( disposing )if( components != null )components.Dispose();base.Dispose( disposing );#region Component Designer generated code/ / Required method for Designer support - do not modify / the contents of this method with the Code Editor./ private void InitializeComponent()this.Paint += new System.Windows.Forms.PaintEventHandler(this.OnPaint);#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 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轴坐标/写标题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;/开始点int b=Height/2;Point LeftTopPoint=new Point(rcClient.X+10,rcClient.Y+TopHeightWarp+20 );/mPicHeight=25;objGraph.DrawCake(this.mBackColor, LeftTopPoint,Width,b,this.mPicHeight); /中心点Point oPoint=new Point ();oPoint.X=LeftTopPoint.X +Width/2;oPoint.Y=LeftTopPoint.Y+b/2;CreatePic(oPoint,Width/2,b/2);/在顶部中心位置写标题private void DrawTitle(Rectangle rcClient)int Width=(int)objGraph.mGraphics.MeasureString(this.mTitle,mTitleFont).Width;int Height=(int)objGraph.mGraphics.MeasureString(this.mTitle,mTitleFont).Height;this.TopHeightWarp=Height;int x=rcClient.Width/2- Width/2;int y=rcClient.Y+Height/2-5; this.objGraph.DrawText(this.mTitle,Color.Blue,mTitleFont,x,y); /求数据列的和private double SumColumn(DataTable dt,string ColumnName)double Sum=0.0;foreach(DataRow dr in dt.Rows) Sum+=System.Convert.ToDouble(drColumnName);return Sum;/以中心点,长轴Width,短轴高Height建立饼图private void CreatePic(Point CenterP ,int Width,int Height)if(mDataTable.Rows.Count=0) return;/角度相对值double Sum=SumColumn(mDataTable,this.mDataColumnName);double angle=360/Sum;double FinishAngle=0.0;int RowIndex=0;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,Color.Orange,Color.Aqua,Color.CadetBlue ; foreach(DataRow dr in mDataTable.Rows)RowIndex+;double Data=System.Convert.ToDouble(drthis.mDataColumnName) ;/显示列数据+-+数据列百分比string text=drthis.mShowColumnName.ToString()+-+(100*System.Math.Round(Data/Sum,2).ToString()+% ;int ColorIndex=RowIndex;if (ColorIndex=color.Length)ColorIndex=color.Length-1;if (this.mShowXText=false) text= ; AddOnePDAPic(colorColorIndex-1,text,angle*Data,FinishAngle,CenterP,Width,Height);FinishAngle+=angle*Data;/画扇形(逆时针)/角度angle,已经画过的角FinishAngle,中心点oPoint,长半轴a,短半轴bpublic void AddOnePDAPic(Color color, string Text,double angle,double FinishAngle,Point oPoint,int a,int b) / Create solid brush.SolidBrush Brush = new SolidBrush(color);ArrayList pList=objGraph.GetPicPoints(angle,FinishAngle,oPoint,a,b);Point curvePoints=new PointpList.Count ; for(int i=0;i1)mGraphics.DrawPolygon(this.mBlackPen, curvePoints);centerP=curvePointspList.Count/2;elsecenterP=curvePoints0;centerP.X=(centerP.X+oPoint.X)/2; if (centerP.YoPoint.Y)centerP.Y=(centerP.Y+oPoint.Y)/2;elsecenterP.Y=(centerP.Y+oPoint.Y)/3;/确定文本位置int txtWidth=(int)objGraph.mGraphics.MeasureString(Text,this.mTextFont).Width;/int txtHeight=(int)objGraph.mGraphics.MeasureString(Text,this.mTextFont).Height;centerP.X=centerP.X-txtWidth/2; /centerP.Y+=txtHeight/2; if (FinishAngle=0)centerP.Y-=5;centerP.X+=10;if (FinishAngle=120)centerP.Y-=5;centerP.X-=10;if (FinishAngle=180)centerP.Y+=15;centerP.X-=10;if (FinishAngle=200)/centerP.Y+=15;centerP.X+=20;if (FinishAngle=270)/centerP.Y+=5;centerP.X+=5;if (FinishAngle=300)centerP.Y+=5;centerP.X+=15;objGraph.DrawText(Text,Color.Black,this.mTextFont,centerP.X,centerP.Y);#region public class MyGraph/网络水平线中二线之间的高/int BetweenLineHeight=20;/最大MaxYCount:线数/int MaximumY=10;/int Lenght=5;/int Width=200;/int Height=300;/bool IsShowGrid=true;public Graphics mGraphics;/背景色/public Color BackColor= System.Drawing.SystemColors.Control;/X轴颜色/public Color AxesXColor=System.Drawing.SystemColors.HighlightText;/Y轴颜色/public Color AxesYColor=System.Drawing.SystemColors.Info;/黑色笔private Pen mBlackPen = new System.Drawing.Pen(Color.FromArgb(0,0,0);/网格线笔private Pen mGridPen= new System.Drawing.Pen(Color.FromArgb(127, 127, 127);/Color BackColor= System.Drawing.Color.FromArgb(System.Byte)(224), (System.Byte)(224), (System.Byte)(224);public MyGraph()/mGraphics=this.CreateGraphics(); / TODO: 在此处添加构造函数逻辑/由左下顶点与宽、高画颜色为color的平行四边形public void DrawRectangle3DTop(Color color, Point LeftUnderPoint,int Width,int Height) try/计算左上顶点Point p1=new Point ();p1.X=LeftUnderPoint.X+Height;p1.Y=LeftUnderPoint.Y-Height;/计算右上顶点Point p2=new Point ();p2.X=LeftUnderPoint.X+Width+Height;p2.Y=LeftUnderPoint.Y-Height;/计算右下顶点Point p3=new Point ();p3.X=LeftUnderPoint.X+Width;p3.Y=LeftUnderPoint.Y;Point curvePoints =LeftUnderPoint,p1,p2,p3;/ Define fill mode./FillMode newFillMode = FillMode.Winding;/ Fill polygon to screen./ Create solid brush.SolidBrush Brush = new SolidBrush(color);mGraphics.FillPolygon(Brush, curvePoints);/mGraphics.FillPolygon(Brush, curvePoints, newFillMode);/画边框mGraphics.DrawPolygon(this.mBlackPen, curvePoints);catch(Exception ex)string str=ex.Message;/由左下顶点与宽、高画颜色为color的平等四边形public void DrawRectangle3DRight(Color color, Point LeftUnderPoint,int Width,int Height) try/ Create solid brush.SolidBrush Brush = new SolidBrush(color);/计算左上顶点Point p1=new Point ();p1.X=LeftUnderPoint.X;p1.Y=LeftUnderPoint.Y-Height;/计算右上顶点Point p2=new Point ();p2.X=p1.X+Width;p2.Y=p1.Y-Width;/计算右下顶点Point p3=new Point ();p3.X=LeftUnderPoint.X+Width;p3.Y=LeftUnderPoint.Y-Width;Point curvePoints =LeftUnderPoint,p1,p2,p3;/ Define fill mode./FillMode newFillMode = FillMode.Winding;/ Fill polygon to screen.mGraphics.FillPolygon(Brush, curvePoints);/画边框mGraphics.DrawPolygon(this.mBlackPen, curvePoints);/mGraphics.FillPolygon(Brush, curvePoints, newFillMode);catch(Exception ex)string str=ex.Message;/由左上角顶点与宽、高画颜色为color的平行四边形public void DrawRectangle(Color color, Point P,int Width,int Height) Rectangle Rectangle1=new Rectangle( P.X,P.Y, Width,Height);/ Create solid brush.SolidBrush Brush = new SolidBrush(color);/ Fill polygon to screen.mGraphics.FillRectangle(Brush, Rectangle1);/画边框mGraphics.DrawRectangle(this.mBlackPen,Rectangle1);/由左下顶点与长、宽、高画颜色为color的立方图形(3D)public void DrawCube(Color color, Point LeftUnderPoint,int Lenght,int Width,int Height) / Create solid brush.SolidBrush Brush = new SolidBrush(color);Point LeftTopPoint= LeftUnderPoint;LeftTopPoint.Y-= Height;DrawRectangle3DTop(color,LeftTopPoint,Width,Lenght);DrawRectangle(color,LeftTopPoint,Width,Height);Point RightP=LeftUnderPoint; RightP.X+=Width;DrawRectangle3DRight(Color.Black,RightP,Lenght,Height);/画X轴public void DrawAxesX(Color color, Point p,int Width,int Height)DrawRectangle3DTop(color,p,Width,Height);/画Y轴public void DrawAxesY(Color color, Point p,int Width,int Height)DrawRectangle3DRight(color,p,Width,Height);/由顶点与长、宽、高画颜色为color,背景色为的BackColor图表(3D)public void DrawPDAChart(Color GridLineColor,Color AxesXColor,Color AxesYColor, Color BackColor,Point p,int Lenght,int Width,int Height,bool IsShowAxesX,bool IsShowAxesY)if(IsShowAxesX)/画X轴DrawAxesX(AxesXColor,p,Width,Lenght);if(IsShowAxesY)/画Y轴DrawAxesY(AxesYColor,p,Lenght,Height);/画图形区Point pRectangle=p; pRectangle.X+=Lenght;pRectangle.Y-=Lenght;pRectangle.Y-=Height;DrawRectangle(BackColor,pRectangle,Width,Height);/画一条水平网络线与对应的折线public void DrawGridLine(Color GridLineColor,Point p,int Width,int Lenght)/Draw the Y scale;Point EndP=p;EndP.X+=Width;Pen pen=new Pen( GridLineColor);/this.mGraphics.DrawLine(pen,p,EndP);/水平线this.mGraphics.DrawLine(pen,p.X,p.Y,EndP.X,EndP.Y ); /左折线this.mGraphics.DrawLine(pen,p.X,p.Y,EndP.X-Lenght,EndP.Y+Lenght ); /画所有水平网络线/p:起始点;Width:线宽;BetweenHeight:二线之间高,Count:线数public void DrawGridLines(Color GridLineColor,Point p,int Width,int Lenght,int BetweenHeight,int Count)Pen pen=new Pen( GridLineColor);for(int i=0;iCount;i+)/DrawGridLine(GridLineColor,p,Width,Lenght); /水平线this.mGraphics.DrawLine(pen,p.X,p.Y,p.X+Width,p.Y ); /左折线this.mGraphics.DrawLine(pen,p.X-Lenght+1,p.Y+Lenght,p.X,p.Y); p.Y-=BetweenHeight; /在位置(x,y)处以颜色color、字体font写文本textpublic void DrawText(string text,Color color, Font font,int x,int y)/ Create solid brush.SolidBrush Brush = new SolidBrush(color);this.mGraphics.DrawString(text, font, Brush, x ,y);/由点p(矩形左上角点),宽pieWidth,高pieHeight,颜色color画馅饼图public void DrawCake(Color color,Point p,int pieWidth,int pieLenght,int pieHeight)Pen PenBlack=new Pen( Color.Black);/黑色最下面的椭圓Rectangle rc1 =new Rectangle(p.X,p.Y+pieHeight,pieWidth,pieLenght);this.mGraphics.DrawEllipse(PenBlack,rc1);SolidBrush objBrush = new SolidBrush(color);for(int i=0;ipieHeight;i+)this.mGraphics.FillEllipse(objBrush,p.X,p.Y+i,pieWidth,pieLenght);/黑色最上面的椭圓Rectangle rc =new Rectangle(p.X,p.Y,pieWidth,pieLenght);this.mGraphics.DrawEllipse(PenBlack,rc);this.mGraphics.DrawLine( PenBlack,p.X,p.Y+pieLenght/2,p.X,p.Y+pieHeight+pieLenght/2);this.mGraphics.DrawLine( PenBlack,p.X+pieWidth,p.Y+pieLenght/2,p.X+pieWidth,p.Y+pieHeight+pieLenght/2);/求隋圆任意一点x坐标的相对点/角angle,中心点oPoint,a,长半轴,b,短半轴public double GetEllipsePX(double angle,int a,int b)/角double radians = angle * (Math.PI/180);double px=a*System.Math.Cos(radians) ; return px;/求隋圆任意一点y坐标的相对点/角angle,中心点oPoint,a,长半轴,b,短半轴public double GetEllipsePY(double angle,int a,int b)/角double radians = angle * (Math.PI/180);double py=b*System.Math.Sin(radians); return py;/画线椭圆线/角angle,中心点oPoint,a,长半轴,b,短半轴public void DrawEllipseLine(double angle,Point oPoint,int a,int b)int px=System.Convert.ToInt32(GetEllipsePX(angle,a,b)+oPoint.X ;int py=System.Convert.ToInt32(GetEllipsePY(angle,a,b)+oPoint.Y ;Pen PenBlack=new Pen( Color.Black);this.mGraphics.DrawLine( PenBlack,oPoint.X,oPoint.Y,px,py); /e.Graphics.DrawLine( PenBlack,oPoint.X,oPoint.Y,oPoint.X+b,oPoint.Y); /取扇形的点集(逆时针)/角angle,已经画过的角FinishAngle,中心点oPoint,长半轴a,短半轴bpublic ArrayList GetPicPoints(double angle,double FinishAngle,Point oPoint,int a,int b)/PointSystem.Convert.ToInt32(angle) curvePoints=new Array() ;/以步长为1求扇形弧线的坐标点ArrayList pList=new ArrayList() ;pList.Add(oPoint); /pList.Add(ArcStartPoint); for(int i=0;iSystem.Convert.ToInt32(angle);i+)int px=System.Convert.ToInt32(GetEllipsePX(i+FinishAngle,a,b)+oPoint.X ;int py=System.Convert.ToInt32(GetEllipsePY(i+FinishAngle,a,b)+oPoint.Y ;pList.Add(new Point(px,py); /curvePoints.SetValue(return pList;/画扇形(逆时针)/角度angle,已经画过的角FinishAngle,中心点oPoint,长半轴a,短半轴bpublic void DrawPDA

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论