程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#基礎知識 >> 用C#創建PDA應用程序的柱形圖控件

用C#創建PDA應用程序的柱形圖控件

編輯:C#基礎知識

  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));
/
            
            
            
          
            
            
            
          
            
            
			

							
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved