精华内容
下载资源
问答
  • C# 甘特图控件 完整源代码 大家可以整到自已系统中去。 C# 甘特图控件 完整源代码 大家可以整到自已系统中去。
  • C#甘特图实现

    2015-12-23 22:08:26
    C#甘特图案例,甘特图实现,可以满足一定需求,还有很多地方需要完善,有兴趣的朋友可以来研究一下。顺便能把完善好的代码发给我更是感激不尽。 邮箱daoxunchang@Foxmail.com
  • C#甘特图控件破解版

    2009-08-06 11:26:54
    本控件已被取消,请不要下载了。C#甘特图控件破解版
  • 一款开源且功能强大的C#甘特图控件.NET Winforms Gantt Chart Control  甘特图在项目管理中非常重要,甘特图的思想比较简单,即以图示的方式通过活动列表和时间刻度形象地表示出任何特定项目的活动顺序与持续时间...

    一款开源且功能强大的C#甘特图控件.NET Winforms Gantt Chart Control

      甘特图在项目管理中非常重要,甘特图的思想比较简单,即以图示的方式通过活动列表和时间刻度形象地表示出任何特定项目的活动顺序与持续时间。它直观地表明任务计划在什么时候进行,及实际进展与计划要求的对比。管理者由此可便利地弄清一项任务(项目)还剩下哪些工作要做,并可评估工作进度。甘特图可以显示几个部门、机器或设备的运行和闲置情况。这表示了该系统的有关工作负荷状况,这样可使管理人员了解何种调整是恰当的。

      由于项目需要,在网上找了很久,经过大量的对比和评估,发现一款真正开源且功能强大的C#甘特图控件.NET Winforms Gantt Chart Control(http://ganttchart.codeplex.com/),效果图如下:

    该款甘特图控件具有如下特征:

    1、独立的时间单位数据结构规范;
    2、支持单任务,分组任务,先例/依赖的任务,可以对任务进行拆分,并附加资源信息;
    3、打印支持;
    4、可对任务的计划和实际进行对比,以百分比进行进度跟踪;
    5、在直接在甘特图上,对各种鼠标事件进行UI定制;
    6、可以通过继承来修改默认的鼠标命令;
    7、支持关键路径。

     

    官方演示代码为:

      1 using System;
      2 using System.Collections.Generic;
      3 using System.ComponentModel;
      4 using System.Data;
      5 using System.Drawing;
      6 using System.Linq;
      7 using System.Text;
      8 using System.Windows.Forms;
      9 
     10 namespace Braincase.GanttChart
     11 {
     12     /// <summary>
     13     /// An elaborate example on how the chart control might be used. 
     14     /// Start by collapsing all regions and then read the constructor.
     15     /// Refer to IProjectManager interface for method description.
     16     /// </summary>
     17     public partial class ExampleFull : Form
     18     {
     19         OverlayPainter _mOverlay = new OverlayPainter();
     20 
     21         ProjectManager _mManager = null;
     22 
     23         /// <summary>
     24         /// Example starts here
     25         /// </summary>
     26         public ExampleFull()
     27         {
     28             InitializeComponent();
     29 
     30             // Create a Project and some Tasks
     31             _mManager = new ProjectManager();
     32             var work = new MyTask(_mManager) { Name = "Prepare for Work" };
     33             var wake = new MyTask(_mManager) { Name = "Wake Up" };
     34             var teeth = new MyTask(_mManager) { Name = "Brush Teeth" };
     35             var shower = new MyTask(_mManager) { Name = "Shower" };
     36             var clothes = new MyTask(_mManager) { Name = "Change into New Clothes" };
     37             var hair = new MyTask(_mManager) { Name = "Blow My Hair" };
     38             var pack = new MyTask(_mManager) { Name = "Pack the Suitcase" };
     39 
     40             _mManager.Add(work);
     41             _mManager.Add(wake);
     42             _mManager.Add(teeth);
     43             _mManager.Add(shower);
     44             _mManager.Add(clothes);
     45             _mManager.Add(hair);
     46             _mManager.Add(pack);
     47 
     48             // Create another 1000 tasks for stress testing
     49             Random rand = new Random();
     50             for (int i = 0; i < 1000; i++)
     51             {
     52                 var task = new MyTask(_mManager) { Name = string.Format("New Task {0}", i.ToString()) };
     53                 _mManager.Add(task);
     54                 _mManager.SetStart(task, rand.Next(300));
     55                 _mManager.SetDuration(task, rand.Next(50));
     56             }
     57 
     58             // Set task durations, e.g. using ProjectManager methods 
     59             _mManager.SetDuration(wake, 3);
     60             _mManager.SetDuration(teeth, 5);
     61             _mManager.SetDuration(shower, 7);
     62             _mManager.SetDuration(clothes, 4);
     63             _mManager.SetDuration(hair, 3);
     64             _mManager.SetDuration(pack, 5);
     65 
     66             // demostrate splitting a task
     67             _mManager.Split(pack, new MyTask(_mManager), new MyTask(_mManager), 2);
     68 
     69             // Set task complete status, e.g. using newly created properties
     70             wake.Complete = 0.9f;
     71             teeth.Complete = 0.5f;
     72             shower.Complete = 0.4f;
     73 
     74             // Give the Tasks some organisation, setting group and precedents
     75             _mManager.Group(work, wake);
     76             _mManager.Group(work, teeth);
     77             _mManager.Group(work, shower);
     78             _mManager.Group(work, clothes);
     79             _mManager.Group(work, hair);
     80             _mManager.Group(work, pack);
     81             _mManager.Relate(wake, teeth);
     82             _mManager.Relate(wake, shower);
     83             _mManager.Relate(shower, clothes);
     84             _mManager.Relate(shower, hair);
     85             _mManager.Relate(hair, pack);
     86             _mManager.Relate(clothes, pack);
     87 
     88             // Create and assign Resources.
     89             // MyResource is just custom user class. The API can accept any object as resource.
     90             var jake = new MyResource() { Name = "Jake" };
     91             var peter = new MyResource() { Name = "Peter" };
     92             var john = new MyResource() { Name = "John" };
     93             var lucas = new MyResource() { Name = "Lucas" };
     94             var james = new MyResource() { Name = "James" };
     95             var mary = new MyResource() { Name = "Mary" };
     96             // Add some resources
     97             _mManager.Assign(wake, jake);
     98             _mManager.Assign(wake, peter);
     99             _mManager.Assign(wake, john);
    100             _mManager.Assign(teeth, jake);
    101             _mManager.Assign(teeth, james);
    102             _mManager.Assign(pack, james);
    103             _mManager.Assign(pack, lucas);
    104             _mManager.Assign(shower, mary);
    105             _mManager.Assign(shower, lucas);
    106             _mManager.Assign(shower, john);
    107 
    108             // Initialize the Chart with our ProjectManager and CreateTaskDelegate
    109             _mChart.Init(_mManager);
    110             _mChart.CreateTaskDelegate = delegate() { return new MyTask(_mManager); };
    111 
    112             // Attach event listeners for events we are interested in
    113             _mChart.TaskMouseOver += new EventHandler<TaskMouseEventArgs>(_mChart_TaskMouseOver);
    114             _mChart.TaskMouseOut += new EventHandler<TaskMouseEventArgs>(_mChart_TaskMouseOut);
    115             _mChart.TaskSelected += new EventHandler<TaskMouseEventArgs>(_mChart_TaskSelected);
    116             _mChart.PaintOverlay += _mOverlay.ChartOverlayPainter;
    117             _mChart.AllowTaskDragDrop = true;
    118 
    119             // set some tooltips to show the resources in each task
    120             _mChart.SetToolTip(wake, string.Join(", ", _mManager.ResourcesOf(wake).Select(x => (x as MyResource).Name)));
    121             _mChart.SetToolTip(teeth, string.Join(", ", _mManager.ResourcesOf(teeth).Select(x => (x as MyResource).Name)));
    122             _mChart.SetToolTip(pack, string.Join(", ", _mManager.ResourcesOf(pack).Select(x => (x as MyResource).Name)));
    123             _mChart.SetToolTip(shower, string.Join(", ", _mManager.ResourcesOf(shower).Select(x => (x as MyResource).Name)));
    124 
    125             // Set Time information
    126             _mManager.TimeScale = TimeScale.Day;
    127             var span = DateTime.Today - _mManager.Start;
    128             _mManager.Now = (int)Math.Round(span.TotalDays); // set the "Now" marker at the correct date
    129             _mChart.TimeScaleDisplay = TimeScaleDisplay.DayOfWeek; // Set the chart to display days of week in header
    130 
    131             // Init the rest of the UI
    132             _InitExampleUI();            
    133         }
    134 
    135         void _mChart_TaskSelected(object sender, TaskMouseEventArgs e)
    136         {
    137             _mTaskGrid.SelectedObjects = _mChart.SelectedTasks.Select(x => _mManager.IsPart(x) ? _mManager.SplitTaskOf(x) : x).ToArray();
    138             _mResourceGrid.Items.Clear();
    139             _mResourceGrid.Items.AddRange(_mManager.ResourcesOf(e.Task).Select(x => new ListViewItem(((MyResource)x).Name)).ToArray());
    140         }
    141 
    142         void _mChart_TaskMouseOut(object sender, TaskMouseEventArgs e)
    143         {
    144             lblStatus.Text = "";
    145             _mChart.Invalidate();
    146         }
    147 
    148         void _mChart_TaskMouseOver(object sender, TaskMouseEventArgs e)
    149         {
    150             lblStatus.Text = string.Format("{0} to {1}", _mManager.GetDateTime(e.Task.Start).ToLongDateString(), _mManager.GetDateTime(e.Task.End).ToLongDateString());
    151             _mChart.Invalidate();
    152         }
    153 
    154         private void _InitExampleUI()
    155         {
    156             TaskGridView.DataSource = new BindingSource(_mManager.Tasks, null);
    157             mnuFilePrint200.Click += (s, e) => _PrintDocument(2.0f);
    158             mnuFilePrint150.Click += (s, e) => _PrintDocument(1.5f);
    159             mnuFilePrint100.Click += (s, e) => _PrintDocument(1.0f);
    160             mnuFilePrint80.Click += (s, e) => _PrintDocument(0.8f);
    161             mnuFilePrint50.Click += (s, e) => _PrintDocument(0.5f);
    162             mnuFilePrint25.Click += (s, e) => _PrintDocument(0.25f);
    163             mnuFilePrint10.Click += (s, e) => _PrintDocument(0.1f);
    164 
    165             mnuFileImgPrint100.Click += (s, e) => _PrintImage(1.0f);
    166             mnuFileImgPrint50.Click += (s, e) => _PrintImage(0.5f);
    167             mnuFileImgPrint10.Click += (s, e) => _PrintImage(0.1f);            
    168         }
    169 
    170         #region Main Menu
    171 
    172         private void mnuFileSave_Click(object sender, EventArgs e)
    173         {
    174             using (var dialog = new SaveFileDialog())
    175             {
    176                 dialog.InitialDirectory = System.IO.Path.GetDirectoryName(Application.ExecutablePath);
    177                 if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
    178                 {
    179                     using (var fs = System.IO.File.OpenWrite(dialog.FileName))
    180                     {
    181                         System.Runtime.Serialization.Formatters.Binary.BinaryFormatter bf = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
    182                         bf.Serialize(fs, _mManager);
    183                     }
    184                 }
    185             }
    186         }
    187 
    188         private void mnuFileOpen_Click(object sender, EventArgs e)
    189         {
    190             using (var dialog = new OpenFileDialog())
    191             {
    192                 dialog.InitialDirectory = System.IO.Path.GetDirectoryName(Application.ExecutablePath);
    193                 if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
    194                 {
    195                     using (var fs = System.IO.File.OpenRead(dialog.FileName))
    196                     {
    197                         System.Runtime.Serialization.Formatters.Binary.BinaryFormatter bf = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
    198                         _mManager = bf.Deserialize(fs) as ProjectManager;
    199                         if (_mManager == null)
    200                         {
    201                             MessageBox.Show("Unable to load ProjectManager. Data structure might have changed from previous verions", "Gantt Chart", MessageBoxButtons.OK, MessageBoxIcon.Error);
    202                         }
    203                         else
    204                         {
    205                             _mChart.Init(_mManager);
    206                             _mChart.Invalidate();
    207                         }
    208                     }
    209                 }
    210             }
    211         }
    212 
    213         private void mnuFileExit_Click(object sender, EventArgs e)
    214         {
    215             this.Close();
    216         }
    217 
    218         private void mnuViewDaysDayOfWeek_Click(object sender, EventArgs e)
    219         {
    220             _mManager.TimeScale = TimeScale.Day;
    221             _mChart.TimeScaleDisplay = TimeScaleDisplay.DayOfWeek;
    222             _mChart.Invalidate();
    223         }
    224 
    225         private void mnuFileNew_Click(object sender, EventArgs e)
    226         {
    227             // start a new Project and init the chart with the project
    228             _mManager = new ProjectManager();
    229             _mManager.Add(new Task() { Name = "New Task" });
    230             _mChart.Init(_mManager);
    231             _mChart.Invalidate();
    232         }
    233 
    234         private void mnuHelpAbout_Click(object sender, EventArgs e)
    235         {
    236             if (MessageBox.Show("Please visit http://www.jakesee.com/net-c-winforms-gantt-chart-control/ for more help and details", "Braincase Solutions - Gantt Chart", MessageBoxButtons.OKCancel) == System.Windows.Forms.DialogResult.OK)
    237             {
    238                 System.Diagnostics.Process.Start("http://www.jakesee.com/net-c-winforms-gantt-chart-control/");
    239             }
    240         }
    241 
    242         private void mnuViewRelationships_Click(object sender, EventArgs e)
    243         {
    244             _mChart.ShowRelations = mnuViewRelationships.Checked = !mnuViewRelationships.Checked;
    245             _mChart.Invalidate();
    246         }
    247 
    248         private void mnuViewSlack_Click(object sender, EventArgs e)
    249         {
    250             _mChart.ShowSlack = mnuViewSlack.Checked = !mnuViewSlack.Checked;
    251             _mChart.Invalidate();
    252         }
    253 
    254         private void mnuViewIntructions_Click(object sender, EventArgs e)
    255         {
    256             _mOverlay.PrintMode = !(mnuViewIntructions.Checked = !mnuViewIntructions.Checked);
    257             _mChart.Invalidate();
    258         }
    259 
    260         #region Timescale Views
    261         private void mnuViewDays_Click(object sender, EventArgs e)
    262         {
    263             _mManager.TimeScale = TimeScale.Day;
    264             mnuViewDays.Checked = true;
    265             mnuViewWeek.Checked = false;
    266             _mChart.Invalidate();
    267         }
    268 
    269         private void mnuViewWeek_Click(object sender, EventArgs e)
    270         {
    271             _mManager.TimeScale = TimeScale.Week;
    272             mnuViewDays.Checked = false;
    273             mnuViewWeek.Checked = true;
    274             _mChart.Invalidate();
    275         }
    276 
    277         private void mnuViewDayOfWeek_Click(object sender, EventArgs e)
    278         {
    279             _mChart.TimeScaleDisplay = TimeScaleDisplay.DayOfWeek;
    280             mnuViewDayOfWeek.Checked = true;
    281             mnuViewDayOfMonth.Checked = false;
    282             mnuViewWeekOfYear.Checked = false;
    283             _mChart.Invalidate();
    284         }
    285 
    286         private void mnuViewDayOfMonth_Click(object sender, EventArgs e)
    287         {
    288             _mChart.TimeScaleDisplay = TimeScaleDisplay.DayOfMonth;
    289             mnuViewDayOfWeek.Checked = false;
    290             mnuViewDayOfMonth.Checked = true;
    291             mnuViewWeekOfYear.Checked = false;
    292             _mChart.Invalidate();
    293         }
    294 
    295         private void mnuViewWeekOfYear_Click(object sender, EventArgs e)
    296         {
    297             _mChart.TimeScaleDisplay = TimeScaleDisplay.WeekOfYear;
    298             mnuViewDayOfWeek.Checked = false;
    299             mnuViewDayOfMonth.Checked = false;
    300             mnuViewWeekOfYear.Checked = true;
    301             _mChart.Invalidate();
    302         }
    303         #endregion Timescale Views
    304 
    305         #endregion Main Menu
    306 
    307         #region Sidebar
    308 
    309         private void _mDateTimePicker_ValueChanged(object sender, EventArgs e)
    310         {
    311             _mManager.Start = _mStartDatePicker.Value;
    312             var span = DateTime.Today - _mManager.Start;
    313             _mManager.Now = (int)Math.Round(span.TotalDays);
    314             if (_mManager.TimeScale == TimeScale.Week) _mManager.Now = (_mManager.Now % 7) * 7;
    315             _mChart.Invalidate();
    316         }
    317 
    318         private void _mPropertyGrid_SelectedGridItemChanged(object sender, SelectedGridItemChangedEventArgs e)
    319         {
    320             _mChart.Invalidate();
    321         }
    322 
    323         private void _mNowDatePicker_ValueChanged(object sender, EventArgs e)
    324         {
    325             TimeSpan span = _mNowDatePicker.Value - _mStartDatePicker.Value;
    326             _mManager.Now = span.Days + 1;
    327             if (_mManager.TimeScale == TimeScale.Week) _mManager.Now = _mManager.Now / 7 + (_mManager.Now % 7 > 0 ? 1 : 0);
    328             _mChart.Invalidate();
    329         }
    330 
    331         private void _mScrollDatePicker_ValueChanged(object sender, EventArgs e)
    332         {
    333             _mChart.ScrollTo(_mScrollDatePicker.Value);
    334             _mChart.Invalidate();
    335         }
    336 
    337         private void _mTaskGridView_SelectionChanged(object sender, EventArgs e)
    338         {
    339             if (TaskGridView.SelectedRows.Count > 0)
    340             {
    341                 var task = TaskGridView.SelectedRows[0].DataBoundItem as Task;
    342                 _mChart.ScrollTo(task);
    343             }
    344         }
    345 
    346         #endregion Sidebar
    347 
    348         #region Print
    349 
    350         private void _PrintDocument(float scale)
    351         {
    352             using (var dialog = new PrintDialog())
    353             {
    354                 dialog.Document = new System.Drawing.Printing.PrintDocument();
    355                 if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
    356                 {
    357                     // set the print mode for the custom overlay painter so that we skip printing instructions
    358                     dialog.Document.BeginPrint += (s, arg) => _mOverlay.PrintMode = true;
    359                     dialog.Document.EndPrint += (s, arg) => _mOverlay.PrintMode = false;
    360 
    361                     // tell chart to print to the document at the specified scale
    362                     _mChart.Print(dialog.Document, scale);
    363                 }
    364             }
    365         }
    366 
    367         private void _PrintImage(float scale)
    368         {
    369             using (var dialog = new SaveFileDialog())
    370             {
    371                 dialog.Filter = "Bitmap (*.bmp) | *.bmp";
    372                 if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
    373                 {
    374                     // set the print mode for the custom overlay painter so that we skip printing instructions
    375                     _mOverlay.PrintMode = true;
    376                     // tell chart to print to the document at the specified scale
    377 
    378                     var bitmap = _mChart.Print(scale);
    379                     _mOverlay.PrintMode = false; // restore printing overlays
    380 
    381                     bitmap.Save(dialog.FileName, System.Drawing.Imaging.ImageFormat.Bmp);
    382                 }
    383             }
    384         }
    385 
    386         #endregion Print        
    387 
    388         
    389     }
    390 
    391     #region overlay painter
    392     /// <summary>
    393     /// An example of how to encapsulate a helper painter for painter additional features on Chart
    394     /// </summary>
    395     public class OverlayPainter
    396     {
    397         /// <summary>
    398         /// Hook such a method to the chart paint event listeners
    399         /// </summary>
    400         /// <param name="sender"></param>
    401         /// <param name="e"></param>
    402         public void ChartOverlayPainter(object sender, ChartPaintEventArgs e)
    403         {
    404             // Don't want to print instructions to file
    405             if (this.PrintMode) return;
    406 
    407             var g = e.Graphics;
    408             var chart = e.Chart;
    409 
    410             // Demo: Static billboards begin -----------------------------------
    411             // Demonstrate how to draw static billboards
    412             // "push matrix" -- save our transformation matrix
    413             e.Chart.BeginBillboardMode(e.Graphics);
    414 
    415             // draw mouse command instructions
    416             int margin = 300;
    417             int left = 20;
    418             var color = chart.HeaderFormat.Color;
    419             StringBuilder builder = new StringBuilder();
    420             builder.AppendLine("THIS IS DRAWN BY A CUSTOM OVERLAY PAINTER TO SHOW DEFAULT MOUSE COMMANDS.");
    421             builder.AppendLine("*******************************************************************************************************");
    422             builder.AppendLine("Left Click - Select task and display properties in PropertyGrid");
    423             builder.AppendLine("Left Mouse Drag - Change task starting point");
    424             builder.AppendLine("Right Mouse Drag - Change task duration");
    425             builder.AppendLine("Middle Mouse Drag - Change task complete percentage");
    426             builder.AppendLine("Left Doubleclick - Toggle collaspe on task group");
    427             builder.AppendLine("Right Doubleclick - Split task into task parts");
    428             builder.AppendLine("Left Mouse Dragdrop onto another task - Group drag task under drop task");
    429             builder.AppendLine("Right Mouse Dragdrop onto another task part - Join task parts");
    430             builder.AppendLine("SHIFT + Left Mouse Dragdrop onto another task - Make drop task precedent of drag task");
    431             builder.AppendLine("ALT + Left Dragdrop onto another task - Ungroup drag task from drop task / Remove drop task from drag task precedent list");
    432             builder.AppendLine("SHIFT + Left Mouse Dragdrop - Order tasks");
    433             builder.AppendLine("SHIFT + Middle Click - Create new task");
    434             builder.AppendLine("ALT + Middle Click - Delete task");
    435             builder.AppendLine("Left Doubleclick - Toggle collaspe on task group");
    436             var size = g.MeasureString(builder.ToString(), e.Chart.Font);
    437             var background = new Rectangle(left, chart.Height - margin, (int)size.Width, (int)size.Height);
    438             background.Inflate(10, 10);
    439             g.FillRectangle(new System.Drawing.Drawing2D.LinearGradientBrush(background, Color.LightYellow, Color.Transparent, System.Drawing.Drawing2D.LinearGradientMode.Vertical), background);
    440             g.DrawRectangle(Pens.Brown, background);
    441             g.DrawString(builder.ToString(), chart.Font, color, new PointF(left, chart.Height - margin));
    442 
    443             
    444             // "pop matrix" -- restore the previous matrix
    445             e.Chart.EndBillboardMode(e.Graphics);
    446             // Demo: Static billboards end -----------------------------------
    447         }
    448 
    449         public bool PrintMode { get; set; }
    450     }
    451     #endregion overlay painter
    452 
    453     #region custom task and resource
    454     /// <summary>
    455     /// A custom resource of your own type (optional)
    456     /// </summary>
    457     [Serializable]
    458     public class MyResource
    459     {
    460         public string Name { get; set; }
    461     }
    462     /// <summary>
    463     /// A custom task of your own type deriving from the Task interface (optional)
    464     /// </summary>
    465     [Serializable]
    466     public class MyTask : Task
    467     {
    468         public MyTask(ProjectManager manager)
    469             : base()
    470         {
    471             Manager = manager;
    472         }
    473 
    474         private ProjectManager Manager { get; set; }
    475 
    476         public new int Start { get { return base.Start; } set { Manager.SetStart(this, value); } }
    477         public new int End { get { return base.End; } set { Manager.SetEnd(this, value); } }
    478         public new int Duration { get { return base.Duration; } set { Manager.SetDuration(this, value); } }
    479         public new float Complete { get { return base.Complete; } set { Manager.SetComplete(this, value); } }
    480     }
    481     #endregion custom task and resource
    482 }
    

     

    演示效果图为:

     

    水平有限,望各位园友不吝赐教!如果觉得不错,请点击推荐和关注! 
    出处:http://www.cnblogs.com/isaboy/ 
    声明:本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

    展开全文
  • C#甘特图(js实现)

    热门讨论 2010-01-26 14:10:21
    全js甘特图,可以用在任意技术上 此例是C#与后台交互的甘特图源码,里面有数据库文件需要还原
  • C#生成甘特图看工程进度

    热门讨论 2009-04-22 22:40:25
    甘特图控件 看工程进度 C#生成甘特图 甘特图控件 看工程进度 C#生成甘特图
  • C# VS2010 甘特图

    2016-03-16 14:44:43
    C# gantt 甘特图, 完整源代码 , VS2010 C# 运行通过, 可以自由扩展。
  • C#制作甘特图

    2012-04-12 09:48:00
    先自制甘特图控件: using System;using System.Collections.Generic;using System.Text;using System.Drawing;using System.Windows.Forms;using System.Drawing.Drawing2D;using System.ComponentModel; ...

         先自制甘特图控件:

     

    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Drawing;
    using System.Windows.Forms;
    using System.Drawing.Drawing2D;
    using System.ComponentModel;

    namespace GanttControls
    {

        /// <summary>  
        /// 甘特图控件  
        /// zgke@sina.com  
        /// qq:116149  
        /// </summary>  
        public class ProjectViewBox : UserControl
        {
            #region 容器
            #region 设计器代码
            /// <summary>   
            /// 必需的设计器变量。  
            /// </summary>  
            private System.ComponentModel.IContainer components = null;

            /// <summary>   
            /// 清理所有正在使用的资源。  
            /// </summary>  
            /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>  
            protected override void Dispose(bool disposing)
            {
                if (disposing && (components != null))
                {
                    components.Dispose();
                }
                base.Dispose(disposing);
            }

            #region 组件设计器生成的代码

            /// <summary>   
            /// 设计器支持所需的方法 - 不要  
            /// 使用代码编辑器修改此方法的内容。  
            /// </summary>  
            private void InitializeComponent()
            {
                this.hScrollBar1 = new System.Windows.Forms.HScrollBar();
                this.vScrollBar1 = new System.Windows.Forms.VScrollBar();
                this.MainPictureBox = new System.Windows.Forms.PictureBox();
                ((System.ComponentModel.ISupportInitialize)(this.MainPictureBox)).BeginInit();
                this.SuspendLayout();
                //   
                // hScrollBar1  
                //   
                this.hScrollBar1.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)
                            | System.Windows.Forms.AnchorStyles.Right)));
                this.hScrollBar1.LargeChange = 1;
                this.hScrollBar1.Location = new System.Drawing.Point(1, 111);
                this.hScrollBar1.Maximum = 60;
                this.hScrollBar1.Name = "hScrollBar1";
                this.hScrollBar1.Size = new System.Drawing.Size(120, 16);
                this.hScrollBar1.TabIndex = 0;
                //   
                // vScrollBar1  
                //   
                this.vScrollBar1.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
                            | System.Windows.Forms.AnchorStyles.Right)));
                this.vScrollBar1.LargeChange = 1;
                this.vScrollBar1.Location = new System.Drawing.Point(121, 0);
                this.vScrollBar1.Maximum = 60;
                this.vScrollBar1.Name = "vScrollBar1";
                this.vScrollBar1.Size = new System.Drawing.Size(16, 111);
                this.vScrollBar1.TabIndex = 1;
                //   
                // MainPictureBox  
                //   
                this.MainPictureBox.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
                            | System.Windows.Forms.AnchorStyles.Left)
                            | System.Windows.Forms.AnchorStyles.Right)));
                this.MainPictureBox.Location = new System.Drawing.Point(0, 0);
                this.MainPictureBox.Name = "MainPictureBox";
                this.MainPictureBox.Size = new System.Drawing.Size(120, 109);
                this.MainPictureBox.TabIndex = 2;
                this.MainPictureBox.TabStop = false;
                //   
                // ProjectBox  
                //   
                this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
                this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
                this.BackColor = System.Drawing.SystemColors.Control;
                this.Controls.Add(this.vScrollBar1);
                this.Controls.Add(this.hScrollBar1);
                this.Controls.Add(this.MainPictureBox);
                this.Name = "ProjectBox";
                this.Size = new System.Drawing.Size(139, 129);
                this.Load += new System.EventHandler(this.ProjectBox_Load);
                this.SizeChanged += new System.EventHandler(this.ProjectBox_SizeChanged);
                ((System.ComponentModel.ISupportInitialize)(this.MainPictureBox)).EndInit();
                this.ResumeLayout(false);

            }

            #endregion

            private System.Windows.Forms.HScrollBar hScrollBar1;
            private System.Windows.Forms.VScrollBar vScrollBar1;
            private System.Windows.Forms.PictureBox MainPictureBox;
            #endregion

            public ProjectViewBox()
            {
                InitializeComponent();
            }

            #region 私有成员
            /// <summary>  
            /// 最小单元格  
            /// </summary>  
            private int m_Size = 18;
            /// <summary>  
            /// 日期索引 记录当前绘制的位置  
            /// </summary>  
            private int m_DayIndex = 0;
            /// <summary>  
            /// 开始绘制位置  
            /// </summary>  
            private int m_JobIndex = 0;
            #endregion

            /// <summary>  
            /// 大小改变  
            /// </summary>  
            /// <param name="sender"></param>  
            /// <param name="e"></param>  
            private void ProjectBox_SizeChanged(object sender, EventArgs e)
            {
                if (MainPictureBox.Image != null) PaintProjectImage();
            }

            private MyScroll Hscroll;
            private MyScroll Vscroll;

            private void ProjectBox_Load(object sender, EventArgs e)
            {
                MainPictureBox.Image = new Bitmap(1, 1);
                PaintProjectImage();
                Hscroll = new MyScroll();
                Hscroll.Click += new MyScroll.ScrollClick(Hscroll_Click);
                Hscroll.AssignHandle(hScrollBar1.Handle);

                Vscroll = new MyScroll();
                Vscroll.Click += new MyScroll.ScrollClick(Vscroll_Click);
                Vscroll.AssignHandle(vScrollBar1.Handle);

                m_ProjectSelect.ProjectBox = this;
            }

            #region 滚动条
            void Vscroll_Click(int p_Value)
            {
                switch (p_Value)
                {
                    case 0:
                        m_JobIndex = vScrollBar1.Value;
                        break;
                    case 1:
                        m_JobIndex--;
                        if (m_JobIndex < 0) m_JobIndex = 0;
                        break;
                    case 2:
                        m_JobIndex++;
                        break;

                }
                if (m_JobIndex <= 0) vScrollBar1.Value = 0;
                if (m_JobIndex >= 60) vScrollBar1.Value = 60;
                PaintProjectImage();
            }

            void Hscroll_Click(int p_Value)
            {
                switch (p_Value)
                {
                    case 0:
                        m_DayIndex = hScrollBar1.Value;
                        break;
                    case 1:
                        m_DayIndex--;
                        break;
                    case 2:
                        m_DayIndex++;
                        break;

                }
                if (m_DayIndex <= 0) hScrollBar1.Value = 0;
                if (m_DayIndex >= 60) hScrollBar1.Value = 60;
                PaintProjectImage();
            }
            #endregion

            /// <summary>  
            /// 刷新  
            /// </summary>  
            public override void Refresh()
            {
                PaintProjectImage();
            }

            /// <summary>  
            /// 开始绘制  
            /// </summary>  
            private void PaintProjectImage()
            {
                MainPictureBox.Image.Dispose();
                if (MainPictureBox.Width == 0 || MainPictureBox.Height == 0) return;
                Bitmap _NewBitmap = new Bitmap(MainPictureBox.Width, MainPictureBox.Height);
                Graphics _Graphics = Graphics.FromImage(_NewBitmap);
                Point _DrawPoint = new Point(0, 0);
                DateTime _DrawDate = DateTime.Now.AddDays(m_DayIndex);
                while (true)
                {
                    _DrawPoint = GraphicsWeekHead(_Graphics, ref _DrawDate, _DrawPoint);

                    if (_DrawPoint.X > MainPictureBox.Width) break;
                }

                DateTime _NodeStarDate = DateTime.Now.AddDays(m_DayIndex);
                int _Hight = m_Size * 2;
                for (int i = m_JobIndex; i < m_ProjectSelect.Count; i++)
                {
                    m_ProjectSelect[i].GraphisDraw(_Graphics, _NodeStarDate, m_Size, ref _Hight, MainPictureBox.Width, MainPictureBox.Height);
                }

                MainPictureBox.Image = _NewBitmap;
            }

            /// <summary>  
            /// 绘制日期头和星期几  
            /// </summary>  
            /// <param name="p_Graphics">刷子</param>  
            /// <param name="p_StarDate">开始日期 ref类型 返回日期的下周一</param>  
            /// <param name="p_StarPoint">开始位置</param>  
            /// <returns>结束节点</returns>  
            private Point GraphicsWeekHead(Graphics p_Graphics, ref DateTime p_StarDate, Point p_StarPoint)
            {
                int _Date = (int)p_StarDate.DayOfWeek;
                if (_Date != 0) p_StarDate = p_StarDate.AddDays(-_Date);
                int _DrawDateX = p_StarPoint.X - (_Date * m_Size);
                Pen _LinePen = new Pen(Brushes.Black, 1);
                Rectangle _RectangleHead = new Rectangle(_DrawDateX, 0, m_Size * 7, m_Size);
                StringFormat _StringFormat = new StringFormat();
                _StringFormat.LineAlignment = StringAlignment.Far;

                p_Graphics.DrawRectangle(_LinePen, _RectangleHead);
                p_Graphics.DrawString(p_StarDate.ToString("yyyy年MM月dd日"), this.Font, Brushes.Black, _RectangleHead, _StringFormat);

                _RectangleHead = new Rectangle(p_StarPoint.X, m_Size, p_StarPoint.X + m_Size, m_Size);
                Rectangle _BlackRect = new Rectangle(0, m_Size * 2, m_Size, this.Height - (m_Size * 2));

                Color _BlackColor = m_ProjectSelect.BackColor;

                for (int i = _Date; i != 7; i++)
                {
                    string _WeekText = string.Empty;
                    switch (i)
                    {
                        case 0:
                            _WeekText = "日";
                            _BlackColor = m_ProjectSelect.WeekBackColor;
                            break;
                        case 1:
                            _WeekText = "一";
                            _BlackColor = m_ProjectSelect.BackColor;
                            break;
                        case 2:
                            _WeekText = "二";
                            _BlackColor = m_ProjectSelect.BackColor;
                            break;
                        case 3:
                            _WeekText = "三";
                            _BlackColor = m_ProjectSelect.BackColor;
                            break;
                        case 4:
                            _WeekText = "四";
                            _BlackColor = m_ProjectSelect.BackColor;
                            break;
                        case 5:
                            _WeekText = "五";
                            _BlackColor = m_ProjectSelect.BackColor;
                            break;
                        case 6:
                            _WeekText = "六";
                            _BlackColor = m_ProjectSelect.WeekBackColor;
                            break;
                    }
                    HatchBrush _BlackBrush = new HatchBrush(HatchStyle.Percent50, _BlackColor, Color.White);
                    _BlackRect.X = _RectangleHead.X;
                    p_Graphics.FillRectangle(_BlackBrush, _BlackRect);

                    p_Graphics.DrawRectangle(_LinePen, _RectangleHead);
                    p_Graphics.DrawString(_WeekText, new Font("宋体", 9.0f), GetWorkColumneColor(p_StarDate, i), _RectangleHead, _StringFormat);
                    _RectangleHead.X += m_Size;
                }
                p_StarDate = p_StarDate.AddDays(7);
                return new Point(_RectangleHead.X, m_Size * 2);
            }

            /// <summary>  
            /// 获取日期的标题色  
            /// </summary>  
            /// <param name="p_Date">日期</param>  
            /// <param name="p_Day">1-7</param>  
            /// <returns></returns>  
            public SolidBrush GetWorkColumneColor(DateTime p_Date, int p_Day)
            {
                DateTime _ValueDate = p_Date.AddDays(p_Day);

                if (_ValueDate.DayOfWeek == DayOfWeek.Saturday && !m_ProjectSelect.SaturdayOfWork)
                {
                    return new SolidBrush(m_ProjectSelect.ColumnHolidayColor);
                }
                if (_ValueDate.DayOfWeek == DayOfWeek.Sunday && !m_ProjectSelect.SundayOfWork)
                {
                    return new SolidBrush(m_ProjectSelect.ColumnHolidayColor);
                }

                for (int z = 0; z != m_ProjectSelect.HolidayList.Count; z++)
                {
                    bool _Year = m_ProjectSelect.HolidayList[z].Year == _ValueDate.Year;
                    bool _Month = m_ProjectSelect.HolidayList[z].Month == _ValueDate.Month;
                    bool _Day = m_ProjectSelect.HolidayList[z].Day == _ValueDate.Day;

                    if (_Year && _Month && _Day)
                    {
                        return new SolidBrush(m_ProjectSelect.ColumnHolidayColor);
                    }
                }

                return new SolidBrush(m_ProjectSelect.ColumnWorkDayColor);
            }

            #region 公用属性
            private ProjectSelect m_ProjectSelect = new ProjectSelect();
            /// <summary>  
            /// 基本设置  
            /// </summary>       
            [Browsable(false)]
            [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
            public ProjectSelect ProjectSelectData
            {
                get { return m_ProjectSelect; }
                set { m_ProjectSelect = value; m_ProjectSelect.ProjectBox = this; }
            }
            #endregion

            /// <summary>  
            /// 实在写不好Scroll 判断消息了  
            /// </summary>  
            private class MyScroll : NativeWindow
            {
                public delegate void ScrollClick(int p_Value);
                public event ScrollClick Click;

                protected override void WndProc(ref Message m)
                {
                    base.WndProc(ref m);
                    int _ClickType = -1;
                    switch (m.Msg)
                    {
                        case 0xEa:
                            _ClickType = 0;
                            break;
                        case 0x2114:
                            switch (m.WParam.ToInt32())
                            {
                                case 0:
                                    _ClickType = 1;
                                    break;
                                case 1:
                                    _ClickType = 2;
                                    break;
                                case 3:
                                    _ClickType = 0;
                                    break;
                                case 2:
                                    _ClickType = 0;
                                    break;
                            }
                            break;
                        case 0x2115:
                            switch (m.WParam.ToInt32())
                            {
                                case 0:
                                    _ClickType = 1;
                                    break;
                                case 1:
                                    _ClickType = 2;
                                    break;
                                case 3:
                                    _ClickType = 0;
                                    break;
                                case 2:
                                    _ClickType = 0;
                                    break;
                            }
                            break;
                        default:
                            break;
                    }
                    if (Click != null && _ClickType != -1) Click(_ClickType);

                }
            }
            #endregion

            /// <summary>  
            /// 数据类  
            /// </summary>  
            [Serializable]
            public class ProjectSelect
            {
                private IList<DateTime> m_HolidayList = new List<DateTime>();
                private bool m_SundayOfWork = false;
                private bool m_SaturdayOfWork = false;
                private ProjectViewBox m_ProjectBox;
                private Color m_BackColor = Color.White;
                private Color m_WeekBackColor = Color.FromArgb(255, 212, 208, 200);
                private IList<ProjectNode> m_Nodes = new List<ProjectNode>();
                private Color m_ColumnWorkDayColor = Color.Black;
                private Color m_ColumnHolidayColor = Color.Red;


                public ProjectNode this[int p_Index]
                {
                    get
                    {
                        return m_Nodes[p_Index];
                    }
                }

                /// <summary>  
                /// 工作日背景颜色  
                /// </summary>  
                public Color BackColor { get { return m_BackColor; } set { m_BackColor = value; } }
                /// <summary>  
                /// 假期颜色  
                /// </summary>  
                public Color WeekBackColor { get { return m_WeekBackColor; } set { m_WeekBackColor = value; } }
                /// <summary>  
                /// 星期六是否工作 false为放假  
                /// </summary>  
                public bool SaturdayOfWork { get { return m_SaturdayOfWork; } set { m_SaturdayOfWork = value; } }
                /// <summary>  
                /// 星期日是否工作 false为放假  
                /// </summary>  
                public bool SundayOfWork { get { return m_SundayOfWork; } set { m_SundayOfWork = value; } }
                /// <summary>  
                /// 假期设置  
                /// </summary>  
                public IList<DateTime> HolidayList { get { return m_HolidayList; } set { m_HolidayList = value; } }
                /// <summary>  
                /// 该设置的显示控件  
                /// </summary>  
                public ProjectViewBox ProjectBox { get { return m_ProjectBox; } set { m_ProjectBox = value; } }
                /// <summary>  
                /// 数量  
                /// </summary>  
                public int Count { get { return m_Nodes.Count; } }
                /// <summary>  
                /// 假期颜色(标题)  
                /// </summary>  
                public Color ColumnHolidayColor { get { return m_ColumnHolidayColor; } set { m_ColumnHolidayColor = value; } }
                /// <summary>  
                /// 工作日颜色(标题)  
                /// </summary>  
                public Color ColumnWorkDayColor { get { return m_ColumnWorkDayColor; } set { m_ColumnWorkDayColor = value; } }


                /// <summary>  
                /// 添加  
                /// </summary>  
                /// <param name="p_NodeList">内容</param>  
                public void Add(ProjectNode p_NodeList)
                {
                    p_NodeList.ProjectSelect = this;
                    m_Nodes.Add(p_NodeList);
                    Refresh();
                }
                /// <summary>  
                /// 清除  
                /// </summary>  
                public void Clear()
                {
                    m_Nodes.Clear();
                    Refresh();
                }
                /// <summary>  
                /// 插入  
                /// </summary>  
                /// <param name="p_Index"></param>  
                /// <param name="p_NodeList"></param>  
                public void Insert(int p_Index, ProjectNode p_NodeList)
                {
                    m_Nodes.Insert(p_Index, p_NodeList);
                    Refresh();
                }
                /// <summary>  
                /// 删除  
                /// </summary>  
                /// <param name="p_Index"></param>  
                public void RemoveAt(int p_Index)
                {
                    m_Nodes.RemoveAt(p_Index);
                    Refresh();
                }
                /// <summary>  
                /// 删除  
                /// </summary>  
                /// <param name="p_NodeList"></param>  
                public void Remove(ProjectNode p_NodeList)
                {
                    m_Nodes.Remove(p_NodeList);
                    Refresh();
                }
                /// <summary>  
                /// 刷新  
                /// </summary>  
                public void Refresh()
                {
                    m_ProjectBox.Refresh();
                }


            }

            /// <summary>  
            /// 节点类  
            /// </summary>  
            [Serializable]
            public class ProjectNode
            {
                private string m_JobName = string.Empty;
                private sbyte m_Complete = -1;
                private DateTime m_StartDate = DateTime.Now;
                private uint m_WorkDay = 0;
                private HatchStyle m_Style = HatchStyle.Percent50;
                private Color m_Color = Color.Blue;
                private ProjectSelect m_ProjectSelect;

                public ProjectNode(DateTime p_StarTime, uint p_WorkDay)
                {
                    m_StartDate = p_StarTime;
                    m_WorkDay = p_WorkDay;
                }

                public ProjectNode(DateTime p_StarTime, uint p_WorkDay, string p_JobName)
                {
                    m_StartDate = p_StarTime;
                    m_WorkDay = p_WorkDay;
                    m_JobName = p_JobName;
                }

                /// <summary>  
                /// 节点设置  
                /// </summary>  
                public ProjectSelect ProjectSelect
                {
                    get { return m_ProjectSelect; }
                    set { m_ProjectSelect = value; }
                }
                /// <summary>  
                /// 显示样式  
                /// </summary>  
                public HatchStyle Style { get { return m_Style; } set { m_Style = value; Refresh(); } }
                /// <summary>  
                /// 显示颜色  
                /// </summary>  
                public Color Color { get { return m_Color; } set { m_Color = value; Refresh(); } }
                /// <summary>  
                /// 任务名称  
                /// </summary>  
                public string JobName { get { return m_JobName; } set { m_JobName = value; Refresh(); } }
                /// <summary>  
                /// 任务开始日期  
                /// </summary>  
                public DateTime StartDate { get { return m_StartDate; } set { m_StartDate = value; Refresh(); } }
                /// <summary>  
                /// 工作天数  
                /// </summary>  
                public uint WorkDay { get { return m_WorkDay; } set { m_WorkDay = value; Refresh(); } }
                /// <summary>  
                /// 资源名称  
                /// </summary>  
                public sbyte Complete { get { return m_Complete; } set { m_Complete = value; } }

                /// <summary>  
                /// 根据开始时间获取显示的区域  
                /// </summary>  
                /// <param name="p_StarDate"></param>  
                /// <param name="p_DrawSize"></param>  
                /// <param name="p_Height"></param>  
                /// <returns></returns>  
                public Rectangle GetRectangle(DateTime p_StarDate, int p_DrawSize, int p_Height)
                {
                    DateTime _TimeStar = StartDate;
                    GetWorkDay(ref _TimeStar);
                    TimeSpan _Span = _TimeStar - p_StarDate;
                    int _Day = 0;
                    if (_Span.Days >= 0)
                    {
                        _Day = (int)(_Span.TotalDays + 0.999999999999999);
                    }
                    else
                    {
                        _Day = (int)_Span.TotalDays;
                    }

                    int _Width = _Day * p_DrawSize;
                    int _WorkDay = (int)WorkDay;

                    GetWorkDayCount(_TimeStar, ref _WorkDay);
                    return new Rectangle(_Width, p_Height, _WorkDay * p_DrawSize, p_DrawSize);
                }

                public void GraphisDraw(Graphics p_Graphics, DateTime p_StarDate, int p_DrawSize, ref int p_Height, int p_MaxWidth, int p_MaxHeight)
                {
                    if (p_Height > p_MaxHeight) return;
                    Rectangle _Rectangle = GetRectangle(p_StarDate, p_DrawSize, p_Height);
                    if (_Rectangle.X > p_MaxWidth) return;
                    _Rectangle.Y += 5;
                    _Rectangle.Height -= 5;
                    HatchBrush _BlackBrush = new HatchBrush(m_Style, m_Color, Color.White);
                    p_Graphics.FillRectangle(_BlackBrush, _Rectangle);
                    p_Graphics.DrawRectangle(new Pen(Brushes.Black, 1), _Rectangle);
                    if (m_Complete >= 0)
                    {
                        StringFormat _StringFormt = new StringFormat();
                        _StringFormt.Alignment = StringAlignment.Center;
                        _StringFormt.LineAlignment = StringAlignment.Center;
                        p_Graphics.DrawString(m_Complete.ToString() + "%", new Font("宋体", 12, FontStyle.Bold), Brushes.Black, _Rectangle, _StringFormt);
                    }
                    p_Graphics.DrawString(m_JobName, new Font("宋体", 9), Brushes.Black, _Rectangle.X + _Rectangle.Width, _Rectangle.Y + 1);
                    p_Height += p_DrawSize;
                }

                /// <summary>  
                /// 刷新  
                /// </summary>  
                private void Refresh()
                {
                    if (m_ProjectSelect != null) m_ProjectSelect.Refresh();
                }

                /// <summary>  
                /// 获取工作结束日期  
                /// </summary>  
                private void GetWorkDayCount(DateTime p_StarTime, ref int p_WorkDay)
                {
                    DateTime _StarTime = p_StarTime;
                    for (int i = 0; i != p_WorkDay; i++)
                    {
                        _StarTime = p_StarTime.AddDays(i);
                        if (_StarTime.DayOfWeek == DayOfWeek.Saturday && !m_ProjectSelect.SaturdayOfWork)
                        {
                            p_WorkDay++;
                            continue;
                        }
                        if (_StarTime.DayOfWeek == DayOfWeek.Sunday && !m_ProjectSelect.SundayOfWork)
                        {
                            p_WorkDay++;
                            continue;
                        }

                        for (int z = 0; z != m_ProjectSelect.HolidayList.Count; z++)
                        {
                            bool _Year = m_ProjectSelect.HolidayList[z].Year == _StarTime.Year;
                            bool _Month = m_ProjectSelect.HolidayList[z].Month == _StarTime.Month;
                            bool _Day = m_ProjectSelect.HolidayList[z].Day == _StarTime.Day;

                            if (_Year && _Month && _Day)
                            {
                                p_WorkDay++;
                                break;
                            }
                        }
                    }
                }

                /// <summary>  
                /// 获取开始日期  
                /// </summary>  
                /// <param name="p_Value">数据</param>  
                /// <returns>日期</returns>  
                private void GetWorkDay(ref DateTime p_Value)
                {
                    bool _Set = false;
                    if (p_Value.DayOfWeek == DayOfWeek.Saturday && !m_ProjectSelect.SaturdayOfWork) p_Value = p_Value.AddDays(1);
                    if (p_Value.DayOfWeek == DayOfWeek.Sunday && !m_ProjectSelect.SundayOfWork) p_Value = p_Value.AddDays(1);

                    for (int i = 0; i != m_ProjectSelect.HolidayList.Count; i++)
                    {
                        bool _Year = m_ProjectSelect.HolidayList[i].Year == p_Value.Year;
                        bool _Month = m_ProjectSelect.HolidayList[i].Month == p_Value.Month;
                        bool _Day = m_ProjectSelect.HolidayList[i].Day == p_Value.Day;

                        if (_Year && _Month && _Day)
                        {
                            p_Value = p_Value.AddDays(1);
                            _Set = true;
                            break;
                        }
                    }
                    if (_Set) GetWorkDay(ref p_Value);
                }


            }
        }

    }

     

    前端:

     

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;

    namespace GanttControls
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }

            private void button1_Click(object sender, EventArgs e)
            {
                GanttControls.ProjectViewBox.ProjectNode p1 =new ProjectViewBox.ProjectNode(DateTime.Parse("2012-04-12"), 2, "新任务1");
                GanttControls.ProjectViewBox.ProjectNode p2 = new ProjectViewBox.ProjectNode(DateTime.Parse("2012-04-13"), 3, "新任务2");
                GanttControls.ProjectViewBox.ProjectNode p3 = new ProjectViewBox.ProjectNode(DateTime.Parse("2012-04-13"), 2, "新任务3");
                GanttControls.ProjectViewBox.ProjectNode p4 = new ProjectViewBox.ProjectNode(DateTime.Parse("2012-04-14"), 5, "新任务2");
                GanttControls.ProjectViewBox.ProjectNode p5 = new ProjectViewBox.ProjectNode(DateTime.Parse("2012-04-15"), 8, "新任务3");

                p1.Complete = 10;
                p2.Complete = 80;
                p3.Complete = 60;


                projectViewBox1.ProjectSelectData.HolidayList.Add(DateTime.Parse("2012-04-01"));
                projectViewBox1.ProjectSelectData.HolidayList.Add(DateTime.Parse("2012-04-12"));
                projectViewBox1.ProjectSelectData.HolidayList.Add(DateTime.Parse("2012-04-14"));
                projectViewBox1.ProjectSelectData.HolidayList.Add(DateTime.Parse("2012-04-15"));
                projectViewBox1.ProjectSelectData.HolidayList.Add(DateTime.Parse("2012-04-16"));
                projectViewBox1.ProjectSelectData.HolidayList.Add(DateTime.Parse("2012-04-17"));

                projectViewBox1.ProjectSelectData.Add(p1);
                projectViewBox1.ProjectSelectData.Add(p2);
                projectViewBox1.ProjectSelectData.Add(p3);
                projectViewBox1.ProjectSelectData.Add(p4);
                projectViewBox1.ProjectSelectData.Add(p5);

               
            }
        }
    }

     

     

     

    转载于:https://www.cnblogs.com/Mayvar/archive/2012/04/12/wanghonghua_201204120938.html

    展开全文
  • C#写的画甘特图的工具
  • C# 实现简单的甘特图控件
  • 甘特图控件

    2012-05-24 18:48:22
    一个C#甘特图控件,不大,也不复杂,可以做简单的项目进度显示
  • 甘特图C#实现

    2015-05-06 20:24:00
    1.新建用户控件;解决方案=》项目右键单击=》新建用户控件,把下面的代码拷贝进去 ... /// 甘特图控件 /// </summary> public class ProjectViewBox : DevExpress.XtraEditors.XtraUserControl { ...

    1.新建用户控件;解决方案=》项目右键单击=》新建用户控件,把下面的代码拷贝进去

    /// <summary>  
        /// 甘特图控件  
        /// </summary>  
        public class ProjectViewBox : DevExpress.XtraEditors.XtraUserControl
        {
            #region 容器
            #region 设计器代码
            /// <summary>   
            /// 必需的设计器变量。  
            /// </summary>  
            private System.ComponentModel.IContainer components = null;
    
            /// <summary>   
            /// 清理所有正在使用的资源。  
            /// </summary>  
            /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>  
            protected override void Dispose(bool disposing)
            {
                if (disposing && (components != null))
                {
                    components.Dispose();
                }
                base.Dispose(disposing);
            }
    
            #region 组件设计器生成的代码
    
            /// <summary>   
            /// 设计器支持所需的方法 - 不要  
            /// 使用代码编辑器修改此方法的内容。  
            /// </summary>  
            private void InitializeComponent()
            {
                this.hScrollBar1 = new System.Windows.Forms.HScrollBar();
                this.vScrollBar1 = new System.Windows.Forms.VScrollBar();
                this.MainPictureBox = new System.Windows.Forms.PictureBox();
                ((System.ComponentModel.ISupportInitialize)(this.MainPictureBox)).BeginInit();
                this.SuspendLayout();
                // 
                // hScrollBar1
                // 
                this.hScrollBar1.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)
                | System.Windows.Forms.AnchorStyles.Right)));
                this.hScrollBar1.LargeChange = 1;
                this.hScrollBar1.Location = new System.Drawing.Point(1, 111);
                this.hScrollBar1.Maximum = 60;
                this.hScrollBar1.Name = "hScrollBar1";
                this.hScrollBar1.Size = new System.Drawing.Size(120, 16);
                this.hScrollBar1.TabIndex = 0;
                // 
                // vScrollBar1
                // 
                this.vScrollBar1.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
                | System.Windows.Forms.AnchorStyles.Right)));
                this.vScrollBar1.LargeChange = 1;
                this.vScrollBar1.Location = new System.Drawing.Point(121, 0);
                this.vScrollBar1.Maximum = 60;
                this.vScrollBar1.Name = "vScrollBar1";
                this.vScrollBar1.Size = new System.Drawing.Size(16, 111);
                this.vScrollBar1.TabIndex = 1;
                // 
                // MainPictureBox
                // 
                this.MainPictureBox.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
                | System.Windows.Forms.AnchorStyles.Left)
                | System.Windows.Forms.AnchorStyles.Right)));
                this.MainPictureBox.Location = new System.Drawing.Point(0, 0);
                this.MainPictureBox.Name = "MainPictureBox";
                this.MainPictureBox.Size = new System.Drawing.Size(316, 328);
                this.MainPictureBox.TabIndex = 2;
                this.MainPictureBox.TabStop = false;
                // 
                // ProjectViewBox
                // 
                this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
                this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
                this.BackColor = System.Drawing.SystemColors.Control;
                this.Controls.Add(this.vScrollBar1);
                this.Controls.Add(this.hScrollBar1);
                this.Controls.Add(this.MainPictureBox);
                this.Name = "ProjectViewBox";
                this.Size = new System.Drawing.Size(139, 129);
                this.Load += new System.EventHandler(this.ProjectBox_Load);
                this.SizeChanged += new System.EventHandler(this.ProjectBox_SizeChanged);
                ((System.ComponentModel.ISupportInitialize)(this.MainPictureBox)).EndInit();
                this.ResumeLayout(false);
    
            }
    
            #endregion
    
            private System.Windows.Forms.HScrollBar hScrollBar1;
            private System.Windows.Forms.VScrollBar vScrollBar1;
            private System.Windows.Forms.PictureBox MainPictureBox;
            #endregion
    
            public ProjectViewBox()
            {
                InitializeComponent();
            }
    
            #region 私有成员
            /// <summary>  
            /// 最小单元格  
            /// </summary>  
            private int m_Size = 20;
            private int mh_Size = 20;
            /// <summary>  
            /// 日期索引 记录当前绘制的位置  
            /// </summary>  
            private int m_DayIndex = 0;//提起索引的添加,重点
            /// <summary>  
            /// 开始绘制位置  
            /// </summary>  
            private int m_JobIndex = 0;
            #endregion
    
            /// <summary>  
            /// 大小改变  
            /// </summary>  
            /// <param name="sender"></param>  
            /// <param name="e"></param>  
            private void ProjectBox_SizeChanged(object sender, EventArgs e)
            {
                if (MainPictureBox.Image != null) PaintProjectImage();
            }
    
            private MyScroll Hscroll;
            private MyScroll Vscroll;
    
            private void ProjectBox_Load(object sender, EventArgs e)
            {
                MainPictureBox.Image = new Bitmap(1, 1);
                PaintProjectImage();
                Hscroll = new MyScroll();
                Hscroll.Click += new MyScroll.ScrollClick(Hscroll_Click);
                Hscroll.AssignHandle(hScrollBar1.Handle);
    
                Vscroll = new MyScroll();
                Vscroll.Click += new MyScroll.ScrollClick(Vscroll_Click);
                Vscroll.AssignHandle(vScrollBar1.Handle);
    
                m_ProjectSelect.ProjectBox = this;
            }
    
            #region 滚动条
            void Vscroll_Click(int p_Value)
            {
                switch (p_Value)
                {
                    case 0:
                        m_JobIndex = vScrollBar1.Value;
                        break;
                    case 1:
                        m_JobIndex--;
                        if (m_JobIndex < 0) m_JobIndex = 0;
                        break;
                    case 2:
                        m_JobIndex++;
                        break;
    
                }
                if (m_JobIndex <= 0) vScrollBar1.Value = 0;
                if (m_JobIndex >= 60) vScrollBar1.Value = 60;
                PaintProjectImage();
            }
    
            void Hscroll_Click(int p_Value)
            {
                switch (p_Value)
                {
                    case 0:
                        m_DayIndex = hScrollBar1.Value;
                        break;
                    case 1:
                        m_DayIndex--;
                        break;
                    case 2:
                        m_DayIndex++;
                        break;
    
                }
                if (m_DayIndex <= 0) hScrollBar1.Value = 0;
                if (m_DayIndex >= 60) hScrollBar1.Value = 60;
                PaintProjectImage();
            }
            #endregion
    
            /// <summary>  
            /// 刷新  
            /// </summary>  
            public override void Refresh()
            {
                PaintProjectImage();
            }
    
            /// <summary>  
            /// 开始绘制  
            /// </summary>  
            private void PaintProjectImage()
            {
                MainPictureBox.Image.Dispose();
                if (MainPictureBox.Width == 0 || MainPictureBox.Height == 0) return;
                Bitmap _NewBitmap = new Bitmap(MainPictureBox.Width, MainPictureBox.Height);
                Graphics _Graphics = Graphics.FromImage(_NewBitmap);
                Point _DrawPoint = new Point(0, 0);
                DateTime _DrawDate = DateTime.Now.AddDays(m_DayIndex);//日期表头的定义
                while (true)
                {
                    _DrawPoint = GraphicsWeekHead(_Graphics, ref _DrawDate, _DrawPoint);
    
                    if (_DrawPoint.X > MainPictureBox.Width) break;
                }
    
                DateTime _NodeStarDate = DateTime.Now.AddDays(m_DayIndex);
                int _Hight = m_Size * 2;
                for (int i = m_JobIndex; i < m_ProjectSelect.Count; i++)
                {
                    m_ProjectSelect[i].GraphisDraw(_Graphics, _NodeStarDate, m_Size, ref _Hight, MainPictureBox.Width, MainPictureBox.Height);
                }
    
                MainPictureBox.Image = _NewBitmap;
            }
    
            /// <summary>  
            /// 绘制日期头和星期几  
            /// </summary>  
            /// <param name="p_Graphics">刷子</param>  
            /// <param name="p_StarDate">开始日期 ref类型 返回日期的下周一</param>  
            /// <param name="p_StarPoint">开始位置</param>  
            /// <returns>结束节点</returns>  
            private Point GraphicsWeekHead(Graphics p_Graphics, ref DateTime p_StarDate, Point p_StarPoint)
            {
                int _Date = (int)p_StarDate.DayOfWeek;
                if (_Date != 0) p_StarDate = p_StarDate.AddDays(-_Date);
                int _DrawDateX = p_StarPoint.X - (_Date * mh_Size);
                Pen _LinePen = new Pen(Brushes.Black, 1);
                Rectangle _RectangleHead = new Rectangle(_DrawDateX, 0, mh_Size * 7, mh_Size);
                StringFormat _StringFormat = new StringFormat();
                _StringFormat.LineAlignment = StringAlignment.Far;
    
                p_Graphics.DrawRectangle(_LinePen, _RectangleHead);
                p_Graphics.DrawString(p_StarDate.ToString("yyyy年MM月dd日"), this.Font, Brushes.Black, _RectangleHead, _StringFormat);
    
                _RectangleHead = new Rectangle(p_StarPoint.X, mh_Size, p_StarPoint.X + mh_Size, mh_Size);
                Rectangle _BlackRect = new Rectangle(0, mh_Size * 2, mh_Size, this.Height - (mh_Size * 2));
    
                Color _BlackColor = m_ProjectSelect.BackColor;
    
                for (int i = _Date; i != 7; i++)
                {
                    string _WeekText = string.Empty;
                    switch (i)
                    {
                        case 0:
                            _WeekText = "日";
                            _BlackColor = m_ProjectSelect.WeekBackColor;
                            break;
                        case 1:
                            _WeekText = "一";
                            _BlackColor = m_ProjectSelect.BackColor;
                            break;
                        case 2:
                            _WeekText = "二";
                            _BlackColor = m_ProjectSelect.BackColor;
                            break;
                        case 3:
                            _WeekText = "三";
                            _BlackColor = m_ProjectSelect.BackColor;
                            break;
                        case 4:
                            _WeekText = "四";
                            _BlackColor = m_ProjectSelect.BackColor;
                            break;
                        case 5:
                            _WeekText = "五";
                            _BlackColor = m_ProjectSelect.BackColor;
                            break;
                        case 6:
                            _WeekText = "六";
                            _BlackColor = m_ProjectSelect.WeekBackColor;
                            break;
                    }
                    HatchBrush _BlackBrush = new HatchBrush(HatchStyle.Percent50, _BlackColor, Color.White);
                    _BlackRect.X = _RectangleHead.X;
                    p_Graphics.FillRectangle(_BlackBrush, _BlackRect);
    
                    p_Graphics.DrawRectangle(_LinePen, _RectangleHead);
                    p_Graphics.DrawString(_WeekText, new Font("宋体", 8.0f), GetWorkColumneColor(p_StarDate, i), _RectangleHead, _StringFormat);
                    _RectangleHead.X += mh_Size;
                }
                p_StarDate = p_StarDate.AddDays(7);
                return new Point(_RectangleHead.X, mh_Size * 2);
            }
    
            /// <summary>  
            /// 获取日期的标题色  
            /// </summary>  
            /// <param name="p_Date">日期</param>  
            /// <param name="p_Day">1-7</param>  
            /// <returns></returns>  
            public SolidBrush GetWorkColumneColor(DateTime p_Date, int p_Day)
            {
                DateTime _ValueDate = p_Date.AddDays(p_Day);
    
                if (_ValueDate.DayOfWeek == DayOfWeek.Saturday && !m_ProjectSelect.SaturdayOfWork)
                {
                    return new SolidBrush(m_ProjectSelect.ColumnHolidayColor);
                }
                if (_ValueDate.DayOfWeek == DayOfWeek.Sunday && !m_ProjectSelect.SundayOfWork)
                {
                    return new SolidBrush(m_ProjectSelect.ColumnHolidayColor);
                }
    
                for (int z = 0; z != m_ProjectSelect.HolidayList.Count; z++)
                {
                    bool _Year = m_ProjectSelect.HolidayList[z].Year == _ValueDate.Year;
                    bool _Month = m_ProjectSelect.HolidayList[z].Month == _ValueDate.Month;
                    bool _Day = m_ProjectSelect.HolidayList[z].Day == _ValueDate.Day;
    
                    if (_Year && _Month && _Day)
                    {
                        return new SolidBrush(m_ProjectSelect.ColumnHolidayColor);
                    }
                }
    
                return new SolidBrush(m_ProjectSelect.ColumnWorkDayColor);
            }
    
            #region 公用属性
            private ProjectSelect m_ProjectSelect = new ProjectSelect();
            /// <summary>  
            /// 基本设置  
            /// </summary>       
            [Browsable(false)]
            [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
            public ProjectSelect ProjectSelectData
            {
                get { return m_ProjectSelect; }
                set { m_ProjectSelect = value; m_ProjectSelect.ProjectBox = this; }
            }
            #endregion
    
            /// <summary>  
            /// 实在写不好Scroll 判断消息
            /// </summary>  
            private class MyScroll : NativeWindow
            {
                public delegate void ScrollClick(int p_Value);
                public event ScrollClick Click;
    
                protected override void WndProc(ref Message m)
                {
                    base.WndProc(ref m);
                    int _ClickType = -1;
                    switch (m.Msg)
                    {
                        case 0xEa:
                            _ClickType = 0;
                            break;
                        case 0x2114:
                            switch (m.WParam.ToInt32())
                            {
                                case 0:
                                    _ClickType = 1;
                                    break;
                                case 1:
                                    _ClickType = 2;
                                    break;
                                case 3:
                                    _ClickType = 0;
                                    break;
                                case 2:
                                    _ClickType = 0;
                                    break;
                            }
                            break;
                        case 0x2115:
                            switch (m.WParam.ToInt32())
                            {
                                case 0:
                                    _ClickType = 1;
                                    break;
                                case 1:
                                    _ClickType = 2;
                                    break;
                                case 3:
                                    _ClickType = 0;
                                    break;
                                case 2:
                                    _ClickType = 0;
                                    break;
                            }
                            break;
                        default:
                            break;
                    }
                    if (Click != null && _ClickType != -1) Click(_ClickType);
    
                }
            }
            #endregion
    
            /// <summary>  
            /// 数据类  
            /// </summary>  
            [Serializable]
            public class ProjectSelect
            {
                private IList<DateTime> m_HolidayList = new List<DateTime>();
                private bool m_SundayOfWork = true;
                private bool m_SaturdayOfWork = true;
                private ProjectViewBox m_ProjectBox;
                private Color m_BackColor = Color.White;
                private Color m_WeekBackColor = Color.FromArgb(200, 200, 200, 200);
                private IList<ProjectNode> m_Nodes = new List<ProjectNode>();
                private Color m_ColumnWorkDayColor = Color.Black;
                private Color m_ColumnHolidayColor = Color.Red;
    
    
                public ProjectNode this[int p_Index]
                {
                    get
                    {
                        return m_Nodes[p_Index];
                    }
                }
    
                /// <summary>  
                /// 工作日背景颜色  
                /// </summary>  
                public Color BackColor { get { return m_BackColor; } set { m_BackColor = value; } }
                /// <summary>  
                /// 假期颜色  
                /// </summary>  
                public Color WeekBackColor { get { return m_WeekBackColor; } set { m_WeekBackColor = value; } }
                /// <summary>  
                /// 星期六是否工作 false为放假  
                /// </summary>  
                public bool SaturdayOfWork { get { return m_SaturdayOfWork; } set { m_SaturdayOfWork = value; } }
                /// <summary>  
                /// 星期日是否工作 false为放假  
                /// </summary>  
                public bool SundayOfWork { get { return m_SundayOfWork; } set { m_SundayOfWork = value; } }
                /// <summary>  
                /// 假期设置  
                /// </summary>  
                public IList<DateTime> HolidayList { get { return m_HolidayList; } set { m_HolidayList = value; } }
                /// <summary>  
                /// 该设置的显示控件  
                /// </summary>  
                public ProjectViewBox ProjectBox { get { return m_ProjectBox; } set { m_ProjectBox = value; } }
                /// <summary>  
                /// 数量  
                /// </summary>  
                public int Count { get { return m_Nodes.Count; } }
                /// <summary>  
                /// 假期颜色(标题)  
                /// </summary>  
                public Color ColumnHolidayColor { get { return m_ColumnHolidayColor; } set { m_ColumnHolidayColor = value; } }
                /// <summary>  
                /// 工作日颜色(标题)  
                /// </summary>  
                public Color ColumnWorkDayColor { get { return m_ColumnWorkDayColor; } set { m_ColumnWorkDayColor = value; } }
    
    
                /// <summary>  
                /// 添加  
                /// </summary>  
                /// <param name="p_NodeList">内容</param>  
                public void Add(ProjectNode p_NodeList)
                {
                    p_NodeList.ProjectSelect = this;
                    m_Nodes.Add(p_NodeList);
                    Refresh();
                }
                /// <summary>  
                /// 清除  
                /// </summary>  
                public void Clear()
                {
                    m_Nodes.Clear();
                    Refresh();
                }
                /// <summary>  
                /// 插入  
                /// </summary>  
                /// <param name="p_Index"></param>  
                /// <param name="p_NodeList"></param>  
                public void Insert(int p_Index, ProjectNode p_NodeList)
                {
                    m_Nodes.Insert(p_Index, p_NodeList);
                    Refresh();
                }
                /// <summary>  
                /// 删除  
                /// </summary>  
                /// <param name="p_Index"></param>  
                public void RemoveAt(int p_Index)
                {
                    m_Nodes.RemoveAt(p_Index);
                    Refresh();
                }
                /// <summary>  
                /// 删除  
                /// </summary>  
                /// <param name="p_NodeList"></param>  
                public void Remove(ProjectNode p_NodeList)
                {
                    m_Nodes.Remove(p_NodeList);
                    Refresh();
                }
                /// <summary>  
                /// 刷新  
                /// </summary>  
                public void Refresh()
                {
                    m_ProjectBox.Refresh();
                }
    
    
            }
    
            /// <summary>  
            /// 节点类  
            /// </summary>  
            [Serializable]
            public class ProjectNode //注意Nodes
            {
                private string m_JobName = string.Empty;
                private sbyte m_Complete = -1;
                private DateTime m_StartDate = DateTime.Now;
                private uint m_WorkDay = 0;
                private HatchStyle m_Style = HatchStyle.Percent50;
                private Color m_Color = Color.Blue;
                private ProjectSelect m_ProjectSelect;
    
                public ProjectNode(DateTime p_StarTime, uint p_WorkDay)
                {
                    m_StartDate = p_StarTime;
                    m_WorkDay = p_WorkDay;
                }
    
                public ProjectNode(DateTime p_StarTime, uint p_WorkDay, string p_JobName)
                {
                    m_StartDate = p_StarTime;
                    m_WorkDay = p_WorkDay;
                    m_JobName = p_JobName;
                }
    
                /// <summary>  
                /// 节点设置  
                /// </summary>  
                public ProjectSelect ProjectSelect
                {
                    get { return m_ProjectSelect; }
                    set { m_ProjectSelect = value; }
                }
                /// <summary>  
                /// 显示样式  
                /// </summary>  
                public HatchStyle Style { get { return m_Style; } set { m_Style = value; Refresh(); } }
                /// <summary>  
                /// 显示颜色  
                /// </summary>  
                public Color Color { get { return m_Color; } set { m_Color = value; Refresh(); } }
                /// <summary>  
                /// 任务名称  
                /// </summary>  
                public string JobName { get { return m_JobName; } set { m_JobName = value; Refresh(); } }
                /// <summary>  
                /// 任务开始日期  
                /// </summary>  
                public DateTime StartDate { get { return m_StartDate; } set { m_StartDate = value; Refresh(); } }
                /// <summary>  
                /// 工作天数  
                /// </summary>  
                public uint WorkDay { get { return m_WorkDay; } set { m_WorkDay = value; Refresh(); } }
                /// <summary>  
                /// 资源名称/工程进度  
                /// </summary>  
                public sbyte Complete { get { return m_Complete; } set { m_Complete = value; } }
    
                /// <summary>  
                /// 根据开始时间获取显示的区域  
                /// </summary>  
                /// <param name="p_StarDate"></param>  
                /// <param name="p_DrawSize"></param>  
                /// <param name="p_Height"></param>  
                /// <returns></returns>  
                public Rectangle GetRectangle(DateTime p_StarDate, int p_DrawSize, int p_Height)
                {
                    DateTime _TimeStar = StartDate;
                    GetWorkDay(ref _TimeStar);
                    TimeSpan _Span = _TimeStar - p_StarDate;
                    int _Day = 0;
                    if (_Span.Days >= 0)
                    {
                        _Day = (int)(_Span.TotalDays + 0.999999999999999);
                    }
                    else
                    {
                        _Day = (int)_Span.TotalDays;
                    }
    
                    int _Width = _Day * p_DrawSize;
                    int _WorkDay = (int)WorkDay;
    
                    GetWorkDayCount(_TimeStar, ref _WorkDay);
                    return new Rectangle(_Width, p_Height, _WorkDay * p_DrawSize, p_DrawSize);
                }
    
                /*/
                public Rectangle GetRectangle1(DateTime p_StarDate, int p_DrawSize, int p_Height)
                {
                    DateTime _TimeStar = StartDate;
                    GetWorkDay(ref _TimeStar);
                    TimeSpan _Span = _TimeStar - p_StarDate;
                    int _Day = 0;
                    if (_Span.Days >= 0)
                    {
                        _Day = (int)(_Span.TotalDays + 0.999999999999999);
                    }
                    else
                    {
                        _Day = (int)_Span.TotalDays;
                    }
    
                    int _Width = _Day * p_DrawSize;
                    int _WorkDay = (int)WorkDay;
    
                    GetWorkDayCount(_TimeStar, ref _WorkDay);
                    return new Rectangle(_Width, p_Height, _WorkDay * p_DrawSize, p_DrawSize);
                }
                /*/
                public void GraphisDraw(Graphics p_Graphics, DateTime p_StarDate, int p_DrawSize, ref int p_Height, int p_MaxWidth, int p_MaxHeight)
                {
                    if (p_Height > p_MaxHeight) return;
                    Rectangle _Rectangle = GetRectangle(p_StarDate, p_DrawSize, p_Height);
                    if (_Rectangle.X > p_MaxWidth) return;
                    _Rectangle.Y += 4;
                    _Rectangle.Height -= 8;
                    HatchBrush _BlackBrush = new HatchBrush(m_Style, m_Color, Color.White);
                    p_Graphics.FillRectangle(_BlackBrush, _Rectangle);
                    p_Graphics.DrawRectangle(new Pen(Brushes.Black, 1), _Rectangle);
                    if (m_Complete >= 0)
                    {
                        StringFormat _StringFormt = new StringFormat();
                        _StringFormt.Alignment = StringAlignment.Center;
                        _StringFormt.LineAlignment = StringAlignment.Center;
                        p_Graphics.DrawString(m_Complete.ToString() + "%", new Font("微软雅黑", 12, FontStyle.Bold), Brushes.Black, _Rectangle, _StringFormt);
                    }
                    p_Graphics.DrawString(m_JobName, new Font("微软雅黑", 9), Brushes.Black, _Rectangle.X + _Rectangle.Width, _Rectangle.Y + 1);
                    p_Height += p_DrawSize;
                }
    
                /// <summary>  
                /// 刷新  
                /// </summary>  
                private void Refresh()
                {
                    if (m_ProjectSelect != null) m_ProjectSelect.Refresh();
                }
    
                /// <summary>  
                /// 获取工作结束日期  
                /// </summary>  
                private void GetWorkDayCount(DateTime p_StarTime, ref int p_WorkDay)
                {
                    DateTime _StarTime = p_StarTime;
                    for (int i = 0; i != p_WorkDay; i++)
                    {
                        _StarTime = p_StarTime.AddDays(i);
                        /*if (_StarTime.DayOfWeek == DayOfWeek.Saturday && !m_ProjectSelect.SaturdayOfWork)
                        {
                            p_WorkDay++;
                            continue;
                        }
                        if (_StarTime.DayOfWeek == DayOfWeek.Sunday && !m_ProjectSelect.SundayOfWork)
                        {
                            p_WorkDay++;
                            continue;
                        }*/
    
                        for (int z = 0; z != m_ProjectSelect.HolidayList.Count; z++)
                        {
                            bool _Year = m_ProjectSelect.HolidayList[z].Year == _StarTime.Year;
                            bool _Month = m_ProjectSelect.HolidayList[z].Month == _StarTime.Month;
                            bool _Day = m_ProjectSelect.HolidayList[z].Day == _StarTime.Day;
    
                            if (_Year && _Month && _Day)
                            {
                                p_WorkDay++;
                                break;
                            }
                        }
                    }
                }
    
                /// <summary>  
                /// 获取开始日期  
                /// </summary>  
                /// <param name="p_Value">数据</param>  
                /// <returns>日期</returns>  
                private void GetWorkDay(ref DateTime p_Value)
                {
                    bool _Set = false;
                    //if (p_Value.DayOfWeek == DayOfWeek.Saturday && !m_ProjectSelect.SaturdayOfWork) p_Value = p_Value.AddDays(1);
                    //if (p_Value.DayOfWeek == DayOfWeek.Sunday && !m_ProjectSelect.SundayOfWork) p_Value = p_Value.AddDays(1);
    
                    for (int i = 0; i != m_ProjectSelect.HolidayList.Count; i++)
                    {
                        bool _Year = m_ProjectSelect.HolidayList[i].Year == p_Value.Year;
                        bool _Month = m_ProjectSelect.HolidayList[i].Month == p_Value.Month;
                        bool _Day = m_ProjectSelect.HolidayList[i].Day == p_Value.Day;
    
                        if (_Year && _Month && _Day)
                        {
                            p_Value = p_Value.AddDays(1);
                            _Set = true;
                            break;
                        }
                    }
                    if (_Set) GetWorkDay(ref p_Value);
                }
            }
        }  
    

     2.在左边工具栏中把新建好的控件拖拽进去Form,然后添加代码显示,下面是样例代码:

    #region 加载横道图
                AutoScroll = true;
                /*
                int[] day = new int[dataGridView1.Rows.Count];
                string[] fromtime = new string[dataGridView1.Rows.Count];
                string[] _Node = new string[dataGridView1.Rows.Count];
                */
                //test
                int DT = new int();
                DT = dtTree.Rows.Count;
    
                string[,] data = new string[DT, 4];
                DateTime st = new DateTime();
                DateTime ft = new DateTime();
                TimeSpan ts = new TimeSpan();
                for (int i = 0; i < DT; i++)
                {
                    data[i, 0] = dtTree.Rows[i][1].ToString();
                    data[i, 1] = dtTree.Rows[i][3].ToString();
                    data[i, 2] = dtTree.Rows[i][4].ToString();
                    DateTime.TryParse(data[i, 1], out st);
                    DateTime.TryParse(data[i, 2], out ft);
                    ts = ft - st;
                    data[i, 3] = ts.Days.ToString();
    
                    string startDay = data[i, 1];
                    DateTimeFormatInfo dtFormat = new DateTimeFormatInfo();
                    dtFormat.ShortDatePattern = "yyyy/MM/dd";
                    DateTime dt = Convert.ToDateTime(startDay, dtFormat);
                    string nameStr = data[i, 0];
                    int days = Convert.ToInt32(data[i, 3]);
                    ProjectViewBox.ProjectNode _Nodei = new ProjectViewBox.ProjectNode(dt, (uint)days, nameStr);
                    projectViewBox1.ProjectSelectData.Add(_Nodei);
                }
                #endregion
    

      个人测试成功,有问题可以留言或者联系邮箱ruanyangzhi@126.com

     

    转载于:https://www.cnblogs.com/chinatrust/p/4483056.html

    展开全文
  • 甘特图控件C#源码

    2014-07-11 10:20:51
    The .NET C# Winforms Gantt Chart Control is a class library project hosted on codeplex that provides a quick way to get a gantt chart up and running for your application. This control was first ...
  • 本资源为C#源码,可以自己改成需要的样式!
  • 开源甘特图Gantt_Chart(C#版本)

    热门讨论 2014-08-09 22:59:41
    开源甘特图Gantt_Chart,原来是VB版本的,我已经改为C#版本了
  • QQ:1916531335-VARCHART XGantt.NET--.NET / C#甘特图控件,可靠,强大且经过验证的.NET / C#甘特图控件。-全球有500多个客户使用。 VARCHART XGantt.NET世界级甘特图大师的开发控件,VARCHART XGantt是全球最大...

    QQ:1916531335-VARCHART XGantt.NET--.NET / C#甘特图控件,可靠,强大且经过验证的.NET / C#甘特图控件。-全球有500多个客户使用。

    VARCHART XGantt.NET 世界级甘特图大师的开发控件,VARCHART XGantt是全球最大、功能最强的甘特图控件,它能够以甘特图、柱状图的形式来编辑、打印以及图形化的表示数据,极短时间内实现甘特图效果,其强大的功能可与Microsoft的Project系列产品相媲美。

    VARCHART XGantt.NET 是一个支持ActiveX、.Net等平台的C#甘特图控件。通过交互式甘特图为您的应用提供出色的可视化计划排程体验。

    VARCHART XGantt.NET功能特点

    1、智能甘特图-加快规划与控制

    时间相关的数据如任务,订单,活动以及资源的最佳显示方式是甘特图。在一个智能的甘特图中,规划人员可以在计划过程中采取正确的措施,在放大到细节的同时也不会错过对整体的把控。QQ:1916531335-VARCHART XGantt.NET--.NET 软件实现了对简单到棘手和复杂任务的规划和控制。

    2、可视化规划与调度

    甘特图能实现机器利用率,订单进度,聘用员工的可用性的可视化,以及更多计划,控制和优化生产。甘特图能与规划人员进行交互,是的规划人员可以根据实际情况迅速地修改计划。因此,你可以全面利用你的资源进而加快整个进程。QQ:1916531335-VARCHART XGantt.NET--.NET 软件提供了强大的功能,可以开发生产规划计划和控制的任何类型的甘特图。

    3、精确的产能规划

    无论生产中的机器部署或项目中的员工是否当事 - 精确产能规划只关心应当结算工作人员,机器,设备和质量。甘特图显示这些资源的一个重要工具是直方图,可以让你对目前的进程一目了然。甘特图还可用来显示实际产能或未来产能以及利用率概览。QQ:1916531335-VARCHART XGantt.NET--.NET

    4、高效的项目管理

    作为大型项目经理,你必须要达到项目的里程碑,牢记项目任务,记住子项目的链接,最重要的是你需要短期和长期监视该项目。有了QQ:1916531335-VARCHART XGantt.NET--.NET ,你可以开发单独的项目计划,帮助你控制所有重要的项目参数,并安全准时完成项目。

    5、排班和人力资源规划

    员工信息及其部署可通过甘特图显示。QQ:1916531335-VARCHART XGantt.NET--.NET 可使你通过日历功能预先设置的进度条的长度,并添加计划相关的信息,例如夜班或早班,每班必需的员工数量等。在甘特图中的排班和人力资源规划表几乎没有任何可视化限制。

    6、缩短开发时间

    VARCHART XGantt.NET 甘特图软件帮助你大大节省时间和精力,让你的应用程序更快地推向市场。QQ:1916531335-VARCHART XGantt.NET--.NET 提供:一个全面的应用程序编程接口(API)来根据你的需求完全自定义你的应用程序,一个来轻松地将数据连接到你的甘特图图形元素的关系型数据模型卓越的可视化和数据操纵功能,一系列带有源码的有用的甘特图应用程序示例。

    7、广泛的技术支持

    VARCHART XGantt.NET 甘特图软件拥有以下三个技术和编程语言的版本:NET,ASP.NET和ActiveX。

    您是否需要在应用程序中可视化不同的轮班日历和/或资源日历?使用QQ:1916531335-VARCHART XGantt.NET--.NET ,您可以管理和可视化每个资源的单个工作日历,并在甘特图中显示工作时间和非工作时间。除此之外,您还可以在直方图中显示资源容量及其各自的利用率,并清楚地强调它们。

    展开全文
  • C# 实现简单的甘特图控件 .

    千次阅读 2011-07-26 17:43:34
    C# 实现简单的甘特图控件 分类: 控件 .NET 2009-08-22 15:01 1175人阅读 评论(3) 收藏 举报 效果图 日期红色的可以设置假期 也可以设置星期六星期日是否为假期 例子 ProjectViewBox.ProjectNode _Node1 = n
  • 基于JS简单甘特图源码,支持JAVA c#。基于JS简单甘特图源码,支持JAVA c#。基于JS简单甘特图源码,支持JAVA c#
  • VARCHART XGantt是一个支持ActiveX、.Net等平台的C#甘特图控件。通过交互式甘特图为您的应用提供出色的可视化计划排程体验。工作、任务、订单、活动以及资源在甘特图中更清晰、灵活的展示,并且动一动手指就完成计划...
  • 甘特图-.net开发

    2010-08-18 08:47:01
    甘特图C#,.net,开发,JS 甘特图开发,用JS实现甘特图的效果,非常好用。
  • var dtMin = Convert.ToDateTime(dt.Compute("min(Start)", "1=1")); var dtMax = Convert.ToDateTime(dt.Compute("max(Finish)", "1=1")); int days = (dtMax - dtMi...
  • 从零开始编写自己的C#框架(10)——项目实施计划与甘特图 原文:从零开始编写自己的C#框架(10)——项目实施计划与甘特图 不知不觉本系列已经写了一个月,编码前的各项工作到此也终于结束了。回头看...
  • 可满足项目管理应用程序的所有需求,是最完善的甘特图图表库。它允许你创建动态甘特图,并以一个方便的图形化方式可视化项目进度。有了dhtmlxGantt,你可以显示活动之间的依赖关系,显示具有完成百分比阴影的当前...
  • 我要实现网页上的 甘特图,需要c#.net实现的!vs2005 甘特图控件 也行 http://zhidao.baidu.com/question/93633804.html 急问:开发甘特图(Gantt Chart)的组件? 2011-8-22 04:24 提问者:匿名 | 浏览次数...

空空如也

空空如也

1 2 3 4 5 ... 12
收藏数 237
精华内容 94
关键字:

c#甘特图

c# 订阅