public bool SelectElements(ICollection elements)
        {
            Interop.IHTMLElement       body      = _editor.MSHTMLDocument.GetBody();
            Interop.IHTMLTextContainer container = body as Interop.IHTMLTextContainer;
            Debug.Assert(container != null);
            object controlRange = container.createControlRange();

            Interop.IHtmlControlRange htmlControlRange = controlRange as Interop.IHtmlControlRange;
            Debug.Assert(htmlControlRange != null);
            if (htmlControlRange == null)
            {
                return(false);
            }

            Interop.IHtmlControlRange2 htmlControlRange2 = controlRange as Interop.IHtmlControlRange2;
            Debug.Assert(htmlControlRange2 != null);
            if (htmlControlRange2 == null)
            {
                return(false);
            }


            int hr = 0;

            foreach (object o in elements)
            {
                Interop.IHTMLElement element = GetIHtmlElement(o);
                if (element == null)
                {
                    return(false);
                }
                hr = htmlControlRange2.addElement(element);
                if (hr != Interop.S_OK)
                {
                    break;
                }
            }
            if (hr == Interop.S_OK)
            {
                //If it succeeded, simply select the control range
                htmlControlRange.Select();
            }
            else
            {
                // elements like DIV and SPAN, w/o layout, cannot be added to a control selelction.
                Interop.IHtmlBodyElement bodyElement = (Interop.IHtmlBodyElement)body;
                Interop.IHTMLTxtRange    textRange   = bodyElement.createTextRange();
                if (textRange != null)
                {
                    foreach (object o in elements)
                    {
                        try
                        {
                            Interop.IHTMLElement element = GetIHtmlElement(o);
                            if (element == null)
                            {
                                return(false);
                            }
                            textRange.MoveToElementText(element);
                        }
                        catch
                        {
                        }
                    }
                    textRange.Select();
                }
            }
            return(true);
        }
Beispiel #2
0
 public void MoveToElementText(IElement element)
 {
     Interop.IHTMLElement pElement = element.GetBaseElement();
     tr.MoveToElementText(pElement);
 }