本人想作一個甘特表格控件,主要實現以下功能:數據庫
一、表頭爲相關任務列和顯示日期類形式(分月、周、日、時),ide
二、內容以甘特圖同樣顯示計劃和進度相關信息,根據計劃開工和結束日期、計劃時長 和實際開工和結束日期、實際時長畫出開發甘特圖內容,並以不一樣顏色標色,是否延誤、是否提早等信息。字體
三、當鼠標移動到某個任務圖上時顯示相關完整的任務信息,如計劃開工和結束日期、計劃時長,實際開工和結束日期、實際時長、當前狀態,計劃數量、完成數量等。this
目前第一步表頭已實現,第二步簡易實現,未獲取數據庫信息實現,第三步又怎麼實現?本人貼出控件所有代碼,但願可以獲得朋友們的支持,謝謝。spa
1、自定義控件第一類表頭,表頭刻度是以天天按小時分段顯示,可調整刻度數 設計
2、自定義控件第二類表頭,表頭是以天天分段顯示。 code
3、自定義控件第三類表頭,表頭刻度是以按周分段顯示。 orm
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() { } } }