且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

WPF,WebBrowser问题如果AllowTransparency = True

更新时间:2022-06-27 02:40:44

还有一个变种。试试这个或等待WPF 4.5(详情这里)。
One more variant. Try this one or wait for WPF 4.5 (details here).


我并不感到惊讶,网页浏览器控件(这只是IE)是没有办法的支持透明度。所以,关闭浏览器控件的透明度,它可能是你唯一的选择。



或 [ ^ ]你可以试试这个。
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.


这个这是一个老问题,但我想发布我已做的工作以使其正常工作。



如果你想创建一个没有边框的窗口可以调整大小并且能够托管一个WebBrowser控件或一个指向你根本不能的URL的Frame控件,所述控件的内容将显示为空,如OP所说。



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



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

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



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


And voila, A WPF window with no border and still movable and resizable without losing compatibility with with controls like WebBrowser