www.pudn.com > WorkflowDesigner.rar > DesignerCtrl.cs, change:2007-11-28,size:12763b


using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Drawing; 
using System.Data; 
using System.Text; 
using System.Windows.Forms; 
 
namespace WorkflowDesigner.Designer 
{ 
    public partial class DesignerCtrl : UserControl 
    { 
        /// <summary> 
        /// 工作流设计器 
        /// </summary> 
        public DesignerCtrl() 
        { 
            InitializeComponent(); 
            Document = new WfFlowDocument(); 
            //设置第一次的标志位 
        } 
 
        #region 字段 
        /// <summary> 
        /// 工作流设计器文档 
        /// </summary> 
        private WfFlowDocument _document; 
 
        /// <summary> 
        /// 当前工具 
        /// </summary> 
        private DesignerTool _currentTool; 
 
  
        #endregion 
 
        #region 属性 
 
        /// <summary> 
        /// 工作流设计器文档 
        /// </summary> 
        public WfFlowDocument Document 
        { 
            get { return _document; } 
            set 
            { 
                //设置初识值 
                this.vScrollBar1.Value = 0; 
                this.hScrollBar1.Value = 0; 
                designerPanel.Left = 25; 
                designerPanel.Top = 25; 
                _document = value; 
            } 
        } 
        /// <summary> 
        /// 得到横向滚动条和竖向滚动条交叉的(top,hight)点 
        /// </summary> 
        public Point ScrollOffset 
        { 
            get 
            { 
                return new Point(this.hScrollBar1.Value, vScrollBar1.Value); 
            } 
        } 
 
        /// <summary> 
        /// 当前工具 
        /// </summary> 
        public DesignerTool CurrentTool 
        { 
            get 
            { 
                return _currentTool; 
            } 
            set 
            { 
                if (_currentTool == null || _currentTool.GetType() != value.GetType()) 
                { 
                    CurrentToolChangeEventArg e = new CurrentToolChangeEventArg(); 
                    e.oldTool = _currentTool; 
                    e.newTool = value; 
                    _currentTool = value; 
                    _currentTool.Ctrl = this; 
                    if (CurrentToolChange != null) 
                    { 
                        CurrentToolChange(this, e); 
                    } 
                } 
            } 
        } 
        #endregion 
 
        #region 构造函数 
 
        #endregion 
 
        #region 操作 
 
        #region 私有 
 
        /// <summary> 
        /// 当设计器读取时 初始设置当前工具为“指针”工具 
        /// </summary> 
        /// <param name="sender">事件参数</param> 
        /// <param name="e">事件参数</param> 
        private void DesignerCtrl_Load(object sender, EventArgs e) 
        { 
            //将当前工具设置为选择工具 
            CurrentTool = new DesignerSelector(); 
        } 
 
        /// <summary> 
        /// 当设计器重绘制时 绘制所有可视化对象 先绘制线 再绘制节点 
        /// </summary> 
        /// <param name="sender">事件参数</param> 
        /// <param name="e">事件参数</param> 
        private void DesignerCtrl_Paint(object sender, PaintEventArgs e) 
        { 
            //第一次的时候需要话虚线 
 
            // sm add 先画线再画其他的东西 
            foreach (DesignerVisualObject vo in this._document.VisualObjectList) 
            { 
                if (vo is WfTransition) 
                { 
                    vo.Draw(e.Graphics); 
                } 
            } 
 
            foreach (DesignerVisualObject vo in this._document.VisualObjectList) 
            { 
                if (vo is WfBackTransition) 
                { 
                    vo.Draw(e.Graphics); 
                } 
            } 
 
            foreach (DesignerVisualObject vo in this._document.VisualObjectList) 
            { 
                if ((!(vo is WfTransition)) && (!(vo is WfBackTransition))) 
                { 
                    vo.Draw(e.Graphics); 
                } 
            } 
        } 
 
         
        /// <summary> 
        /// 当鼠标按下时根据选择的当前工具触发工具的鼠标按下事件 
        /// </summary> 
        /// <param name="sender">事件参数</param> 
        /// <param name="e">事件参数</param> 
        private void DesignerCtrl_MouseDown(object sender, MouseEventArgs e) 
        { 
            this.Focus(); 
            if (e.Button == MouseButtons.Left) 
            { 
                this.CurrentTool.OnMouseDown(e.X, e.Y); 
            } 
             
        } 
        /// <summary> 
        /// 当鼠标移动时根据选择的当前工具触发工具的鼠标移动事件(画布必须被鼠标捕获) 
        /// </summary> 
        /// <param name="sender">事件参数</param> 
        /// <param name="e">事件参数</param> 
        private void DesignerCtrl_MouseMove(object sender, MouseEventArgs e) 
        { 
            if (e.Button == MouseButtons.Left) 
            { 
                if (this.HasCapture()) 
                { 
                    this.CurrentTool.OnMouseMove(e.X, e.Y); 
                } 
            } 
             
        } 
 
        /// <summary> 
        /// 当鼠标弹开时根据选择的当前工具触发工具的鼠标弹开事件 
        /// </summary> 
        /// <param name="sender">事件参数</param> 
        /// <param name="e">事件参数</param> 
        private void DesignerCtrl_MouseUp(object sender, MouseEventArgs e) 
        { 
            if (e.Button == MouseButtons.Left) 
            { 
                this.CurrentTool.OnMouseUp(e.X, e.Y); 
            } 
             
        } 
 
        /// <summary> 
        /// 当鼠标更改捕获,取消所有捕获 
        /// </summary> 
        /// <param name="sender">事件参数</param> 
        /// <param name="e">事件参数</param> 
        private void designerPanel_MouseCaptureChanged(object sender, EventArgs e) 
        { 
            this.CurrentTool.OnCancelCapture(); 
        } 
 
        /// <summary> 
        /// 当delete键按下时 删除要删除的对象 
        /// </summary> 
        /// <param name="sender">事件参数</param> 
        /// <param name="e">事件参数</param> 
        private void designerPanel_KeyDown(object sender, KeyEventArgs e) 
        { 
            if (e.KeyCode == Keys.Delete) 
            { 
 
 
                if (MessageBox.Show("是否删除该节点?", "删除节点", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) 
                { 
 
                    foreach (DesignerVisualObject vo in this._document.DeleteSelected()) 
                    { 
                        this.FireVisualObjectDeleteEvent(vo); 
 
                    } 
                    this.RedrawAll(); 
                } 
            } 
             
        } 
 
        /// <summary> 
        /// 当鼠标双击时打开鼠标对话框 
        /// </summary> 
        /// <param name="sender">事件参数</param> 
        /// <param name="e">事件参数</param> 
        private void designerPanel_MouseDoubleClick(object sender, MouseEventArgs e) 
        { 
            this._document.OpenPropertyDialogAtPoint(e.X, e.Y); 
            this.RedrawAll(); 
        } 
 
        /// <summary> 
        /// 当竖向滚动条滚动时 
        /// </summary> 
        /// <param name="sender">事件参数</param> 
        /// <param name="e">事件参数</param> 
        private void vScrollBar1_Scroll(object sender, ScrollEventArgs e) 
        { 
            this.designerPanel.Top = -e.NewValue + 25; 
 
        } 
 
        /// <summary> 
        /// 当横向滚动条滚动时 
        /// </summary> 
        /// <param name="sender">事件参数</param> 
        /// <param name="e">事件参数</param> 
        private void hScrollBar1_Scroll(object sender, ScrollEventArgs e) 
        { 
            this.designerPanel.Left = -e.NewValue + 25; 
        } 
 
        /// <summary> 
        /// 重新定义尺寸,重新初始化滚动条参数 
        /// </summary> 
        /// <param name="sender"></param> 
        /// <param name="e"></param> 
        private void DesignerCtrl_Resize(object sender, EventArgs e) 
        { 
            if (Height == 0 || Width == 0) 
            { 
                return; 
            } 
            int pageSize = Height - this.hScrollBar1.Height; 
            this.vScrollBar1.LargeChange = pageSize; 
            vScrollBar1.Maximum = designerPanel.Width + 50; 
 
 
            pageSize = Width - vScrollBar1.Width; 
            hScrollBar1.Maximum = designerPanel.Height + 50; 
            hScrollBar1.LargeChange = pageSize; 
 
        } 
        #endregion 
 
        #region 保护 
 
        #endregion 
 
        #region 公用 
 
        /// <summary> 
        /// 释放鼠标捕获的画布 
        /// </summary> 
        public void ReleaseCapture() 
        { 
            this.designerPanel.Capture = false; 
        } 
        /// <summary> 
        /// 鼠标是否捕获的画布 
        /// </summary> 
        /// <returns>ture捕获 false为未捕获</returns> 
        public bool HasCapture() 
        { 
            return designerPanel.Capture; 
        } 
        /// <summary> 
        /// 使画布被鼠标捕获 
        /// </summary> 
        public void SetCapture() 
        { 
            designerPanel.Capture = true; 
        } 
        /// <summary> 
        /// 重新绘制 
        /// </summary> 
        public void RedrawAll() 
        { 
            //下次绘制时重新绘制 
            designerPanel.Invalidate(); 
        } 
 
        #endregion 
 
        #region 内部 
        /// <summary> 
        /// 触发增加可视化对象的事件 
        /// </summary> 
        /// <param name="newObject">可视化对象基类</param> 
        internal void FireVisualObjectAddEvent(DesignerVisualObject newObject) 
        { 
            if (VisualObjectAdd != null) 
            { 
                VisualObjectAddEventArg e = new VisualObjectAddEventArg(); 
                e.newObject = newObject; 
                VisualObjectAdd(this, e); 
            } 
        } 
 
        /// <summary> 
        /// 触发删除可视化对象的事件 
        /// </summary> 
        /// <param name="deleteObject">要删除的可视化对象基类</param> 
        internal void FireVisualObjectDeleteEvent(DesignerVisualObject deleteObject) 
        { 
            if (VisualObjectDelete != null) 
            { 
                VisualObjectDeleteEventArg e = new VisualObjectDeleteEventArg(); 
                e.deleteObject = deleteObject; 
                VisualObjectDelete(this, e); 
            } 
        } 
        #endregion 
        #endregion 
 
 
        #region 事件 
        /// <summary> 
        /// 当前工具变换委托函数 
        /// </summary> 
        /// <param name="sender">当前设计工具</param> 
        /// <param name="e">当前工具变换事件参数类</param> 
        public delegate void CurrentToolChangeEventHandler(object sender, CurrentToolChangeEventArg e); 
        /// <summary> 
        /// 当前工具变换事件 
        /// </summary> 
        public event CurrentToolChangeEventHandler CurrentToolChange; 
 
 
        /// <summary> 
        /// 当前工具变换委托函数 
        /// </summary> 
        /// <param name="sender">当前设计工具</param> 
        /// <param name="e">当前工具变换事件参数类</param> 
        public delegate void VisualObjectAddHandle(object sender, VisualObjectAddEventArg e); 
        /// <summary> 
        /// 当前工具变换事件 
        /// </summary> 
        public event VisualObjectAddHandle VisualObjectAdd; 
 
 
 
 
 
        /// <summary> 
        /// 删除可视化对象委托函数 
        /// </summary> 
        /// <param name="sender">当前设计工具</param> 
        /// <param name="e">删除可视化对象事件参数类</param> 
        public delegate void VisualObjectDeleteHandle(object sender, VisualObjectDeleteEventArg e); 
        /// <summary> 
        /// 删除可视化对象事件 
        /// </summary> 
        public event VisualObjectDeleteHandle VisualObjectDelete; 
 
        #endregion 
         
    } 
 
    #region 事件类 
    /// <summary> 
    /// 当前工具变换事件参数类 
    /// </summary> 
    public class CurrentToolChangeEventArg : EventArgs 
    { 
        /// <summary> 
        /// 原先的设计工具 
        /// </summary> 
        public DesignerTool oldTool; 
        /// <summary> 
        /// 新的设计工具 
        /// </summary> 
        public DesignerTool newTool; 
    } 
 
    /// <summary> 
    /// 新增可视对象参数 
    /// </summary> 
    public class VisualObjectAddEventArg : EventArgs 
    { 
        /// <summary> 
        /// 新增对象 
        /// </summary> 
        public DesignerVisualObject newObject; 
    } 
 
    /// <summary> 
    /// 删除可视对象参数 
    /// </summary> 
    public class VisualObjectDeleteEventArg : EventArgs 
    {  
        /// <summary> 
        /// 删除对象 
        /// </summary> 
        public DesignerVisualObject deleteObject; 
         
    } 
    #endregion 
 
 
}