Beispiel #1
0
 /// <summary>
 /// ctor
 /// </summary>
 /// <param name="ctrl"></param>
 /// <param name="host"></param>
 public BaseControlWarpper(IRuntimeDesignControl ctrl, IDesignerHost host)
     : base(host)
 {
     if (ctrl == null)
     {
         this.WrappedObject = null;
     }
     else
     {
         this.WrappedObject = ctrl;
         ICpNode            = ctrl as ICpNode;
         IFormCtrl          = ctrl as IFormCtrl;
         IChkBox            = ctrl as ICheckBox;
         ICbx      = ctrl as ICombox;
         ILine     = ctrl as ICtlLine;
         IPic      = ctrl as IPictureBox;
         IPnl      = ctrl as IPanel;
         ITabCtrl  = ctrl as ITabControl;
         ISign     = ctrl as ISignatureControl;
         IRtf      = ctrl as IRtfEditor;
         IDateTime = ctrl as IXtraDateTime;
         if (ICbx != null)
         {
             ICbx.Items.CollectionChanged -= new CollectionChangeEventHandler(Items_CollectionChanged);
             ICbx.Items.CollectionChanged += new CollectionChangeEventHandler(Items_CollectionChanged);
         }
         if (ITabCtrl != null)
         {
             ITabCtrl.TabPages.CollectionChanged -= new CollectionChangeEventHandler(TabPages_CollectionChanged);
             ITabCtrl.TabPages.CollectionChanged += new CollectionChangeEventHandler(TabPages_CollectionChanged);
         }
     }
 }
        /// <summary>
        /// 递归创建控件(单个)
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        private object CreateControl(EntityCPNode entityParent, int presentationMode)
        {
            try
            {
                if (entityParent.ControlName == typeof(DesignPanel).Name + "1")
                {
                    return(null);
                }

                Type   type     = Type.GetType(entityParent.ControlType);
                object instance = null;
                if (type != null)
                {
                    instance = Activator.CreateInstance(type);

                    if (instance != null && instance is Control)
                    {
                        Control ctrlParent = instance as Control;

                        ctrlParent.Name      = entityParent.ControlName;
                        ctrlParent.Text      = entityParent.NodeDesc;
                        ctrlParent.ForeColor = entityParent.ForeColor;
                        ctrlParent.BringToFront();

                        if (ctrlParent is IRuntimeDesignControl)
                        {
                            try
                            {
                                IRuntimeDesignControl ictrl = ctrlParent as IRuntimeDesignControl;

                                this._createdControls.Add(ictrl);

                                if (ictrl is ICpNode)
                                {
                                    ICpNode iNode = ictrl as ICpNode;

                                    iNode.NodeName       = entityParent.NodeName;
                                    iNode.NodeType       = entityParent.NodeType;
                                    iNode.NodeDays       = entityParent.NodeDays;
                                    iNode.ParentNodeName = entityParent.ParentNodeName;
                                }
                            }
                            catch (Exception ex)
                            {
                                throw ex;
                            }
                        }

                        //查找当前控件的子控件
                        //var query = from item in this._controlsdata
                        //            where item.Parent == ctrlParent.Name
                        //            select item;

                        foreach (var itemChild in this._controlsdata)//query)
                        {
                            object  obj       = CreateControl(itemChild, presentationMode);
                            Control ctrlChild = obj as Control;
                            if (ctrlChild != null)
                            {
                                ctrlParent.Controls.Add(ctrlChild);

                                if (ctrlChild is IRuntimeDesignControl)
                                {
                                    IRuntimeDesignControl ICtrl = ctrlChild as IRuntimeDesignControl;
                                    ICtrl.Location = new System.Drawing.Point((int)itemChild.Left, (int)itemChild.Top);
                                    ICtrl.Width    = (int)itemChild.Width;
                                    ICtrl.Height   = (int)itemChild.Height;
                                }
                                else
                                {
                                    ctrlChild.Location = new System.Drawing.Point((int)itemChild.Left, (int)itemChild.Top);
                                    ctrlChild.Width    = (int)itemChild.Width;
                                    ctrlChild.Height   = (int)itemChild.Height;
                                }
                            }
                        }
                    }
                }
                return(instance);
            }
            catch (Exception ex)
            {
                throw;
            }
        }
        /// <summary>
        /// (递归)序列化单个控件
        /// </summary>
        /// <param name="nametable"></param>
        /// <param name="parentSiteName"></param>
        /// <param name="level"></param>
        /// <param name="value"></param>
        /// <param name="serializedData"></param>
        private void SerializeControl(Hashtable nametable, string parentSiteName, object value, List <EntityCPNode> serializedData, bool recursive)
        {
            try
            {
                IComponent   component = value as IComponent;
                EntityCPNode entity    = new EntityCPNode();

                entity.ControlType = value.GetType().AssemblyQualifiedName;

                if (component != null && component.Site != null && component.Site.Name != null)
                {
                    entity.ControlName = component.Site.Name;
                    //entity.Parent = parentSiteName;
                    nametable[value] = component.Site.Name;
                }

                bool    isControl = (value is Control);
                Control ctrl      = value as Control;

                if (isControl)
                {
                    if (ctrl is IRuntimeDesignControl)
                    {
                        IRuntimeDesignControl ictrl = ctrl as IRuntimeDesignControl;

                        entity.Height    = (int)ictrl.Height;
                        entity.Width     = (int)ictrl.Width;
                        entity.Top       = (int)ictrl.Location.Y;
                        entity.Left      = (int)ictrl.Location.X;
                        entity.NodeDesc  = ictrl.Text;
                        entity.ForeColor = ictrl.ForeColor;
                        //序列化字体
                        if (ictrl.TextFont != null)
                        {
                            entity.TextFont = FontSerializationService.Serialize(ictrl.TextFont);
                        }
                    }
                    else
                    {
                        entity.Height = (int)ctrl.Height;
                        entity.Width  = (int)ctrl.Width;
                        entity.Top    = (int)ctrl.Top;
                        entity.Left   = (int)ctrl.Left;
                    }

                    if (value is ICpNode)
                    {
                        ICpNode iNode = value as ICpNode;

                        entity.NodeName       = iNode.NodeName;
                        entity.NodeType       = iNode.NodeType;
                        entity.NodeDays       = iNode.NodeDays;
                        entity.ParentNodeName = iNode.ParentNodeName;
                    }
                }

                if (component != null && isControl)
                {
                    if (recursive)
                    {
                        foreach (Control child in ((Control)value).Controls)
                        {
                            if (child.Site != null && child.Site.Container == designerHost.Container)
                            {
                                if (!nametable.ContainsKey(child))
                                {
                                    SerializeControl(nametable, component.Site.Name, child, serializedData, recursive);
                                }
                            }
                        }
                    }
                }

                serializedData.Add(entity);
            }
            catch (Exception ex)
            {
                throw;
            }
        }
        /// <summary>
        /// (递归)反序列化单个控件
        /// 反序列化后如果当前控件名字已存在则重新命名当前控件:如果当前控件为子控件,则把序列化数据中的控件的父控件名字改为新的控件名字
        /// </summary>
        /// <param name="entity"></param>
        /// <param name="listAll"></param>
        /// <returns></returns>
        private IComponent DeserializeControl(EntityCPNode entity, List <EntityCPNode> listAll)
        {
            try
            {
                if (entity.ControlName.Contains(typeof(DesignPanel).Name))
                {
                    return(null);
                }

                Type type = Type.GetType(entity.ControlType);

                object instance = null;
                if (type != null)
                {
                    if (typeof(IComponent).IsAssignableFrom(type))
                    {
                        instance = designerHost.CreateComponent(type, CreateControlName(entity.ControlName, designerHost as IContainer, Type.GetType(entity.ControlType), listAll));
                    }
                    else
                    {
                        instance = Activator.CreateInstance(type);
                    }

                    if (instance != null && instance is Control)
                    {
                        Control ctrlParent = instance as Control;

                        if (string.IsNullOrEmpty(ctrlParent.Name))
                        {
                            ctrlParent.Name = CreateControlName(entity.ControlName, designerHost as IContainer, Type.GetType(entity.ControlType), listAll);
                        }

                        if (ctrlParent is ICpNode)
                        {
                            ICpNode iNode = ctrlParent as ICpNode;
                            iNode.NodeName       = entity.NodeName;
                            iNode.NodeType       = entity.NodeType;
                            iNode.NodeDays       = entity.NodeDays;
                            iNode.ParentNodeName = entity.ParentNodeName;
                        }

                        if (ctrlParent is IRuntimeDesignControl)
                        {
                            IRuntimeDesignControl ictrl = ctrlParent as IRuntimeDesignControl;
                            ictrl.Width     = (int)entity.Width;
                            ictrl.Height    = (int)entity.Height;
                            ictrl.Location  = new System.Drawing.Point((int)entity.Left, (int)entity.Top);
                            ictrl.Text      = entity.NodeDesc;
                            ictrl.ForeColor = entity.ForeColor;
                            if (!string.IsNullOrEmpty(entity.TextFont))
                            {
                                ictrl.TextFont  = FontSerializationService.Deserialize(entity.TextFont);
                                ctrlParent.Font = FontSerializationService.Deserialize(entity.TextFont);
                            }
                        }
                        else
                        {
                            ctrlParent.Width    = (int)entity.Width;
                            ctrlParent.Height   = (int)entity.Height;
                            ctrlParent.Location = new System.Drawing.Point((int)entity.Left, (int)entity.Top);
                        }

                        //var query = from item in listAll
                        //            where item.Parent == ctrlParent.Name
                        //            select item;

                        //foreach (var item in query)
                        //{
                        //    IComponent objChild = DeserializeControl(item, listAll);
                        //    if (objChild is Control)
                        //    {
                        //        if (objChild is DevExpress.XtraTab.XtraTabPage)
                        //        {
                        //            (ctrlParent as ITabControl).TabPages.Add(objChild as DevExpress.XtraTab.XtraTabPage);
                        //        }
                        //        else
                        //        {
                        //            ctrlParent.Controls.Add(objChild as Control);
                        //        }
                        //    }
                        //}
                    }
                }
                return(instance as IComponent);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }