Click here to Skip to main content
15,890,438 members
Please Sign up or sign in to vote.
4.00/5 (3 votes)
See more:
I put a WebBrowswer Control on a window, but if I set AllowsTransparent of the window to True, the web browser won't show anything (HTMl).

This is my XAML code

XML
<Window x:Class="WpfApplication2.MainWindow"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      Title="MainWindow" Height="350" Width="525" AllowsTransparency="True  WindowStyle="None">
   <Grid>
      <WebBrowser Height="234" HorizontalAlignment="Left" Margin="10,10,0,0" Name="WebBrowser" VerticalAlignment="Top" Width="373" />
   </Grid>
</Window>



Can you help me?
Posted
Updated 21-Feb-16 18:22pm
v4

One more variant. Try this one or wait for WPF 4.5 (details here).
 
Share this answer
 
Comments
Shahin Khorshidnia 27-Mar-12 18:00pm    
Good Link
Thank you Michael.
And: "wait for WPF 4.5", I like the answer :D
I am not surprised, there is no way a web browser control ( which is just IE ) is going to support transparency. So, turn transparency off for the browser control, it's probably your only option.

Or[^] you could try this.
 
Share this answer
 
v2
Comments
Shahin Khorshidnia 28-Jun-11 1:26am    
Thank you for the quick answer but:
how can I turn off transparency for WebBrowser control?
I didn't see any property.
In fact, the background property of this control is white now (Not transparent) but It still has this problem.
Christian Graus 28-Jun-11 1:40am    
Did you read the article I linked to ? If you can't specify transparency at the control level ( I thought you could ), then having a non transparent form, or doing the horrible hack explained in the article I linked to, are your solutions.
Shahin Khorshidnia 29-Jun-11 1:11am    
The link was not an article.
Sorry didn't help.
This is an old question but I wanted to post what I have done to get it working.

When you want to create a window with no border that is resizeable and is able to host a WebBrowser control or a Frame control pointed to a URL you simply couldn't, the contents of said control would show empty as the OP said.

I found a workaround though; in the Window, if you set the WindowStyle to None, ResizeMode to NoResize (bear with me, you will still be able to resize once done) then make sure you have UNCHECKED AllowsTransparency you will have a static sized window with no border and will show the browser control.

Now, you probably still want to be able to resize right? Well we can to that with a interop call:
C#
[DllImport("user32.dll", CharSet = CharSet.Auto)]
private static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);

[DllImportAttribute("user32.dll")]
public static extern bool ReleaseCapture();

//Attach this to the MouseDown event of your drag control to move the window in place of the title bar
private void WindowDrag(object sender, MouseButtonEventArgs e) // MouseDown
{
    ReleaseCapture();
    SendMessage(new WindowInteropHelper(this).Handle,
        0xA1, (IntPtr)0x2, (IntPtr)0);
}

//Attach this to the PreviewMousLeftButtonDown event of the grip control in the lower right corner of the form to resize the window
private void WindowResize(object sender, MouseButtonEventArgs e) //PreviewMousLeftButtonDown
{
    HwndSource hwndSource = PresentationSource.FromVisual((Visual)sender) as HwndSource;
    SendMessage(hwndSource.Handle, 0x112, (IntPtr)61448, IntPtr.Zero);
}

And voila, A WPF window with no border and still movable and resizable without losing compatibility with with controls like WebBrowser
 
Share this answer
 
Comments
mikiqex 9-Nov-14 3:54am    
Your solution works, thanks, but during resizing the WebBrowser terribly flickers - Win7, .NET 4.x (tried different)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900