技术开发 频道

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


【IT168技术文档】

  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
0
相关文章