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 Braincase.GanttChart { /// /// An elaborate example on how the chart control might be used. /// Start by collapsing all regions and then read the constructor. /// Refer to IProjectManager interface for method description. /// public partial class ExampleFull : Form { OverlayPainter _mOverlay = new OverlayPainter(); ProjectManager _mManager = null; object selectCureetTask; public ExampleFull() { InitializeComponent(); CheckForIllegalCrossThreadCalls = false; _mOverlay.PrintMode = true; DataTable dt_CraftTasks; dt_CraftTasks = InitCraftTasks(); // Create a Project and some Tasks _mManager = new ProjectManager(); InitTasks(ref _mManager, dt_CraftTasks); // Initialize the Chart with our ProjectManager and CreateTaskDelegate _mChart.Init(_mManager); _mChart.CreateTaskDelegate = delegate() { return new MyTask(_mManager); }; // Attach event listeners for events we are interested in _mChart.TaskMouseOver += new EventHandler(_mChart_TaskMouseOver); _mChart.TaskMouseOut += new EventHandler(_mChart_TaskMouseOut); _mChart.TaskSelected += new EventHandler(_mChart_TaskSelected); _mChart.TaskMouseDrop += _mChart_TaskMouseDrop; _mChart.PlaySliderMouseDrag += _mChart_PlaySliderMouseDrag; _mChart.AllowTaskDragDrop = true; // Set Time information //_mManager.TimeScale = TimeScale.Day; } private void _mChart_TaskMouseDrop(object sender, TaskDragDropEventArgs e) { SetMaxCountLenth(); selectCureetTask = e.Source; textBox_ID.Text = ((MyTask)selectCureetTask).ID.ToString(); textBox_Start.Text = ((MyTask)selectCureetTask).Start.ToString(); textBox_Duration.Text = ((MyTask)selectCureetTask).Duration.ToString(); textBox_Name.Text = ((MyTask)selectCureetTask).Name; textBox_End.Text = (((MyTask)selectCureetTask).Start + ((MyTask)selectCureetTask).Duration).ToString(); _mChart.Invalidate(); } private DataTable InitCraftTasks() { DataTable dt = new DataTable(); // DataRow dr; dt.Columns.Add("CraftID"); dt.Columns.Add("Start"); dt.Columns.Add("Duration"); dt.Columns.Add("Name"); dt.Rows.Add("1", "1", "10", "工艺Name01"); dt.Rows.Add("2", "5", "20", "工艺Name02"); dt.Rows.Add("3", "10", "30", "工艺Name03"); dt.Rows.Add("4", "15", "10", "工艺Name04"); dt.Rows.Add("5", "10", "20", "工艺Name05"); dt.Rows.Add("6", "20", "20", "工艺Name06"); return dt; } private void InitTasks(ref ProjectManager m_Manager,DataTable dt) { string craftID ; string craftName ; string duration; string start ; for (int i = 0; i < dt.Rows.Count; i++) { craftID = dt.Rows[i]["CraftID"].ToString(); craftName = dt.Rows[i]["Name"].ToString(); duration = dt.Rows[i]["Duration"].ToString(); start = dt.Rows[i]["Start"].ToString(); TaskAdd(ref m_Manager, craftID, start, duration, craftName); } } private void TaskAdd(ref ProjectManager m_Manager, string m_CraftID,string m_Start, string m_Duration,string m_Name) { try { string craftID = m_CraftID; float duration = (float)Convert.ToDouble(m_Duration); float start = (float)Convert.ToDouble(m_Start); var taskName = new MyTask(_mManager) { Name = m_Name }; _mManager.Add(taskName); _mManager.SetID(taskName, craftID); _mManager.SetComplete(taskName, 1.0f); _mManager.SetDuration(taskName, duration); _mManager.SetStart(taskName, start); SetMaxCountLenth(); _mChart.Invalidate(); } catch(Exception err) { LogNet.LogisTrac.WriteLog(typeof(ExampleFull), err); } } private void SetMaxCountLenth() { float maxPlayLenth = 0; float maxLeneh=0; float tempLeneh =0; foreach(MyTask a in _mManager.Tasks) { tempLeneh = a.Start + a.Duration; if(tempLeneh> maxLeneh) { Task task = a; maxLeneh = tempLeneh; if (_mChart._mChartTaskRects.ContainsKey(task)) { maxPlayLenth = _mChart._mChartTaskRects[a].X + _mChart._mChartTaskRects[a].Width; } //maxPlayLenth=a. //MaxPlayLenth } } //最大播放长度 _mManager.MaxPlayLenth = maxPlayLenth; //时间最大值 _mManager.MaxCountLenth = maxLeneh; //间隔时间 _mManager.TimeInterval = (float)Convert.ToDouble(numericUpDown_TimeInterval.Value); //总帧数 _mManager.AllCountFrame = (int)(_mManager.MaxCountLenth / _mManager.TimeInterval); textBox_MaxEnd.Text = maxLeneh.ToString(); textBox_AllCountFrame.Text = _mManager.AllCountFrame.ToString(); } private void TaskSave() { foreach (MyTask a in _mManager.Tasks) { } } private void TaskDelete(MyTask taskName) { try { _mManager.Delete(taskName); selectCureetTask = null; textBox_ID.Text = ""; textBox_Start.Text = ""; textBox_Duration.Text = ""; textBox_Name.Text = ""; textBox_End.Text = ""; SetMaxCountLenth(); _mChart.Invalidate(); } catch(Exception err) { LogNet.LogisTrac.WriteLog(typeof(ExampleFull), err); } } private void SetTaskStart(string m_Start) { if(selectCureetTask!=null) { float start = (float)Convert.ToDouble(m_Start); _mManager.SetStart((MyTask)selectCureetTask, start); SetMaxCountLenth(); _mChart.Invalidate(); } } /// /// 选择task (e.Task) /// /// /// void _mChart_TaskSelected(object sender, TaskMouseEventArgs e) { SetMaxCountLenth(); textBox_ID.Text = e.Task.ID.ToString(); textBox_Start.Text = e.Task.Start.ToString(); textBox_Duration.Text = e.Task.Duration.ToString(); textBox_Name.Text = e.Task.Name; textBox_End.Text = (e.Task.Start + e.Task.Duration).ToString(); selectCureetTask = e.Task; } void _mChart_TaskMouseOut(object sender, TaskMouseEventArgs e) { lblStatus.Text = ""; _mChart.Invalidate(); } void _mChart_TaskMouseOver(object sender, TaskMouseEventArgs e) { lblStatus.Text = string.Format("工艺代码:{0} 开始时间: {1} 持续时间: {2} 结束时间: {3} 工艺名称: {4}",e.Task.ID.ToString(), e.Task.Start.ToString(),e.Task.Duration.ToString(), e.Task.End.ToString(),e.Task.Name); _mChart.Invalidate(); } private void button_ZoomPlus_Click(object sender, EventArgs e) { if (_mChart.ZoomScaleIndex > 0) { _mChart.ZoomScaleIndex--; _mChart.Refresh(); SetMaxCountLenth(); } } private void button1_ZoomRedues_Click(object sender, EventArgs e) { if (_mChart.ZoomScaleIndex < 10) { _mChart.ZoomScaleIndex++; _mChart.Refresh(); SetMaxCountLenth(); } } /// /// 增加工艺 /// /// /// private void button_Insert_Click(object sender, EventArgs e) { string craftID = textBox_ID.Text; string start = textBox_Start.Text; string duration = textBox_Duration.Text; string craftName = textBox_Name.Text; ProjectManager m_Manager = _mManager; TaskAdd(ref m_Manager, craftID, start, duration, craftName); } /// /// 删除选择的工艺 /// /// /// private void button_Delete_Click(object sender, EventArgs e) { MyTask taskName; if (selectCureetTask == null) return; taskName = (MyTask)selectCureetTask; TaskDelete(taskName); } /// /// 保存工艺规划 /// /// /// private void button_Save_Click(object sender, EventArgs e) { TaskSave(); } private void textBox_Start_KeyUp(object sender, KeyEventArgs e) { try { string m_Start = textBox_Start.Text; SetTaskStart(m_Start); } catch(Exception err) { LogNet.LogisTrac.WriteLog(typeof(ExampleFull), err); } } private void numericUpDown_TimeInterval_ValueChanged(object sender, EventArgs e) { SetMaxCountLenth(); } } #region overlay painter /// /// An example of how to encapsulate a helper painter for painter additional features on Chart /// public class OverlayPainter { /// /// Hook such a method to the chart paint event listeners /// /// /// public void ChartOverlayPainter(object sender, ChartPaintEventArgs e) { // Don't want to print instructions to file if (this.PrintMode) return; var g = e.Graphics; var chart = e.Chart; // Demo: Static billboards begin ----------------------------------- // Demonstrate how to draw static billboards // "push matrix" -- save our transformation matrix e.Chart.BeginBillboardMode(e.Graphics); // draw mouse command instructions int margin = 300; int left = 20; var color = chart.HeaderFormat.Color; StringBuilder builder = new StringBuilder(); builder.AppendLine("THIS IS DRAWN BY A CUSTOM OVERLAY PAINTER TO SHOW DEFAULT MOUSE COMMANDS."); builder.AppendLine("*******************************************************************************************************"); builder.AppendLine("Left Click - Select task and display properties in PropertyGrid"); builder.AppendLine("Left Mouse Drag - Change task starting point"); builder.AppendLine("Right Mouse Drag - Change task duration"); builder.AppendLine("Middle Mouse Drag - Change task complete percentage"); builder.AppendLine("Left Doubleclick - Toggle collaspe on task group"); builder.AppendLine("Right Doubleclick - Split task into task parts"); builder.AppendLine("Left Mouse Dragdrop onto another task - Group drag task under drop task"); builder.AppendLine("Right Mouse Dragdrop onto another task part - Join task parts"); builder.AppendLine("SHIFT + Left Mouse Dragdrop onto another task - Make drop task precedent of drag task"); builder.AppendLine("ALT + Left Dragdrop onto another task - Ungroup drag task from drop task / Remove drop task from drag task precedent list"); builder.AppendLine("SHIFT + Left Mouse Dragdrop - Order tasks"); builder.AppendLine("SHIFT + Middle Click - Create new task"); builder.AppendLine("ALT + Middle Click - Delete task"); builder.AppendLine("Left Doubleclick - Toggle collaspe on task group"); var size = g.MeasureString(builder.ToString(), e.Chart.Font); var background = new Rectangle(left, chart.Height - margin, (int)size.Width, (int)size.Height); background.Inflate(10, 10); g.FillRectangle(new System.Drawing.Drawing2D.LinearGradientBrush(background, Color.LightYellow, Color.Transparent, System.Drawing.Drawing2D.LinearGradientMode.Vertical), background); g.DrawRectangle(Pens.Brown, background); g.DrawString(builder.ToString(), chart.Font, color, new PointF(left, chart.Height - margin)); // "pop matrix" -- restore the previous matrix e.Chart.EndBillboardMode(e.Graphics); // Demo: Static billboards end ----------------------------------- } public bool PrintMode { get; set; } } #endregion overlay painter #region custom task and resource /// /// A custom resource of your own type (optional) /// [Serializable] public class MyResource { public string Name { get; set; } } /// /// A custom task of your own type deriving from the Task interface (optional) /// [Serializable] public class MyTask : Task { public MyTask(ProjectManager manager) : base() { Manager = manager; } private ProjectManager Manager { get; set; } //1代表0.001 public new float Start { get { return base.Start; } set { Manager.SetStart(this, value); } } public new float End { get { return base.End; } set { Manager.SetEnd(this, value); } } public new float Duration { get { return base.Duration; } set { Manager.SetDuration(this, value); } } public new float Complete { get { return base.Complete; } set { Manager.SetComplete(this, value); } } public new string ID { get { return base.ID; } set { Manager.SetID(this, value); } } } #endregion custom task and resource }