4
votes

I am displaying a window with a WebBrowser control on it. I want the windows to be frameless so I have set WindowStyle="None" This works BUT displays a colored border around the window. Allowstransparency="true" removes this BUT the WebBrowser is no longer displayed (buttons are)

I have found http://www.neowin.net/forum/topic/646970-c%23-wpf-window-with-transparency-makes-windowsformshost-disappear/ BUT I cannot get it to work (SetWindowsLong Parameter error)

Window x:Class="ZoomBrowserWPF.WebWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:UMenu"
        Title="Test" Height="605" Width="700" ResizeMode="CanResizeWithGrip"
        Loaded="Window_Loaded" Unloaded="Window_Unloaded"
        WindowStyle="None"        
        Background="Transparent"              
        Left="1" Top="1"
        UseLayoutRounding="True" SizeChanged="Window_SizeChanged" >
    <Border Name="WindowBorder"  BorderBrush="Black" BorderThickness="1" CornerRadius="10"     Background="Beige">
    <Grid>        
        <Grid.RowDefinitions>
            <RowDefinition Height="25"/>
            <RowDefinition Height="30"/>
            <RowDefinition/>
            <RowDefinition Height="33"/>
            <RowDefinition Height="25.5"/>
        </Grid.RowDefinitions>
        <Grid x:Name="GridWebBrowser" Grid.Row="2" Grid.RowSpan="2">            
            <WebBrowser x:Name="webBrowser"  Grid.ColumnSpan="2" Visibility="Visible"
                         Margin="0,0,-16,0" 
                        ScrollViewer.HorizontalScrollBarVisibility="Disabled" 
                        ScrollViewer.VerticalScrollBarVisibility="Auto" 
                        ScrollViewer.IsDeferredScrollingEnabled="False"
                        ScrollViewer.CanContentScroll="False"
                        />
        </Grid>
        <Button x:Name="btnZoomIn" Content="Zoom in" Grid.Row="1" Height="23" HorizontalAlignment="Left" Margin="12,0,0,0"  VerticalAlignment="Top" Width="75" Click="btnZoomIn_Click" />
        <Button x:Name="btnZoomOut" Content="Zoom out" Grid.Row="1" Height="23" HorizontalAlignment="Left" Margin="168,0,0,0"  VerticalAlignment="Top" Width="75" Click="btnZoomOut_Click" />
        <TextBlock Grid.Row="1" Height="23" HorizontalAlignment="Left" Margin="102,0,0,0" Name="txtZoom" Text="100" VerticalAlignment="Top" Width="60" />
    </Grid>
    </Border>
</Window>
6

6 Answers

9
votes

I know this is an old question but I had exactly the same problem today and I solved it using

ResizeMode="NoResize"

instead of

Allowstransparency="true"

The ResizeMode does remove the annoing border too and does not impact the WebBrowser control. Seems to be the easiest way to solve your problem in this case :)

5
votes

It doesn't work through WPF trickery, because the WebBrowser control is actually not a WPF control. It is a wrapped ActiveX IE web browser control, not rendered by WPF.

Perhaps your issue is related to this post Removing border from WebBrowser control.

Honestly, if possible, try to abandon this awful WebControl and use something else. There are free alternatives with proper WPF support, such as Awesomium.NET, CefSharp, or CefGlue.NET (all based on Chromium).

3
votes

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:

    [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

0
votes

I looked at the source you posted and it works for me. Heres the Code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Interop;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace PAUL.Allgemein.Seiten
{
/// <summary>
/// Interaktionslogik für Robbe.xaml
/// </summary>
public partial class Robbe : Window
{
    #region The Classic Window API
    //The SendMessage function sends a message to a window or windows.
    [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = false)]
    static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, int wParam, int lParam);

    //ReleaseCapture releases a mouse capture
    [DllImportAttribute("user32.dll", CharSet = CharSet.Auto, SetLastError = false)]
    public static extern bool ReleaseCapture();

    //SetWindowLong lets you set a window style
    [System.Runtime.InteropServices.DllImport("user32.dll")]
    static extern int SetWindowLong(IntPtr hWnd, int nIndex, long dwNewLong);
    #endregion

    const int GWL_STYLE = -16;
    const long WS_POPUP = 2147483648;

    //private const int GWL_STYLE = -16;
    //private const int WS_SYSMENU = 0x80000;
    //[DllImport("user32.dll", SetLastError = true)]
    //private static extern int GetWindowLong(IntPtr hWnd, int nIndex);
    //[DllImport("user32.dll")]
    //private static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);
    public Robbe()
    {

        InitializeComponent();
    }

    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        IntPtr hWnd = new System.Windows.Interop.WindowInteropHelper(this).Handle;
        SetWindowLong(hWnd, GWL_STYLE, WS_POPUP);
    }
}
}

and the Xaml code:

xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Robbe" Height="300" Width="300"
    Loaded="Window_Loaded">
<Grid>
    <!-- Creates the shadow on the right and bottom -->
    <Border Focusable="False" BorderBrush="Gray"           
        BorderThickness="0,0,2,2"
        CornerRadius="10"
        Background="Beige" >
        <WebBrowser Source="C:\Users\nicholas\Desktop\puale\PAUL\bin\Debug\robbe.swf" Margin="62,71,69,56"></WebBrowser>
    </Border>
</Grid>

0
votes

I reached this question when I face similar issue with SystemWebBrowser control, I needed to remove the top white space as well as keep ResizeMode as CanResize. So, I had to search a little more to find the solution. The solution is to play with WindowChrome and set CaptionHeight to 0 as below,

<WindowChrome.WindowChrome>
<WindowChrome 
    CaptionHeight="0"
    ResizeBorderThickness="5" />
</WindowChrome.WindowChrome>

With this you can keep other properties like,

    ResizeMode="CanResize"
    WindowStyle="None"

And leave default value of AllowsTransparency

-1
votes

What you can do is to to wrap the webbrowser control to a popup control which has AllowTransparency attribute that can be set to false, ofcourse the downside it is floating is to make the webbrowser control fit to your targetplacement

 <Grid Grid.Row="1"
       Grid.Column="2">
       <Grid Name="grdBrowser" 
             Background="White"></Grid>
                <Popup AllowsTransparency="False" 
                       Height="{Binding Path=Height,ElementName=grdBrowser}"
                       Width="{Binding Path=Width,ElementName=grdBrowser}"
                       IsOpen="True"
                       Placement="Mouse" 
                       PlacementTarget="{Binding ElementName=grdBrowser}">
                            <WebBrowser/>
                </Popup>

 </Grid>