用 C# 编写 Gantt 控件
由于近期公司项目的需要,考虑使用C#来实现Gantt控件。示意图如下(明天补上)。
观察示意图,我们可以发现Gantt图控件主要由三部分组成。日期栏、条目和连接线。我们分别加以处理,首先是显示背景的日期栏。我最初的想法是尽量用标准的.Net控件来实现,第一个找到的是TableLayout,感觉应该很合适,使用Label填充对应的栏位,即可实现。试验结果是刷新很严重。即使采用DoubleBuffer处理也不起什么作用。怒,决意自行绘制表格,遂以Panel替换之。结果出乎想象,使用居然DoubleBuffer来绘制感觉不到闪烁。代码如下:
需要源码的同学请留言
窃喜中……开始绘制条目项,简单使用Label即可。代码如下,不做说明。
protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
{
base.OnPaint(e);
Graphics g = e.Graphics;
SolidBrush brush = new SolidBrush(base.ForeColor);
StringFormat format = new StringFormat();
format.Alignment = StringAlignment.Center;
format.LineAlignment = StringAlignment.Center;
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
g.FillRectangle(brush, 0, 0, base.Width, base.Height / 2);
g.FillPolygon(brush, new Point[] {new Point(0, base.Height / 2),
new Point(6, base.Height), new Point(12, base.Height / 2)});
g.FillPolygon(brush, new Point[] {new Point(base.Width - 12, base.Height / 2),
new Point(base.Width - 6, base.Height), new Point(base.Width, base.Height / 2)});
g.DrawString(this.OrderNO, base.Font, new SolidBrush(Color.WhiteSmoke),
new RectangleF(0.0f, 0.0f, base.Width, base.Height / 2), format);
}
最后一个需要处理的是关联条目的线条和箭头,依然用Label来实现,省时省力。代码如下:
protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
{
base.OnPaint(e);
Graphics g = e.Graphics;
SolidBrush brush = new SolidBrush(base.ForeColor);
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
g.FillRectangle(brush, 0, 0, base.Width - 6 + 1, 2);
g.FillRectangle(brush, base.Width - 6 - 1, 2, 2, base.Height - 8);
g.FillPolygon(brush, new Point[] {new Point(base.Width - 12, base.Height - 8),
new Point(base.Width - 6, base.Height), new Point(base.Width, base.Height - 8)});
}
至此,Gantt图控件大功告成。全部代码暂不提供,自己动手,丰衣足食。完。
- cngator 的blog
- 添加新评论
- 278 次阅读
求代码,急用,以前没接触过GDI++,谢谢,谢谢!万分感谢
太懒了, 上面的代码已经实现最少60%的功能了.
自己动手! 丰衣足食! 有困难可以再来问我.
也求代码阿,最近正要用呢。多谢了阿。
libo_1024_cn@126.com
求代码
joan2005qq@hotmail.com 谢谢
eeew
老大, 省太多了, 能不能写详细一点点, 或把代码发一些给我看一下.
谢谢.