Ask a questionAsk a question
 

AnswerHwndHost inside ToolTip

  • Tuesday, August 19, 2008 1:39 AMDikhi Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Hi all,

    have anyone successfully put an HwndHost inside ToolTip?
    I am trying to put the WebBrowser control (which is an HwndHost) inside the ToolTip,
    but nothing appear. Could it because they fight over the airspace, or because of the clipping?

    Is there a way to fix that problem?

    Many thanks,
    Dikhi

Answers

  • Monday, August 25, 2008 10:15 AMMarco Zhou Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    The following code shows how to host WebBrowser inside ToolTip:

    <Window x:Class="WpfTestHarness.WebBrowserInToolTip"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:cc="clr-namespace:WpfTestHarness"
        Title="WebBrowserInToolTip" Height="300" Width="300">
      <Button >
        <Button.ToolTip>
          <cc:MyToolTip StaysOpen="True" x:Name="toolTip">
            <WebBrowser Width="300" Height="200" Source="http://www.live.com" />
          </cc:MyToolTip>
        </Button.ToolTip>
      </Button>
    </Window>

     public class MyToolTip : ToolTip
    {
        protected override void OnTemplateChanged(ControlTemplate oldTemplate, ControlTemplate newTemplate)
        {
            if (newTemplate != null)
            {
                this.Visibility = Visibility.Collapsed;
                this.IsOpen = true;
                Popup popup = GetPopupFromVisualChild(this);
                if (popup != null) popup.AllowsTransparency = false;
                this.IsOpen = false;
                this.Visibility = Visibility.Visible;
            }
        }

        private static Popup GetPopupFromVisualChild(Visual child)
        {
            Visual parent = child;
            FrameworkElement visualRoot = null;
            while (parent != null)
            {
                visualRoot = parent as FrameworkElement;
                parent = VisualTreeHelper.GetParent(parent) as Visual;
            }

            Popup popup = null;
            if (visualRoot != null)
            {
                popup = visualRoot.Parent as Popup;
            }

            return popup;
        }
    }

    Hope this helps
    • Marked As Answer byMarco Zhou Monday, August 25, 2008 10:16 AM
    •  

All Replies

  • Tuesday, August 19, 2008 9:31 AMTim DawsonMVPUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Let me get this straight. You're trying to host an entire instance of the Internet Explorer browser... inside a tooltip?
    Controls for WPF and Windows Forms at http://www.divelements.co.uk
  • Thursday, August 21, 2008 5:52 AMDikhi Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    Well that's not entirely true.
    I am trying to display a Chm helpfile content inside a tooltip.

    But, there's no easy way to grab just the content.
    And the only feasible way to do that is by using the web browser. (any suggestion is welcomed)

    Now, to fulfill what I need,
    I am trying to host the WebBrowser control which is an HwndHost inside the tooltip,
    but the control is not appearing.

    Thanks,
    dikhi.

  • Monday, August 25, 2008 10:15 AMMarco Zhou Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    The following code shows how to host WebBrowser inside ToolTip:

    <Window x:Class="WpfTestHarness.WebBrowserInToolTip"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:cc="clr-namespace:WpfTestHarness"
        Title="WebBrowserInToolTip" Height="300" Width="300">
      <Button >
        <Button.ToolTip>
          <cc:MyToolTip StaysOpen="True" x:Name="toolTip">
            <WebBrowser Width="300" Height="200" Source="http://www.live.com" />
          </cc:MyToolTip>
        </Button.ToolTip>
      </Button>
    </Window>

     public class MyToolTip : ToolTip
    {
        protected override void OnTemplateChanged(ControlTemplate oldTemplate, ControlTemplate newTemplate)
        {
            if (newTemplate != null)
            {
                this.Visibility = Visibility.Collapsed;
                this.IsOpen = true;
                Popup popup = GetPopupFromVisualChild(this);
                if (popup != null) popup.AllowsTransparency = false;
                this.IsOpen = false;
                this.Visibility = Visibility.Visible;
            }
        }

        private static Popup GetPopupFromVisualChild(Visual child)
        {
            Visual parent = child;
            FrameworkElement visualRoot = null;
            while (parent != null)
            {
                visualRoot = parent as FrameworkElement;
                parent = VisualTreeHelper.GetParent(parent) as Visual;
            }

            Popup popup = null;
            if (visualRoot != null)
            {
                popup = visualRoot.Parent as Popup;
            }

            return popup;
        }
    }

    Hope this helps
    • Marked As Answer byMarco Zhou Monday, August 25, 2008 10:16 AM
    •  
  • Tuesday, August 26, 2008 2:33 AMDikhi Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    Hi Marco,

    Thanks for the thorough example.. That's sound promising.
    I'll have a play with it now.

     

    cheers,

    Dikhi

  • Friday, October 10, 2008 9:30 AMDikhi Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    It's working, Zhou..
    thanks.

    Dikhi
  • Wednesday, March 25, 2009 10:26 AMPyroa Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Hello,

    This piece of code it's very fine ! I'ts working thanks you !

    But i'have an other question :

    I've tried to put the background of the tooltip to transparent, but i'ts impossible with this code.
    I've also tried to modify the code by putting the part of code " if (popup != null) popup.AllowsTransparency = false;" in " if (popup != null) popup.AllowsTransparency = true;" the background it is really transparent but the webbrowser don"t appears.

    Have you a solution for this ?
  • Friday, June 12, 2009 4:07 PMMichael L. Long Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    Pyroa not sure if you wound up finding the answer to this but incase not (or others haven't that reach this page) it appears to be a bug with the WebBrowser control and MS doesn't appear to have plans to address the transparency issue with the control. This is not necessarily a shock since the control isn't pure WPF and it would take a lot of munge and slop to fake it/make it happen. Maybe there will be a pure WPF WebBrowser soon? That would be great.

    Got this insight while investigating other issues not related to it.

    From Comment #5 @ http://rhizohm.net/irhetoric/blog/72/default.aspx

    "
    re: WebBrowser disappears when allowtransparency=true: we've gone thru our contacts at MS and "there are no firm plans to fix this, as deeply depends on complex legacy restrictions in technology". It has to do with the way wpf renders windows vs the underlying webbrowser technology that uses win32 painting.
    Comment by Glenn - November 14, 2008 @ 11:59 AM
    "

  • Tuesday, August 18, 2009 10:04 PMBob Eaton Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    I'm not using WPF, but in trying to do the exact same thing (HTML display in a tooltip), i've included a WebBrowser on a minimal Windows Form. If I set the transparancy of the Form to 70%, for example, it does show thru even the WebBrowser.