C# WPF在窗口上显示WebBrowser且AllowTransparency="true"时无法正常显示。

5
我正在显示一个带有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>
6个回答

10

我知道这是一个老问题,但今天我遇到了完全相同的问题,并使用以下方法解决:

ResizeMode="NoResize"

替代

Allowstransparency="true"

ResizeMode可以去除烦人的边框,不会影响WebBrowser控件。在这种情况下,似乎是解决问题最简单的方法 :)


用户3231903的回复是一个很好的替代方案(以便进行评级)。如果您有并想要使用透明度,我建议您查看这篇文章:http://blogs.msdn.com/b/changov/archive/2009/01/19/webbrowser-control-on-transparent-wpf-window.aspx - Vincenzo Costa

5

由于WebBrowser控件实际上不是WPF控件,所以它不能通过WPF技巧来工作。 它是一个包装的ActiveX IE Web浏览器控件,不是由WPF渲染的。

也许你的问题与这篇帖子有关Removing border from WebBrowser control

老实说,如果可能的话,请尝试放弃这个可怕的WebControl并使用其他东西。 有一些免费的替代品具有适当的WPF支持,例如Awesomium.NET、CefSharp或CefGlue.NET(都基于Chromium)。


那是我使用的 :-) - user2819245

3

这个问题很旧了,但我想分享一下我所做的工作。

当您想要创建一个没有边框、可调整大小并能够托管指向URL的WebBrowser控件或Frame控件的窗口时,您简直无法实现。如OP所说,控件的内容将为空。

不过,我找到了一个变通办法。在窗口中,如果您将WindowStyle设置为None,ResizeMode设置为NoResize(请耐心等待,完成后您仍然可以调整大小),然后确保已取消选中AllowsTransparency,您将拥有一个静态大小的无边框窗口,并且将显示浏览器控件。

现在,您可能仍然想要调整大小对吧?那么我们可以通过Interop调用来实现:

    [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这样的控件的兼容性。


0
我在遇到与SystemWebBrowser控件类似的问题时发现了这个问题,我需要移除顶部的空白区域并保持ResizeMode为CanResize。因此,我不得不多找一些解决方案。解决方案是玩弄WindowChrome并将CaptionHeight设置为0,如下所示,
<WindowChrome.WindowChrome>
<WindowChrome 
    CaptionHeight="0"
    ResizeBorderThickness="5" />
</WindowChrome.WindowChrome>

使用这个方法,你可以保留其他属性,比如:

    ResizeMode="CanResize"
    WindowStyle="None"

并且保留AllowsTransparency的默认值


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

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>


-1
你可以做的是将 WebBrowser 控件封装到一个弹出控件中,该弹出控件具有 AllowTransparency 属性,该属性可以设置为 false。当然,缺点是浮动控件需要适应你的目标位置。
 <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>

不,这个不行。尝试调整了这个解决方案(Placement=Mouse是错误的,弹出窗口一直关闭...),但它有太多问题了... - Gh61

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接