C#開發step步驟條控制項的代碼詳解

來源:互聯網
上載者:User
本篇文章主要介紹了用C#來實現一個step控制項的方法步驟,具有很好的參考價值。下面跟著小編一起來看下吧

現在很多的javascript控制項,非常的不錯,其中step就是一個,如所示:

那麼如何用C#來實現一個step控制項呢?

先定義一個StepEntity類來儲存步驟條節點的資訊:

public class StepEntity {  public string Id { get; set; }  public string StepName { get; set; }  public int StepOrder { get; set; }  public eumStepState StepState { get; set; }  public string StepDesc { get; set; }  public object StepTag { get; set; }  //public Image StepCompletedImage { get; set; }  //public Image StepDoingImage { get; set; }  public StepEntity(string id,string stepname,int steporder,string stepdesc, eumStepState stepstate,object tag)  {   this.Id = id;   this.StepName = stepname;   this.StepOrder = steporder;   this.StepDesc = stepdesc;   this.StepTag = tag;   this.StepState = stepstate;  } }

定義一個名為StepViewer 的使用者控制項。

public partial class StepViewer : UserControl {  public StepViewer()  {   InitializeComponent();   this.Height = 68;  }}

在StepViewer 的使用者控制項中定義一個ListDataSource的屬性,如下:

private List<StepEntity> _dataSourceList = null;  [Browsable(true), Category("StepViewer")]  public List<StepEntity> ListDataSource  {   get   {    return _dataSourceList;   }   set   {    if (_dataSourceList != value)    {     _dataSourceList = value;     Invalidate();    }   }  }

在此控制項的paint方法中,進行步驟條的繪製:

private void StepViewer_Paint(object sender, PaintEventArgs e)  {   if(this.ListDataSource!=null)   {    int CenterY = this.Height / 2;    int index = 1;    int count = ListDataSource.Count;    int lineWidth = 120;    int StepNodeWH = 28;    //this.Width = 32 * count + lineWidth * (count - 1) + 6+300;    //defalut pen & brush    e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;    Brush brush = new SolidBrush(_Gray);    Pen p = new Pen(brush, 1f);    Brush brushNode = new SolidBrush(_DarkGray);    Pen penNode = new Pen(brushNode, 1f);    Brush brushNodeCompleted = new SolidBrush(_Blue);    Pen penNodeCompleted = new Pen(brushNodeCompleted, 1f);    int initX = 6;    //string    Font nFont = new Font("微軟雅黑", 12);    Font stepFont = new Font("微軟雅黑", 11,FontStyle.Bold);    int NodeNameWidth = 0;    foreach (var item in ListDataSource)    {     //round     Rectangle rec = new Rectangle(initX, CenterY - StepNodeWH / 2, StepNodeWH, StepNodeWH);     if (CurrentStep == item.StepOrder)     {      if (item.StepState == eumStepState.OutTime)      {       e.Graphics.DrawEllipse(new Pen(_Red,1f), rec);       e.Graphics.FillEllipse(new SolidBrush(_Red), rec);      }      else      {       e.Graphics.DrawEllipse(penNodeCompleted, rec);       e.Graphics.FillEllipse(brushNodeCompleted, rec);      }      //白色字型      SizeF fTitle = e.Graphics.MeasureString(index.ToString(), stepFont);      Point pTitle = new Point(initX + StepNodeWH / 2 - (int)Math.Round(fTitle.Width) / 2, CenterY - (int)Math.Round(fTitle.Height / 2));      e.Graphics.DrawString(index.ToString(), stepFont, Brushes.White, pTitle);      //nodeName      SizeF sNode = e.Graphics.MeasureString(item.StepName, nFont);      Point pNode = new Point(initX + StepNodeWH, CenterY - (int)Math.Round(sNode.Height / 2) + 2);      e.Graphics.DrawString(item.StepName,new Font( nFont,FontStyle.Bold), brushNode, pNode);      NodeNameWidth = (int)Math.Round(sNode.Width);      if (index < count)      {       e.Graphics.DrawLine(p, initX + StepNodeWH + NodeNameWidth, CenterY, initX + StepNodeWH + NodeNameWidth + lineWidth, CenterY);      }     }     else if (item.StepOrder < CurrentStep)     {      //completed      e.Graphics.DrawEllipse(penNodeCompleted, rec);      //image      RectangleF recRF = new RectangleF(rec.X + 6, rec.Y + 6, rec.Width - 12, rec.Height - 12);      e.Graphics.DrawImage(ControlsResource.check_lightblue, recRF);      //nodeName      SizeF sNode = e.Graphics.MeasureString(item.StepName, nFont);      Point pNode = new Point(initX + StepNodeWH, CenterY - (int)Math.Round(sNode.Height / 2) + 2);      e.Graphics.DrawString(item.StepName, nFont, brushNode, pNode);      NodeNameWidth = (int)Math.Round(sNode.Width);      if (index < count)      {       e.Graphics.DrawLine(penNodeCompleted, initX + StepNodeWH + NodeNameWidth,        CenterY, initX + StepNodeWH + NodeNameWidth + lineWidth, CenterY);      }     }     else     {      e.Graphics.DrawEllipse(p, rec);      //      SizeF fTitle = e.Graphics.MeasureString(index.ToString(), stepFont);      Point pTitle = new Point(initX + StepNodeWH / 2 - (int)Math.Round(fTitle.Width) / 2, CenterY - (int)Math.Round(fTitle.Height / 2));      e.Graphics.DrawString(index.ToString(), stepFont, brush, pTitle);      //nodeName      SizeF sNode = e.Graphics.MeasureString(item.StepName, nFont);      Point pNode = new Point(initX + StepNodeWH, CenterY - (int)Math.Round(sNode.Height / 2) + 2);      e.Graphics.DrawString(item.StepName, nFont, brushNode, pNode);      NodeNameWidth = (int)Math.Round(sNode.Width);      if (index < count)      {       //line       e.Graphics.DrawLine(p, initX + StepNodeWH + NodeNameWidth, CenterY, initX + StepNodeWH + NodeNameWidth + lineWidth, CenterY);      }     }     //描述資訊     if (item.StepDesc != "")     {      Point pNode = new Point(initX + StepNodeWH, CenterY+10);      e.Graphics.DrawString(item.StepDesc,new Font(nFont.FontFamily,10),brush, pNode);     }     index++;     //8 is space width     initX = initX + lineWidth + StepNodeWH+ NodeNameWidth+8;    }   }  }

控制項的使用:

List<StepEntity> list = new List<StepEntity>(); list.Add(new StepEntity("1", "新開單", 1, "這裡是該步驟的描述資訊", eumStepState.Completed, null)); list.Add(new StepEntity("2", "主管審批", 2, "這裡是該步驟的描述資訊", eumStepState.Waiting, null)); list.Add(new StepEntity("3", "總經理審批", 3, "這裡是該步驟的描述資訊", eumStepState.OutTime, null)); list.Add(new StepEntity("2", "完成", 4, "這裡是該步驟的描述資訊", eumStepState.Waiting, null)); this.stepViewer1.CurrentStep = 3; this.stepViewer1.ListDataSource = list;

同樣的,我們可以實現如下的timeline控制項。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.