예제 #1
0
        public Element GetElementByID(string id)
        {
            Interop.IHTMLElement           body     = MSHTMLDocument.GetBody();
            Interop.IHTMLElementCollection children = (Interop.IHTMLElementCollection)body.GetAll();
            Interop.IHTMLElement           element  = (Interop.IHTMLElement)children.Item(id, 0);

            if (element == null)
            {
                return(null);
            }

            return(Element.GetWrapperFor(element, this));
        }
예제 #2
0
        public void SelectAll()
        {
            Interop.IHTMLTxtRange textRange = null;
            // Select the entire body
            Interop.IHtmlBodyElement bodyElement = MSHTMLDocument.GetBody() as Interop.IHtmlBodyElement;
            Debug.Assert(bodyElement != null, "Couldn't get body element in HtmlControl.Find");

            textRange = bodyElement.createTextRange();
            while (textRange.MoveStart("character", -1) > 0)
            {
                ;
            }
            while (textRange.MoveEnd("character", 1) > 0)
            {
                ;
            }
            textRange.Select();
        }
예제 #3
0
        /// <summary>
        /// Finds the specified string in the content of the control and selects it if it exists
        /// </summary>
        /// <param name="searchString">The string to find</param>
        /// <param name="matchCase">Set to true to match casing of the string</param>
        /// <param name="wholeWord">Set to true to only find whole words</param>
        /// <returns></returns>
        public bool Find(string searchString, bool matchCase, bool wholeWord, bool searchUp)
        {
            Interop.IHTMLSelectionObject selectionObj = MSHTMLDocument.GetSelection();

            // Check if a selection actually exists
            bool selectionExists = false;

            if (selectionObj != null)
            {
                selectionExists = selectionObj.GetSelectionType().Equals("Text");
            }
            Interop.IHTMLTxtRange textRange = null;
            if (selectionExists)
            {
                object o = selectionObj.CreateRange();
                textRange = o as Interop.IHTMLTxtRange;
            }
            if (textRange == null)
            {
                // If no selection exists, select the entire body
                Interop.IHtmlBodyElement bodyElement = MSHTMLDocument.GetBody() as Interop.IHtmlBodyElement;
                Debug.Assert(bodyElement != null, "Couldn't get body element in HtmlControl.Find");
                selectionExists = false;
                textRange       = bodyElement.createTextRange();
            }

            // Set up the bounds of the search
            if (searchUp)
            {
                // If we're search up in the document
                if (selectionExists)
                {
                    // If a selection exists, move the range's end to one character before the selection
                    textRange.MoveEnd("character", -1);
                }
                // Move the range's beginning to the start of the document
                int temp = 1;
                while (temp == 1)
                {
                    temp = textRange.MoveStart("textedit", -1);
                }
            }
            else
            {
                // If we're searching down in the document
                if (selectionExists)
                {
                    // If a selection exists, start one char after the selection
                    textRange.MoveStart("character", 1);
                }
                // Move the range's end to the end of the document
                int temp = 1;
                while (temp == 1)
                {
                    temp = textRange.MoveEnd("textedit", 1);
                }
            }

            // Set up the flags for matching case and whole word search
            int flags     = (matchCase ? 0x4 : 0) | (wholeWord ? 0x2 : 0);
            int direction = searchUp ? -10000000 : 10000000;

            //Do the search
            bool success = textRange.FindText(searchString, direction, flags);

            if (success)
            {
                // If we succeeded, select the text, scroll it into view, and we're done!
                textRange.Select();
                textRange.ScrollIntoView(true);
                return(true);
            }
            else if (selectionExists)
            {
                // If we only searched a portion of the document
                // we need to wrap around the document...
                textRange = selectionObj.CreateRange() as Interop.IHTMLTxtRange;
                // Set up the bounds of the search
                if (searchUp)
                {
                    // If we're searching up in the document
                    // Start one char after the selection
                    textRange.MoveStart("character", 1);
                    // Move the range's end to the end of the document
                    int temp = 1;
                    while (temp == 1)
                    {
                        temp = textRange.MoveEnd("textedit", 1);
                    }
                }
                else
                {
                    // If we're searching down in the document
                    // Move the range's end to one character before the selection
                    textRange.MoveEnd("character", -1);
                    // Move the range's beginning to the start of the document
                    int temp = 1;
                    while (temp == 1)
                    {
                        temp = textRange.MoveStart("textedit", -1);
                    }
                }

                success = textRange.FindText(searchString, direction, flags);
                if (success)
                {
                    // If we succeeded, select the text, scroll it into view, and we're done!
                    textRange.Select();
                    textRange.ScrollIntoView(true);
                    return(true);
                }
            }
            return(false);
        }