HtmlEditor經常使用模式

一、撤銷重作模式html

基礎封裝:cookie

  #region 重作單元

        /// <summary>
        /// 表示開始進行重作單元
        /// </summary>
        /// <param name="title"></param>
        private void StartUndoUnit(string title = "coolform")
        {
            var ims = htmlDesignEditor.Document as IMarkupServices2;
            ims.BeginUndoUnit(title);
        }

        /// <summary>
        /// 表示結束進行重作單元
        /// </summary>
        private void EndUndoUnit()
        {
            var ims = htmlDesignEditor.Document as IMarkupServices2;
            ims.EndUndoUnit();
           
        }

        #endregion

封裝好後,在經過代碼來改變HtmlEditor內的內容時,須要把更改的操做包含在起來,如:ui

           StartUndoUnit();
                foreach (var tempSelectCell in cells)
                {
                    var element = tempSelectCell.Value;

                    IHTMLStyle style = element.style;

                    style.SetTextAlign("center");


                }
                EndUndoUnit();    

只有把操做代碼給StartUndoUnit()跟EndUndoUnit()包含起來後,HtmlEditor纔會把裏面的多個操做看成是一個工做單元,當你使用Undo()撤銷時,纔會一次性進行撤銷操做。this

 

二、鼠標區域限制spa

當進行拖拉跳轉單元格等操做時,咱們須要鼠標拖拉的範圍進行調整,即限制鼠標的可活動範圍:設計

    #region "鼠標區域限制"

        /// <summary>
        /// 移除鼠標區域限制
        /// </summary>
        /// <remarks></remarks>
        private void RemoveMouseRect()
        {
            Cursor.Clip = Rectangle.Empty;
        }

        /// <summary>
        /// 設置鼠標區域限制,此區域大小由傳入的控件參數決定
        /// </summary>
        /// <param name="el"></param>
        /// <remarks></remarks>
        private void SetMouseRect(IHTMLElement el)
        {
            Size s = new Size(el.offsetWidth, el.offsetHeight);
            Point p = _coolPf.GetAbsoluteLocation(el);

            SetMouseRect(p, s);
        }

        /// <summary>
        /// 設置鼠標區域限制
        /// </summary>
        /// <param name="p">The p.</param>
        /// <param name="s">The s.</param>
        private void SetMouseRect(Point p, Size s)
        {
            Rectangle r = this.htmlDesignEditor.RectangleToScreen(this.htmlDesignEditor.ClientRectangle);
            p.X += r.Left;
            p.Y += r.Top;
            IHTMLElement2 body = this.htmlDesignEditor.HtmlDocument2.GetBody() as IHTMLElement2;
            p.X -= body.GetScrollLeft();
            p.Y -= body.GetScrollTop();

            //檢測最左邊緣
            if (p.X < 0)
            {
                s.Width += p.X;
                p.X = 0;
            }

            //檢測最上邊緣
            if (p.Y < r.Top)
            {
                s.Height = s.Height + p.Y - r.Top;
                p.Y = r.Top;
            }

            //檢測可見區域
            int height = r.Height;
            int width = r.Width;
            if (r.Left < 0)
            {
                width = r.Width + r.Left;
            }
            if (r.Top < 0)
            {
                height = r.Height + r.Top;
            }

            //設置可見區域
            if (width < s.Width)
            {
                s.Width = width;
            }
            if (height < s.Height)
            {
                s.Height = height - 20;
            }
            if (p.X + s.Width > r.Left + r.Width)
            {
                s.Width -= 6;
            }

            var newRect = new Rectangle(p, s);
            Cursor.Clip = newRect;
        }

        #endregion

eg:如選擇單元格時,須要限制鼠標在當前表格範圍內:code

              //開始選擇單元格
                //保存相關的信息
                //設置鼠標限制區域
                //改變鼠標形狀
                if (_selectTableCellHelper.StartSelectCell(el))
                {
                    Cursor.Current = new Cursor("CellSelect.cur");
                    SetMouseRect(_selectTableCellHelper.MouseDownTable);
                    return;
                }

選擇完成後,須要清空對鼠標的區域限制:orm

   if (_selectTableCellHelper.State == SelectCellState.Doing)
            {
                _selectTableCellHelper.State = SelectCellState.Finish;
                RemoveMouseRect();
                Cursor.Current = Cursors.Default;
            }

 

三、對控件加入自定義的行爲展現htm

在進行控件設計時,咱們分別定義了不少控件,如文本框,時間日期框等等,但文本框,時間日期框本質上都是input控件,爲了在設計時能區別不一樣的控件,咱們對某些控件加入特殊的行爲展現,如對時間日期控件加入日期小標誌,以下圖。blog

 

如何加入自定義行爲,請參見:IElementBehaviorFactory, IElementBehavior, IHTMLPainter三個接口。

eg:

 public class DateTimeBehavior : IElementBehaviorFactory, IElementBehavior, IHTMLPainter
    {
        protected IHTMLDocument2 Document
        {
            get
            {
                return (IHTMLDocument2)this.Element.document;
            }
        }
        protected IHTMLElement Element
        {
            get
            {
                return _behaviorSite.GetElement();
            }
        }
        protected IHTMLWindow2 Window
        {
            get
            {
                return this.Document.GetParentWindow();
            }
        }
        private IElementBehaviorSite _behaviorSite;

        public IElementBehaviorSite ElementSite
        {
            get
            {
                return _behaviorSite;
            }
        }
        public IHTMLPaintSite PaintSite
        {
            get
            {
                return (IHTMLPaintSite)_behaviorSite;
            }
        }



        public void Update()
        {
            PaintSite.InvalidateRect(IntPtr.Zero);
        }


        #region IElementBehaviorFactory 成員

        public IElementBehavior FindBehavior(string bstrBehavior, string bstrBehaviorUrl, IElementBehaviorSite pSite)
        {
            return this;
        }

        #endregion

        #region IElementBehavior 成員

        public void Init(IElementBehaviorSite pBehaviorSite)
        {
            _behaviorSite = pBehaviorSite;

        }

        public void Notify(int dwEvent, IntPtr pVar)
        {
            if (dwEvent == 1)
            {
                //IHTMLEventObj eventobj = this.Element as IHTMLEventObj;

                //mshtml.IHTMLElement el = ElementSite.GetElement() as mshtml.IHTMLElement;

                //mshtml.IHTMLImgElement imgel =( mshtml.IHTMLImgElement)el ;

                //mshtml.HTMLImgEvents2_Event imageEvent = imgel  as mshtml.HTMLImgEvents2_Event;
                //imageEvent.onmousemove += new mshtml.HTMLImgEvents2_onmousemoveEventHandler(imageEvent_onmousemove);

                //imageEvent.onmousedown += new mshtml.HTMLImgEvents2_onmousedownEventHandler(imageEvent_onmousedown);
                //imageEvent.onmouseup += new mshtml.HTMLImgEvents2_onmouseupEventHandler(imageEvent_onmouseup);
            }

        }

        void imageEvent_onmousemove(mshtml.IHTMLEventObj pEvtObj)
        {
            mshtml.IHTMLElement el = pEvtObj.srcElement;
            int ex = el.offsetWidth - 20;
            if (pEvtObj.offsetX >= ex && pEvtObj.offsetX <= el.offsetWidth && pEvtObj.offsetY <= 20)
            {
                _buttonPushed = true;
                Update();
            }
            else
            {
                _buttonPushed = false;
                Update();
            }
            // System .Diagnostics .Debug .WriteLine ("move:"+_buttonPushed .ToString ());
        }

        public void Detach()
        {
            //OnBehaviorDetach();
            _behaviorSite = null;
        }

        #endregion

        #region IHTMLPainter 成員


        public void Draw(int leftBounds, int topBounds, int rightBounds, int bottomBounds, int leftUpdate, int topUpdate, int rightUpdate, int bottomUpdate, int lDrawFlags, IntPtr hdc, IntPtr pvDrawObject)
        {
            //mshtml.IHTMLElement el = ElementSite.GetElement() as mshtml.IHTMLElement;
            //mshtml.IHTMLTable3 table = el as mshtml.IHTMLTable;
            try
            {
                Graphics g = Graphics.FromHdc(hdc);
                //Rectangle bounds = new Rectangle(leftBounds, topBounds, rightBounds - leftBounds, bottomBounds - topBounds);
                //Rectangle updateRect = new Rectangle(leftUpdate, topUpdate, rightUpdate - leftUpdate, bottomUpdate - topUpdate);

                //g.DrawRectangle(new Pen(SystemColors.Control), rightBounds - 20, topBounds, 20, 20);
                //g.FillRectangle(new SolidBrush(SystemColors.Control), rightBounds - 20, topBounds, 20, 20);

                //g.FillEllipse(new SolidBrush(Color.Black), rightBounds - 15, topBounds + 5, 10, 10);
                g.PageUnit = GraphicsUnit.Pixel;

                g.DrawImage(Image.FromFile("datePicker.gif"),  leftBounds  +20, topBounds);
                g.Dispose();

            }
            catch
            {

            }

        }

        public void OnResize(int cx, int cy)
        {
            Size size = new Size(cx, cy);

        }
        private bool _buttonPushed = false;
        public void GetPainterInfo(HTML_PAINTER_INFO htmlPainterInfo)
        {
            //htmlPainterInfo = new HTML_PAINTER_INFO();
            //OnGetPainterInfo(ref htmlPainterInfo);
            htmlPainterInfo.lFlags = 0x000002;
            htmlPainterInfo.lZOrder = 8;
            htmlPainterInfo.iidDrawObject = Guid.Empty;
            htmlPainterInfo.rcBounds = new RECT(20, 0, 0, 20);
            //htmlPainterInfo = new HTML_PAINTER_INFO();
        }


        private const int ButtonPart = 1009;
        #endregion

        private void imageEvent_onmousedown(mshtml.IHTMLEventObj pEvtObj)
        {
            mshtml.IHTMLElement el = pEvtObj.srcElement;
            int ex = el.offsetWidth - 20;
            if (pEvtObj.offsetX >= ex && pEvtObj.offsetX <= el.offsetWidth && pEvtObj.offsetY <= 20)
            //if (((mshtml.IHTMLEventObj3)pEvtObj).behaviorPart == ButtonPart)
            {


                _buttonPushed = true;
                Update();
            }
        }

        private void imageEvent_onmouseup(mshtml.IHTMLEventObj pEvtObj)
        {

            if (_buttonPushed)
            {
                _buttonPushed = false;
                Update();
                if (((mshtml.IHTMLEventObj3)pEvtObj).behaviorPart == ButtonPart)
                {
                    //System.Windows.Forms.MessageBox.Show("ASDdsdsddssdsdsddsFASFA");
                    OnClick();
                }
            }
        }

        private void OnClick()
        {
            //throw new Exception("The method or operation is not implemented.");
        }

        #region IHTMLPainter 成員


        public void HitTestPoint(int ptx, int pty, out int pbHit, out int plPartID)
        {
            Point pt = new Point(ptx, pty);
            plPartID = 0;
            pbHit = 0;
            if (pt.X <= 20 && pt.Y <= 20) plPartID = ButtonPart;
            if (plPartID > 0)
            {
                pbHit = 1;
            }

        }

        #endregion
    }

使用:

添加:

            var grid = new  DateTimeBehavior();
             object obj = grid;
             IHTMLElement2 ele2 = input as IHTMLElement2;
             dynamic cookieid = ele2.AddBehavior(null,ref obj);    

刪除:

               IHTMLElement2 ele = element as IHTMLElement2;
                int behaviorid = _toolTipBehaviorCookiedIDList[uniqueId];
                ele.RemoveBehavior(behaviorid);

*請注意,刪除Behavior須要根據添加Behavior到控件時返回的id才能刪除,故添加時須要記錄保存ID

相關文章
相關標籤/搜索