惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

T
The Blog of Author Tim Ferriss
S
Securelist
D
Docker
The Register - Security
The Register - Security
GbyAI
GbyAI
Recorded Future
Recorded Future
Engineering at Meta
Engineering at Meta
Stack Overflow Blog
Stack Overflow Blog
云风的 BLOG
云风的 BLOG
P
Proofpoint News Feed
罗磊的独立博客
博客园 - 【当耐特】
F
Full Disclosure
WordPress大学
WordPress大学
腾讯CDC
小众软件
小众软件
大猫的无限游戏
大猫的无限游戏
D
DataBreaches.Net
SecWiki News
SecWiki News
L
Lohrmann on Cybersecurity
I
InfoQ
MyScale Blog
MyScale Blog
量子位
Cyberwarzone
Cyberwarzone
博客园 - 三生石上(FineUI控件)
The Hacker News
The Hacker News
F
Fortinet All Blogs
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Jina AI
Jina AI
博客园_首页
H
Help Net Security
K
Kaspersky official blog
酷 壳 – CoolShell
酷 壳 – CoolShell
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
www.infosecurity-magazine.com
www.infosecurity-magazine.com
Webroot Blog
Webroot Blog
Blog — PlanetScale
Blog — PlanetScale
V
Vulnerabilities – Threatpost
Y
Y Combinator Blog
The Cloudflare Blog
P
Proofpoint News Feed
V
Visual Studio Blog
C
Cyber Attacks, Cyber Crime and Cyber Security
T
Tailwind CSS Blog
爱范儿
爱范儿
P
Privacy International News Feed
Security Archives - TechRepublic
Security Archives - TechRepublic
The GitHub Blog
The GitHub Blog
C
Cybersecurity and Infrastructure Security Agency CISA
B
Blog RSS Feed

博客园 - Jacker.W

sql2008分区表相关SQL语句 sql经典语句 (机加类)生产计划管理系统介绍 MasterCam9 CNC程序文件转刀单 程序 关闭UPD123、1900、137、138和TCP135、139、445、3389端口的方法 - Jacker.W - 博客园 网络不存在或尚未启动的解决方法 关于幸福 这两天怎么了? 古名人们啊,该你们站起来澄清的时候到了! 《财经法规与会计职业道德》学习小记(一) 《会计基础》学习小记(一) 芙蓉障里人工湖 可怜的包法利夫人 奥运年来了 教你如何防范电脑病毒入侵 今天学习灭火,灭火演习正上演 “嫦娥一号”可以证明美国当年登月是否属实 晒晒可恶垃圾短信 征烧烤广告宣传图
c# 自定义甘特表格控件 (原创 学习中请指导)
Jacker.W · 2015-07-10 · via 博客园 - Jacker.W

  2、内容以甘特图一样显示计划和进度相关信息,根据计划开工和结束日期、计划时长 和实际开工和结束日期、实际时长画出开发甘特图内容,并以不同颜色标色,是否延误、是否提前等信息。

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

using System.Threading.Tasks;
using System.Management;

//滚动条
using System.Drawing.Drawing2D;  
using System.Drawing.Design;
using System.Runtime.InteropServices;
using System.Windows.Forms.VisualStyles; 


/*
using System.Drawing;//提供对GDI+基本图形功能的访问
using System.Drawing.Drawing2D;//提供高级的二维和矢量图像功能
using System.Drawing.Imaging;//提供高级GDI+图像处理功能
using System.Drawing.Printing;//提供打印相关服务
using System.Drawing.Text;//提供高级GDI+排版功能
using System.Drawing.Design;//扩展设计时,用户界面逻辑和绘制的类。用于扩展,自定义
  
Graphics类主要成员方法:
名称            说明 
DrawArc         画弧 
DrawBezier      画立体的贝塞尔曲线 
DrawBeziers     画连续立体的贝塞尔曲线 
DrawClosedCurve 画闭合曲线 
DrawCurve       画曲线 
DrawEllipse     画椭圆 
DrawImage      画图像 
DrawLine        画线 
DrawPath        通过路劲画线和曲线 
DrawPie         画饼图 
DrawPolygon     画多边形 
DrawRectangle   画矩形 
DrawString      绘制文字 
FillEllipse     填充椭圆 
FillPath        填充路劲 
FillPie         填充饼图 
FillPolygon     填充多边形 
FillRectangle   填充矩形 
FillRectangles  填充矩形组 
FillRegion      填充区域 
*/
namespace MyGanttTitleControl
{
    public partial class GanttTitle: UserControl
    {
        public GanttTitle()
        {
            SetStyle(ControlStyles.AllPaintingInWmPaint, true);
            SetStyle(ControlStyles.UserPaint, true);
            SetStyle(ControlStyles.OptimizedDoubleBuffer,true);
            SetStyle(ControlStyles.Selectable,true);
            SetStyle(ControlStyles.ResizeRedraw, true);
            SetStyle(ControlStyles.SupportsTransparentBackColor, true);
            InitializeComponent();

        }
        //枚举周星期值
        public enum _meWeekValue
        {
            Monday = 1,
            Tuesday = 2,
            Wednesday = 3,
            Thursday = 4,
            Friday = 5,
            Saturday = 6,
            Sunday = 7
        }
        private _meWeekValue _firstDayofWeek = _meWeekValue.Sunday;
        [Category("表头设置"), Description("每周第一天是星期几")]
        public _meWeekValue FirstDayofWeek
        {
            get { return _firstDayofWeek; }
            set { _firstDayofWeek = value; }
        }

        //枚举每年第一周
        public enum _meFirstWeekofYear
        {
            FirstDayofYear = 1,//每年第一天起为第一周
            FirstDayofWeek = 2//每年当周的第一天起为第一周,以FirstDayofWeek设置为准
}
private _meFirstWeekofYear _firstDayofYear = _meFirstWeekofYear.FirstDayofYear; [Category("表头设置"), Description("每年第一周")] public _meFirstWeekofYear FirstDayofYear { get { return _firstDayofYear; } set { _firstDayofYear = value; } } private DateTime tempGanttDate; private DateTime _startGanttDate = DateTime.Now.Date; [Category("表头设置"), Description("起始日期")] public DateTime GanttStartDate { get { return _startGanttDate; } set { tempGanttDate = _startGanttDate; _startGanttDate = value.Date; if (DateTime.Compare(_startGanttDate , _endGanttDate)>0) _startGanttDate = tempGanttDate; } } private DateTime _endGanttDate = DateTime.Now.Date.AddDays(43); [Category("表头设置"), Description("结束日期")] public DateTime GanttEndDate { get { return _endGanttDate; } set { tempGanttDate = _endGanttDate; _endGanttDate = value.Date ; if (DateTime.Compare(_startGanttDate, _endGanttDate) > 0) _endGanttDate = tempGanttDate; } } //甘特图表每格表头方式 public enum _ganttTitleCellWay { OnTime = 1,//按24时计 ByTheDay = 2,//按天计 Weekly = 3,//按周计 UserDefinedDays = 4//自定义天数 } private _ganttTitleCellWay _setGanttTitleCellWay = _ganttTitleCellWay.OnTime; [Category("表头设置"),Description("甘特图表每格表头方式")] public _ganttTitleCellWay SetGanttTitleCellWay { get { return _setGanttTitleCellWay; } set { _setGanttTitleCellWay = value; switch (_setGanttTitleCellWay.ToString()) { case "OnTime": _maxCellScale = 6; break; case "ByTheDay": _maxCellScale = 1; break; case "Weekly": _maxCellScale = 7; break; case "UserDefinedDays": _maxCellScale = 1; break; } } } private int _maxCellScale = 6; [Category("表头设置"), Description("表头单元格最大刻度")] public int MaxCellScale { get { return _maxCellScale; } set { int _maxDays=DateTime.IsLeapYear(GanttStartDate.Year)==true?366:355; _maxCellScale = value; switch (SetGanttTitleCellWay.ToString()) { case "OnTime": { if (_maxCellScale <= 0 || _maxCellScale > 24) { _maxCellScale = 6; } else { _maxCellScale = value; } break; } case "ByTheDay": { if (_maxCellScale != 1) { _maxCellScale = 1; } else { _maxCellScale = value; } break; } case "Weekly": { if (_maxCellScale != 7) { _maxCellScale = 7; } else { _maxCellScale = value; } break; } case "UserDefinedDays": { if (_maxCellScale <= 0 || _maxCellScale > _maxDays) { _maxCellScale = 1; } else { _maxCellScale = value; } break; } } } } private int _titleCellWidth = 140; [Category("表头设置"), Description("表头单元格宽度")] public int TitleCellWidth { get { return _titleCellWidth; } set { _titleCellWidth = value; } } private int _titleCellHeight = 45; [Category("表头设置"), Description("表头单元格宽度")] public int TitleCellHeight { get { return _titleCellHeight; } set { _titleCellHeight = value; } } private Color _titleBackColor=Control.DefaultForeColor ; [Category("表头设置"), Description("表头背景颜色")] public Color TitleBackColor { get { return _titleBackColor; } set { _titleBackColor = value; } } private Color _titleBorderColor = Color.SlateGray; [Category("表头设置"), Description("表头边框线颜色")] public Color TitleBorderColor { get { return _titleBorderColor; } set { _titleBorderColor = value; } } private Color _bigScaleColor = Color.Red ; [Category("表头设置"), Description("大刻度颜色")] public Color BigScaleColor { get { return _bigScaleColor; } set { _bigScaleColor = value; } } private Color _smallScaleColor = Color.Black; [Category("表头设置"), Description("小刻度颜色")] public Color SmallScaleColor { get { return _smallScaleColor; } set { _smallScaleColor = value; } } private Color _bigScaleFontColor = Color.Black; [Category("表头设置"), Description("刻度文字颜色")] public Color BigScaleFontColor { get { return _bigScaleFontColor; } set { _bigScaleFontColor = value; } } private Font _titleFont = new Font("宋体", 8); [Category("表头设置"), Description("表头文字字体")] public Font TitleFont { get { return _titleFont; } set { _titleFont = value; } } private int _backBigWidth = 800; [Category("表头设置"), Description("最大宽度")] public int BackBigWidth { get { return _backBigWidth; } set { _backBigWidth = value; } } private int _backBigHeight = 600; [Category("表头设置"), Description("最大高度")] public int BackBigHeight { get { return _backBigHeight; } set { _backBigHeight = value; } } private int _contCellHeight = 25; [Category("表内容设置"), Description("内容单元格宽度")] public int ContCellHeight { get { return _contCellHeight; } set { _contCellHeight = value; } } private int _contCellRow = 6; [Category("表内容设置"), Description("表格行数")] public int ContCellRow { get { return _contCellRow; } set { _contCellRow = value; } } private int _contCellColumn = 1; [Category("表内容设置"), Description("非进度表格列数")] public int ContCellColumn { get { return _contCellColumn; } set { _contCellColumn = value; } } private Color _contBackColor = Control.DefaultBackColor ; [Category("表内容设置"), Description("表格内容背景色")] public Color ContBackColor { get { return _contBackColor; } set { _contBackColor = value; } } private Color _contFontColor = Color.Black; [Category("表内容设置"), Description("表格内容文字颜色")] public Color ContFontColor { get { return _contFontColor; } set { _contFontColor = value; } } private Color _contGanttBorderColor = Color.DarkGray; [Category("表内容设置"), Description("进度框边框颜色")] public Color ContGanttBorderColor { get { return _contGanttBorderColor; } set { _contGanttBorderColor = value; } } private Color _contGanttFillColor = Color.Blue; [Category("表内容设置"), Description("进度框填充颜色")] public Color ContGanttFillColor { get { return _contGanttFillColor; } set { _contGanttFillColor = value; } } //获取到下一周第一天的天数 private int GetFirstDay(int setWeekNum,int curWeekNum) { curWeekNum = curWeekNum == 0 ? 7 : curWeekNum; if (curWeekNum <= setWeekNum) { return setWeekNum - curWeekNum; } else { return 7 - curWeekNum + setWeekNum; } } private DateTime GetFirstDay(DateTime lblCurDate,int setWeekNum,int curWeekNum) { int curNum; curWeekNum = curWeekNum == 0 ? 7 : curWeekNum; if (curWeekNum < setWeekNum) { curNum= setWeekNum - curWeekNum; } else { curNum= 7 - curWeekNum + setWeekNum; } return lblCurDate.AddDays(curNum-7); } //获取参数日期在当前年份的当前周数 private int GetCurWeek(DateTime lblCurDate) { int curWeekNum = 0; int curDays=lblCurDate.DayOfYear; int secWeekFirstDay = GetFirstDay((int)FirstDayofWeek, (int)DateTime.Parse(lblCurDate.Year.ToString() + "-1-1").DayOfWeek); if (FirstDayofYear.ToString() == "FirstDayofYear") { if (secWeekFirstDay != 0) { curDays = curDays - secWeekFirstDay; } curWeekNum = curDays % 7 > 0 ? curDays / 7 + 1 : curDays / 7; if (secWeekFirstDay != 0) { curWeekNum++; } } else { curDays = curDays - secWeekFirstDay; curWeekNum = curDays % 7 > 0 ? curDays / 7 + 1 : curDays / 7; } return curWeekNum; } private int curWeek; private DateTime tempDay; private Pen pn,p; private Brush b; private StringFormat titleFormat; /// <summary> /// 按时绘制图表 /// </summary> /// <param name="e"></param> /// <param name="totalDays"></param> private void OnTime_Paint(PaintEventArgs e, int totalDays) { int i, n; for (i = 1; i <= totalDays; i++) { e.Graphics.DrawRectangle(pn, i * TitleCellWidth, 0, TitleCellWidth, TitleCellHeight); e.Graphics.DrawLine(pn, i * TitleCellWidth, (int)TitleCellHeight / 3, (i + 1) * TitleCellWidth, (int)TitleCellHeight / 3); e.Graphics.DrawLine(pn, i * TitleCellWidth, (int)TitleCellHeight / 3 * 2, (i + 1) * TitleCellWidth, (int)TitleCellHeight / 3 * 2); tempDay = GanttStartDate.AddDays(i - 1); curWeek = GetCurWeek(tempDay); //画年月日及周数 e.Graphics.DrawString(tempDay.ToShortDateString(), TitleFont, b, (i * TitleCellWidth + (i + 1) * TitleCellWidth) / 2, 8, titleFormat); e.Graphics.DrawString("" + curWeek.ToString() + "" + tempDay.DayOfWeek.ToString(), TitleFont, b, (i * TitleCellWidth + (i + 1) * TitleCellWidth) / 2, (int)TitleCellHeight / 3 + 10, titleFormat); //画刻度线 for (n = 0; n < MaxCellScale; n++) { if (n > 0) { p = new Pen(SmallScaleColor, 1); e.Graphics.DrawLine(p, i * TitleCellWidth + (int)TitleCellWidth / MaxCellScale * n, TitleCellHeight, i * TitleCellWidth + (int)TitleCellWidth / MaxCellScale * n, TitleCellHeight - 3); e.Graphics.DrawString((24 / MaxCellScale * n).ToString(), TitleFont, b, i * TitleCellWidth + (int)TitleCellWidth / MaxCellScale * n, TitleCellHeight / 3 * 2 + 8, titleFormat); } else { p = new Pen(BigScaleColor, 1); e.Graphics.DrawLine(p, i * TitleCellWidth + (int)TitleCellWidth / MaxCellScale * n, TitleCellHeight, i * TitleCellWidth + (int)TitleCellWidth / MaxCellScale * n, TitleCellHeight - 5); } } } //画最后一行线框 p = new Pen(BigScaleColor, 1); e.Graphics.DrawLine(p, (totalDays + 1) * TitleCellWidth, TitleCellHeight, (totalDays + 1) * TitleCellWidth, TitleCellHeight - 5); } /// <summary> /// 按天绘制图表 /// </summary> /// <param name="e"></param> /// <param name="totalDays"></param> /// <param name="resWeek"></param> /// <param name="maxWeek"></param> /// <param name="maxCurWeek"></param> private void ByTheDay_Paint(PaintEventArgs e,int totalDays,int resWeek, int maxWeek, int maxCurWeek) { int i; int tempWeek, tempNum=0; int oldWeek = resWeek - 1; for (i = 1; i <= totalDays; i++) { tempDay = GanttStartDate.AddDays(i - 1); curWeek = GetCurWeek(tempDay); if (DateTime.Compare(tempDay, DateTime.Parse(GanttStartDate.Year.ToString() + "-12-31")) > 0) { tempWeek = maxWeek - resWeek + 1; } else { tempWeek = curWeek - resWeek + 1; } if (oldWeek != curWeek) { e.Graphics.DrawRectangle(pn, tempWeek * TitleCellWidth, 0, TitleCellWidth, TitleCellHeight); e.Graphics.DrawLine(pn, tempWeek * TitleCellWidth, (int)TitleCellHeight / 3, (tempWeek + 1) * TitleCellWidth, (int)TitleCellHeight / 3); e.Graphics.DrawLine(pn, tempWeek * TitleCellWidth, (int)TitleCellHeight / 3 * 2, (tempWeek + 1) * TitleCellWidth, (int)TitleCellHeight / 3 * 2); e.Graphics.DrawString(tempDay.Year.ToString() + "" + tempDay.Month.ToString() + "" + "" + curWeek.ToString() + "", TitleFont, b, (tempWeek * TitleCellWidth + (tempWeek + 1) * TitleCellWidth) / 2, 10, titleFormat); oldWeek = curWeek; } if (i == 1) { DateTime tempFirstDate = GetFirstDay(GanttStartDate, (int)FirstDayofWeek, (int)GanttStartDate.DayOfWeek); tempNum = tempWeek * TitleCellWidth + TitleCellWidth / 7 * (GanttStartDate - tempFirstDate).Days; } else { tempNum = tempNum + TitleCellWidth / 7; } e.Graphics.DrawLine(pn, tempNum, (int)TitleCellHeight / 3, tempNum, (int)TitleCellHeight); e.Graphics.DrawString(tempDay.Day.ToString(), TitleFont, b, (tempNum * 2 + (int)(TitleCellWidth / 7)) / 2, (int)TitleCellHeight / 3 * 2 + 10, titleFormat); switch ((int)tempDay.DayOfWeek) { case 0: e.Graphics.DrawString("", TitleFont, b, (tempNum * 2 + TitleCellWidth / 7) / 2, TitleCellHeight / 3 + 10, titleFormat); break; case 1: e.Graphics.DrawString("", TitleFont, b, (tempNum * 2 + TitleCellWidth / 7) / 2, TitleCellHeight / 3 + 10, titleFormat); break; case 2: e.Graphics.DrawString("", TitleFont, b, (tempNum * 2 + TitleCellWidth / 7) / 2, TitleCellHeight / 3 + 10, titleFormat); break; case 3: e.Graphics.DrawString("", TitleFont, b, (tempNum * 2 + TitleCellWidth / 7) / 2, TitleCellHeight / 3 + 10, titleFormat); break; case 4: e.Graphics.DrawString("", TitleFont, b, (tempNum * 2 + TitleCellWidth / 7) / 2, TitleCellHeight / 3 + 10, titleFormat); break; case 5: e.Graphics.DrawString("", TitleFont, b, (tempNum * 2 + TitleCellWidth / 7) / 2, TitleCellHeight / 3 + 10, titleFormat); break; case 6: e.Graphics.DrawString("", TitleFont, b, (tempNum * 2 + TitleCellWidth / 7) / 2, TitleCellHeight / 3 + 10, titleFormat); break; } } //画最后一行线框 e.Graphics.DrawLine(pn, tempNum + (int)(TitleCellWidth / 7), (int)TitleCellHeight / 3, tempNum + (int)(TitleCellWidth / 7), (int)TitleCellHeight); } /// <summary> /// 按周绘制图表 /// </summary> /// <param name="e"></param> /// <param name="totalDays"></param> /// <param name="resMonth"></param> /// <param name="maxCurMonth"></param> /// <param name="maxMonth"></param> private void Weekly_Paint(PaintEventArgs e,int totalDays,int resMonth,int maxCurMonth,int maxMonth) { int i,n; int resWeek = 0,maxWeek = 0, maxCurWeek = 0, oldWeek = resWeek - 1; e.Graphics.DrawLine(pn, TitleCellWidth, (int)TitleCellHeight / 3, (maxMonth - resMonth + 2) * TitleCellWidth, (int)TitleCellHeight / 3); e.Graphics.DrawLine(pn, TitleCellWidth, (int)TitleCellHeight / 3 * 2, (maxMonth - resMonth + 2) * TitleCellWidth, (int)TitleCellHeight / 3 * 2); for (i = 1; i <= maxMonth-resMonth+1; i++) { e.Graphics.DrawRectangle(pn, i * TitleCellWidth, 0, TitleCellWidth, TitleCellHeight / 3 * 2); e.Graphics.DrawLine(pn, i * TitleCellWidth, (int)TitleCellHeight / 3, (i + 1) * TitleCellWidth, TitleCellHeight / 3); if (maxMonth <= 12) { e.Graphics.DrawString(GanttStartDate.Year.ToString() + "" + (resMonth+i-1).ToString() + "", TitleFont, b, (i * TitleCellWidth + (i + 1) * TitleCellWidth) / 2, TitleCellHeight / 6+3, titleFormat); } else { e.Graphics.DrawString((GanttStartDate.Year + 1).ToString() + "" + (i - 12).ToString() + "", TitleFont, b, (i * TitleCellWidth + (i + 1) * TitleCellWidth) / 2, TitleCellHeight / 6+3, titleFormat); } } //初始周数 resWeek = GetCurWeek(GanttStartDate); //当年最大周数 maxCurWeek = GetCurWeek(DateTime.Parse(GanttStartDate.Year.ToString() + "-12-31")); //最后日期所在周数 maxWeek = GetCurWeek(GanttStartDate.AddDays(totalDays)); if (DateTime.Compare(GanttStartDate.AddDays(totalDays), DateTime.Parse(GanttStartDate.Year.ToString() + "-12-31")) > 0) { maxWeek = maxCurWeek - resWeek + maxCurWeek - 1; } //开始日期当月最大和最小周数 int curFirstWeek = GetCurWeek(DateTime.Parse(GanttStartDate.Year.ToString() + "-" + GanttStartDate.Month.ToString() + "-1")); curWeek = GetCurWeek(DateTime.Parse(GanttStartDate.Year.ToString() + "-" + GanttStartDate.Month.ToString() + "-" + DateTime.DaysInMonth(GanttStartDate.Year, GanttStartDate.Month).ToString())); int startPos; if (resWeek == curFirstWeek) { startPos = TitleCellWidth + TitleCellWidth / 4; } else { startPos = TitleCellWidth + TitleCellWidth / 4 * (resWeek - curFirstWeek); } //画起始周数线 e.Graphics.DrawLine(pn, startPos - TitleCellWidth / 4, TitleCellHeight / 3, startPos - TitleCellWidth / 4, TitleCellHeight / 3 * 2); e.Graphics.DrawLine(p, startPos - TitleCellWidth / 4, TitleCellHeight, startPos - TitleCellWidth / 4, TitleCellHeight - 3); //画起始周数 DateTime tempFirstDate = GetFirstDay(GanttStartDate, (int)FirstDayofWeek, (int)GanttStartDate.DayOfWeek); e.Graphics.DrawString(tempFirstDate.Day.ToString(), TitleFont, b, startPos - TitleCellWidth / 4, (TitleCellHeight + TitleCellHeight / 3 * 2) / 2, titleFormat); for (n = 0; n <= maxWeek - resWeek; n++) { //画周数线 e.Graphics.DrawLine(pn, n * TitleCellWidth / 4 + startPos, TitleCellHeight / 3, n * TitleCellWidth / 4 + startPos, TitleCellHeight / 3 * 2); e.Graphics.DrawString((resWeek + n).ToString()+"", TitleFont, b, n * TitleCellWidth / 4 + startPos - 18, (TitleCellHeight / 3 * 2 + TitleCellHeight / 3) / 2 + 2, titleFormat); //画日期刻度 e.Graphics.DrawLine(p, n * TitleCellWidth / 4 + startPos, TitleCellHeight, n * TitleCellWidth / 4 + startPos, TitleCellHeight - 3); e.Graphics.DrawString(tempFirstDate.AddDays((n + 1) * 7).Day.ToString(), TitleFont, b, n * TitleCellWidth / 4 + startPos, (TitleCellHeight + TitleCellHeight / 3 * 2) / 2, titleFormat); } e.Graphics.DrawLine(pn, TitleCellWidth * (maxMonth - resMonth + 2), TitleCellHeight / 3 * 2, TitleCellWidth * (maxMonth - resMonth + 2), TitleCellHeight); } protected override void OnPaint(PaintEventArgs e) { base.OnPaint(e); int tempWidth = 0; int resWeek=0, maxWeek=0, maxCurWeek=0; int resMonth=0, maxCurMonth=12, maxMonth=0; int newWidth = this.Size.Width; int newHeight = this.Size.Height; int tempHeight = TitleCellHeight + ContCellHeight * (ContCellRow - 1); int totalDays = (GanttEndDate - GanttStartDate).Days + 1; int lastNum=1; switch (SetGanttTitleCellWay.ToString()) { case "OnTime": { tempWidth = (totalDays + 1) * TitleCellWidth; lastNum = totalDays; break; } case "ByTheDay": { //初始周数 resWeek = GetCurWeek(GanttStartDate); //当年最大周数 maxCurWeek = GetCurWeek(DateTime.Parse(GanttStartDate.Year.ToString() + "-12-31")); //最后日期所在周数 maxWeek = GetCurWeek(GanttStartDate.AddDays(totalDays)); if (DateTime.Compare(GanttStartDate.AddDays(totalDays), DateTime.Parse(GanttStartDate.Year.ToString() + "-12-31")) > 0) { maxWeek = maxCurWeek - resWeek + maxCurWeek + 1; } tempWidth = (maxWeek - resWeek + 2) * TitleCellWidth; lastNum = (maxWeek - resWeek + 1); break; } case "Weekly": { //初始月份 resMonth = GanttStartDate.Month; //最后日期所在月份 maxMonth = GanttStartDate.AddDays(totalDays).Month; if (DateTime.Compare(GanttStartDate.AddDays(totalDays), DateTime.Parse(GanttStartDate.Year.ToString() + "-12-31")) > 0) { maxMonth =maxCurMonth-resMonth+ maxCurMonth-1; } tempWidth = (maxMonth - resMonth + 2) * TitleCellWidth; lastNum = maxMonth - resMonth + 1; break; } case "UserDefinedDays": { break; } } //拖动滚动条 if (tempWidth > this.Size.Width) { newWidth = tempWidth; } if (tempHeight > this.Size.Height) { newHeight = tempHeight; } int bgWidht = newWidth; int bgHeight = newHeight; if (bgWidht > BackBigWidth) bgWidht = BackBigWidth; if (bgHeight > BackBigHeight) bgHeight = BackBigHeight; //this.Size = new System.Drawing.Size(bgWidht, bgHeight); this.AutoScrollMinSize = new System.Drawing.Size(newWidth + 10, newHeight + 10); e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; e.Graphics.TranslateTransform(this.AutoScrollPosition.X, this.AutoScrollPosition.Y); pn = new Pen(TitleBorderColor, 1); //定义文字格式 Brush bh =new SolidBrush(ContBackColor); b = new SolidBrush(BigScaleFontColor); p = new Pen(BigScaleColor, 1); titleFormat = new StringFormat(); titleFormat.LineAlignment = StringAlignment.Center; titleFormat.Alignment = StringAlignment.Center; //画首行首列 e.Graphics.FillRectangle(bh, 0, 0, newWidth, newHeight); e.Graphics.DrawRectangle(pn, 0, 0, newWidth, newHeight); e.Graphics.DrawRectangle(pn, 0, 0, TitleCellWidth, TitleCellHeight); e.Graphics.DrawString("生产单号", TitleFont, b, TitleCellWidth / 2, TitleCellHeight / 2, titleFormat); switch (SetGanttTitleCellWay.ToString()) { case "OnTime": { OnTime_Paint(e,totalDays); break; } case "ByTheDay": { ByTheDay_Paint(e,totalDays,resWeek, maxWeek, maxCurWeek); break; } case "Weekly": { Weekly_Paint(e,totalDays,resMonth, maxCurMonth, maxMonth); break; } case "UserDefinedDays": { break; } } b = new SolidBrush(ContFontColor); p = new Pen(ContGanttBorderColor,1); Brush bg = new SolidBrush(ContGanttFillColor); for (int j = 2; j <= ContCellRow; j++) { //画内容框 e.Graphics.DrawRectangle(pn, 0, TitleCellHeight + (j - 2) * ContCellHeight, TitleCellWidth, ContCellHeight); //画任务内容 e.Graphics.DrawString("F160256-00" + (j - 1).ToString() + "-00", TitleFont, b, TitleCellWidth / 2, TitleCellHeight + (j - 2) * ContCellHeight + ContCellHeight/2, titleFormat); //画进度框 e.Graphics.DrawRectangle(pn, TitleCellWidth, TitleCellHeight + (j - 2) * ContCellHeight, TitleCellWidth * lastNum, ContCellHeight); e.Graphics.DrawRectangle(p, TitleCellWidth + 2, TitleCellHeight + (j - 2) * ContCellHeight + 2, TitleCellWidth * (j - 1), ContCellHeight - 4); e.Graphics.FillRectangle(bg, TitleCellWidth + 2, TitleCellHeight + (j - 2) * ContCellHeight + 2, TitleCellWidth * (j - 1), ContCellHeight - 4); } } } //数据节点; public class GanttNodes { private string _gnlJobCode; //生产单号 private string _gnlProcNo; //工序号 private string _gnlProcName; //工序名称 private string _gnlMachine; //计划机床代号+机床名称 private string _gnlOpreator; //计划操作员 private DateTime _gnlStartDate; //开始日期时间 private DateTime _gnlEndDate; //结束日期时间 private float _gnlDuration; //时长 private enum _gnlUnitMode { Second = 0, // Minute = 1, //分钟 Hour = 2, //小时 Day = 3, // Week = 4, // } private _gnlUnitMode gntc_UnitMode = _gnlUnitMode.Minute; private float _gnlDaysofTime; //当天的有效时长; public string gntc_JobCode { get { return _gnlJobCode; } set { _gnlJobCode = value; } } public string gntc_ProcNo { get { return _gnlProcNo; } set { _gnlProcNo = value; } } public string gntc_ProcName { get { return _gnlProcName; } set { _gnlProcName = value; } } public string gntc_Machine { get { return _gnlMachine; } set { _gnlMachine = value; } } public string gntc_Opreator { get { return _gnlOpreator; } set { _gnlOpreator = value; } } public DateTime gntc_StartDate { get { return _gnlStartDate; } set { _gnlStartDate = value; } } public DateTime gntc_EndDate { get { return _gnlEndDate; } set { _gnlEndDate = value; } } public float gntc_Duration { get { return _gnlDuration; } set { _gnlDuration = value; } } private _gnlUnitMode gntc_UseUnitMode { set { gntc_UnitMode = value; } get { return gntc_UnitMode; } } public float gntc_DaysofTime { get { return _gnlDaysofTime; } set { _gnlDaysofTime = value; } } /// <summary> /// 时长单位转换 /// </summary> /// <param name="_gnlLong">时长</param> /// <param name="_gnlResUnit">原时长单位</param> /// <param name="_gnlNewUnit">新时长单位</param> /// <returns></returns> private float DurationTrans(float _gnlLong, _gnlUnitMode _gnlResUnit,_gnlUnitMode _gnlNewUnit,float _gnlDOT) { float result=0; switch (_gnlResUnit.ToString()) { case "Second": { switch (_gnlNewUnit.ToString()) { case "Second": result = (float)_gnlLong; break; case "Minute": result = (float)_gnlLong / 60; break; case "Hour": result = (float)_gnlLong / (60 * 60); break; case "Day": result = (float)(_gnlLong / (60 * 60 * _gnlDOT)); break; case "Week": result = (float)(_gnlLong / (60 * 60 * _gnlDOT * 7)); break; } } break; case "Minute": { switch (_gnlNewUnit.ToString()) { case "Second": result = (float)_gnlLong * 60; break; case "Minute": result = (float)_gnlLong; break; case "Hour": result = (float)_gnlLong / 60; break; case "Day": result = (float)(_gnlLong / (60 * _gnlDOT)); break; case "Week": result = (float)_gnlLong / (60 * _gnlDOT*7); break; } } break; case "Hour": { switch (_gnlNewUnit.ToString()) { case "Second": result = (float)_gnlLong * 60*60; break; case "Minute": result = (float)_gnlLong * 60; break; case "Hour": result = (float)_gnlLong; break; case "Day": result = (float)_gnlLong / _gnlDOT; break; case "Week": result = (float)_gnlLong / (_gnlDOT * 7); break; } } break; case "Day": { switch (_gnlNewUnit.ToString()) { case "Second": result = (float)_gnlLong * _gnlDOT*60*60; break; case "Minute": result = (float)_gnlLong * _gnlDOT*60; break; case "Hour": result = (float)_gnlLong* _gnlDOT; break; case "Day": result = (float)_gnlLong; break; case "Week": result = (float)_gnlLong /7; break; } } break; case "Week": { switch (_gnlNewUnit.ToString()) { case "Second":result = (float)_gnlLong*7*_gnlDOT*60*60; break; case "Minute": result = (float)_gnlLong * 7 * _gnlDOT * 60; break; case "Hour":result = (float)_gnlLong*7*_gnlDOT; break; case "Day":result = (float)_gnlLong*7; break; case "Week": result = (float)_gnlLong; break; } } break; } return result; } public GanttNodes() { } } }