Saif Khan
Saif Khan

Reputation: 18782

.NET Firebug like UI assistance

I am tasked with a project whereby I need to create a scaled down version of a Firebug like UI where the user can load an HTML page, and as they hover the mouse over the elements they'll be highlighted. The app will allow the users to select a table to be screen-scraped....haven't got to that part as yet.

Any advice?

Thanks

Upvotes: 0

Views: 309

Answers (1)

ZeroBugBounce
ZeroBugBounce

Reputation: 3670

Well, I haven't used the Firebug UI, but I have done exactly what you describe using the .NET 2.0 WebBrowser control in a WinForms app.

Basically I added the WebBrowser and a Timer control to the form then in the timer elapsed event, I query the mouse position using the GetCursorPos native function and use the WebBrowser.Document's (HtmlDocument class) GetElementFromPoint method (adjusting the x and y position to be relative to the browser control).

This returns whatever HtmlElement is under the mouse position. Here's the meat of the method:

HtmlElement GetCurrentElement()
{
    if (Browser.ReadyState == WebBrowserReadyState.Complete && Browser.Document != null)
    {
        Win32Point mouseLoc = HtmlScan.Win32.Mouse.GetPosition();
        Point mouseLocation = new Point(mouseLoc.x, mouseLoc.y);
        // modify location to match offset of browser window and control position:
        mouseLocation.X = ((mouseLocation.X - 4) - this.Left) - Browser.Left;
        mouseLocation.Y = ((mouseLocation.Y - 31) - this.Top) - Browser.Top;

        HtmlElement element = Browser.Document.GetElementFromPoint(mouseLocation);

        return element;
    }

    return null;
}

After you get the HtmlElement, you can get the InnerHTML to parse as you see fit.

Richard

Upvotes: 3

Related Questions