C#WPF在窗口上显示带有AllowTransparency =“true”的WebBrowser不会显示

时间:2013-10-18 07:57:31

标签: c# wpf webbrowser-control

我正在显示一个带有WebBrowser控件的窗口。 我希望窗户是无框架的,所以我设置了WindowStyle =“None” 这工作但是在窗口周围显示彩色边框。 Allowstransparency =“true”删除此项但不再显示WebBrowser(按钮是)

我找到http://www.neowin.net/forum/topic/646970-c%23-wpf-window-with-transparency-makes-windowsformshost-disappear/但是我无法让它工作(SetWindowsLong参数错误)

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>

5 个答案:

答案 0 :(得分:5)

它不能通过WPF技巧工作,因为WebBrowser控件实际上不是WPF控件。 它是一个包装的ActiveX IE Web浏览器控件,不由WPF呈现。

也许您的问题与此帖Removing border from WebBrowser control有关。

老实说,如果可能的话,尝试放弃这个糟糕的WebControl并使用其他东西。 有免费的替代方案,支持WPF,例如Awesomium.NET,CefSharp或CefGlue.NET(均基于Chromium)。

答案 1 :(得分:5)

我知道这是一个老问题,但我今天遇到了完全相同的问题,我用

解决了这个问题
ResizeMode="NoResize"

而不是

Allowstransparency="true"

ResizeMode也会删除annoing边框,并且不会影响WebBrowser控件。在这种情况下,似乎是解决问题的最简单方法:)

答案 2 :(得分:2)

这是一个老问题,但我想发布我所做的工作以使其发挥作用。

如果要创建一个没有可调整大小的边框的窗口,并且能够托管WebBrowser控件或指向URL的框架控件,您根本无法实现,则所述控件的内容将显示为空OP说。

我找到了一个解决方法;在Window中,如果你将WindowStyle设置为None,ResizeMode设置为NoResize(忍受我,你仍然可以在完成后重新调整大小)然后确保你有UNCHECKED AllowTransparency你将有一个没有边框的静态大小的窗口并将显示浏览器控件。

现在,您可能仍希望能够正确调整大小?好吧,我们可以通过互操作来实现:

    [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);
    }

瞧,WPF窗口没有边框,仍可移动和调整大小而不会失去与WebBrowser等控件的兼容性

答案 3 :(得分:0)

我查看了您发布的来源,这对我有用。 下面是守则:

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);
    }
}
}

和Xaml代码:     

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>

答案 4 :(得分:-1)

您可以做的是将 webbrowser 控件包装到一个弹出控件中,该控件具有可以设置为 false 的 AllowTransparency 属性,当然它浮动的缺点是使 webbrowser 控件适合您的目标位置

"miDebuggerPath": "C:\\MinGw\\bin\\gdb.exe"