码迷,mamicode.com
首页 > Windows程序 > 详细

甘特图C#实现

时间:2015-05-06 21:14:16      阅读:184      评论:0      收藏:0      [点我收藏+]

标签:

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

 

甘特图C#实现

标签:

原文地址:http://www.cnblogs.com/chinatrust/p/4483056.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!