DevExpress Winforms使用技巧教程:甘特图控件中的自定义绘图

Custom Draw事件

DevExpress WinForms Gantt控件包含以下Custom Draw事件:

  • CustomDrawTask – 允许您在控件的Diagram区域内手动绘制标准任务、摘要任务和里程碑。
  • CustomDrawTaskDependency – 使用此事件来自定义任务依赖性(从一个任务栏指向另一个任务栏的箭头)。
  • CustomTaskDisplayText – 允许您自定义任务标题。
  • CustomDrawTimescaleColumn – 允许您自定义Diagram面板的工作区域。

通过组合这些事件,您可以引入尚未集成到控件中的功能(绘制关键路径、拆分任务、指定截止日期等)。

关键路径

关键路径是一系列非常重要的项目任务,它们之间的延迟为零。 如果您准备好将WinForms Gantt集成到软件项目中,并且等不及下一次重大更新,则可以结合使用CustomDrawTask和CustomDrawDependency事件来手动突出显示与关键路径关联的任务栏以及连接它们的箭头。

DevExpress Winforms使用技巧教程:甘特图控件中的自定义绘图

注意:在此代码示例和其他代码示例中,使用skin colors突出显示元素,这些颜色会根据当前应用的皮肤略微改变色调。

HashSet<int> criticalPathIds = new HashSet<int> { 1, 2, 3, 6, 7, 8, 10, 11, 13 };  ganttControl.CustomDrawTask += (sender, e) => {int taskId = Convert.ToInt32(e.Node.GetValue("Id"));if(criticalPathIds.Contains(taskId)) {e.Appearance.BackColor = DXSkinColors.FillColors.Danger;e.Appearance.ProgressColor = DXSkinColors.FillColors.Danger;}};ganttControl.CustomDrawTaskDependency += (sender, e) => {int predecessorId = Convert.ToInt32(e.PredecessorNode.GetValue("Id"));int successorId = Convert.ToInt32(e.SuccessorNode.GetValue("Id"));if(criticalPathIds.Contains(predecessorId) && criticalPathIds.Contains(successorId)) {e.Appearance.BackColor = DXSkinColors.FillColors.Danger;}};
拆分任务

拆分任务是被中断的任务(在给定的时间),该任务在稍后的时间点恢复。 在下图中,“Develop Software” 是一个3个小时的任务,一分为二,其中有2个小时的暂停。

DevExpress Winforms使用技巧教程:甘特图控件中的自定义绘图
ganttControl.CustomDrawTask += (sender, e) => {var splitInfo = e.Info.Node.GetValue("SplitInfo") as SplitInfo;if(splitInfo != null) {e.Appearance.BackColor = DXSkinColors.FillColors.Danger;e.Appearance.ProgressColor = DXSkinColors.FillColors.Danger;e.DrawShape(splitInfo.Start, splitInfo.Start + splitInfo.Duration);e.DrawRightText();e.Handled = true;}};

一种更有效的方法是将任务持续时间和开始/结束日期存储在数据源中,并在需要的地方插入暂停。 为了支持这种方法,甘特图控件必须能够重新计算所有任务并相应地动态更新其TreeList和Diagram面板。

自定义任务文本

下一个示例说明如何使用自定义文本字符串(“High Priority”或“Normal Priority”)替换任务的标题,这些自定义标题通过使用CustomTaskDisplayText事件绘制在任务的左侧或右侧。

DevExpress Winforms使用技巧教程:甘特图控件中的自定义绘图
HashSet<int> criticalPathIds = new HashSet<int> { 1, 2, 3, 6, 7, 8, 10, 11, 13 };  ganttControl.CustomTaskDisplayText += (sender, e) => {int taskId = Convert.ToInt32(e.Node.GetValue("Id"));if(criticalPathIds.Contains(taskId)) {e.RightText = "High priority";}else {e.RightText = string.Empty;e.LeftText = "Normal priority";}};
Striplines

Stripline是彩色的时间刻度列,突出显示特定时间段(例如,周末)。 在下图中,Stripline突出显示了自定义的4小时间隔。

DevExpress Winforms使用技巧教程:甘特图控件中的自定义绘图

您可以通过CustomDrawTimescaleColumn事件重新绘制时标列来实现Striplines。

DateTime striplineStart = DateTime.Now.AddHours(5);  DateTime striplineEnd = striplineStart.AddHours(4);  Color striplineColor = Color.FromArgb(128, 255, 224, 166);  ganttControl.CustomDrawTimescaleColumn += (sender, e) => {GanttTimescaleColumn column = e.Column;float stripLineStartPoint = (float) Math.Max(e.GetPosition(striplineStart), column.Bounds.Left);float stripLineEndPoint = (float) Math.Min(e.GetPosition(striplineEnd), column.Bounds.Right);e.DrawBackground();RectangleF boundsToDraw = new RectangleF(stripLineStartPoint, column.Bounds.Y,stripLineEndPoint - stripLineStartPoint, column.Bounds.Height);if(boundsToDraw.Width > 0)e.Cache.FillRectangle(striplineColor, boundsToDraw);e.DrawHeader();e.Handled = true;};
截止期限

在下图中,“Deploy Beta”遵循固定的期限。

DevExpress Winforms使用技巧教程:甘特图控件中的自定义绘图

就像之前的代码片段一样,截止日期是通过CustomDrawTimescaleColumn事件绘制的,但是在这种情况下需要绘制一个细矩形,而不是用自定义颜色填充整个列。

DateTime deadLine = TaskStorage.GetFinishDateFromTask("Deploy Beta");  ganttControl.CustomDrawTimescaleColumn += (sender, e) => {GanttTimescaleColumn column = e.Column;if(column.StartDate <= deadLine && column.FinishDate >= deadLine) {e.DrawBackground();float x = (float) e.GetPosition(deadLine);float width = 4;RectangleF deadLineRect = new RectangleF(x, column.Bounds.Y, width, column.Bounds.Height);e.Cache.FillRectangle(DXSkinColors.FillColors.Danger, deadLineRect);e.DrawHeader();e.Handled = true;}};

DevExpress v19.2全新发布,欢迎下载最新版体验哦~

声明:本站部分文章及图片源自用户投稿,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!

上一篇 2019年10月22日
下一篇 2019年10月22日

相关推荐

发表回复

登录后才能评论