C#制作甘特图

这篇博客介绍如何使用C#制作一个甘特图控件,详细展示了控件的代码实现,包括滚动条处理、绘图逻辑等。通过自定义控件,实现了甘特图的显示和刷新功能,并提供了添加任务、处理节假日和周末的工作天数计算等功能。
摘要由CSDN通过智能技术生成

     先自制甘特图控件:

 

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 滚动条

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值