【转载】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;
  }
 }

定义步骤状态的枚举。

public enum eumStepState
    {
        Completed,
        Waiting,
        OutTime
    }

定义一个名为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;

 

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C#中的Timer控件是一种计时器控件,可以在指定的时间间隔内执行指定的代码。Timer控件有两种类型:System.Windows.Forms.Timer和System.Timers.Timer。其中,System.Windows.Forms.Timer是Windows窗体应用程序中使用的计时器,而System.Timers.Timer是在非UI线程中使用的计时器。 下面是System.Windows.Forms.Timer控件的详细介绍: 1. Timer控件的属性: - Enabled:获取或设置一个值,该值指示计时器是否应引发Tick事件。 - Interval:获取或设置计时器在引发Tick事件之前等待的时间间隔(以毫秒为单位)。 - Tag:获取或设置与控件关联的对象。 - Site:获取或设置控件的站点。 - CanRaiseEvents:获取一个指示组件是否可以引发事件的值。 - DesignMode:获取一个值,该值指示 Component 当前是否处于设计模式。 - Events:获取附加到此 Component 的事件处理程序的列表。 2. Timer控件的方法: - Start():启动计时器。 - Stop():停止计时器。 3. Timer控件的事件: - Tick:每当计时器间隔时间到达时发生。 下面是一个使用System.Windows.Forms.Timer控件的例子: ```csharp using System; using System.Windows.Forms; public class Form1 : Form { private Timer timer1; public Form1() { InitializeComponent(); timer1 = new Timer(); timer1.Interval = 1000; // 设置计时器间隔为1秒 timer1.Tick += new EventHandler(timer1_Tick); // 绑定Tick事件 timer1.Start(); // 启动计时器 } private void timer1_Tick(object sender, EventArgs e) { // 在这里编写计时器到达间隔时间后要执行的代码 } } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值