如何在VB.Net中为WebBrowser设置透明背景颜色?

时间:2013-11-22 09:24:17

标签: vb.net webbrowser-control

我正在使用Web浏览器在VB.Net表单应用程序中自动加载来自Web的图像,但是,有一个白色背景,其中图像不会填充表单上的整个导航器对象

如何在我的应用程序中为Web浏览器对象设置透明背景?

谢谢,
下进行。

1 个答案:

答案 0 :(得分:3)

将表单的透明度键设置为白色。 您选择作为透明度键的颜色是透明的。使用该颜色的整个表单上的任何内容都将变为透明。由于浏览器的背景为白色,白色透明度键将使其透明,您可以使用Windows Aero Glass DWM效果获得玻璃透明度,但它仅适用于Windows Vista以上,对于以前版本的Windows,您将拥有手动绘制它是一项很长的工作。最简单,最快捷的方法是将透明度键设置为白色:)

Me.TransparencyKey = Color.White

enter image description here

You can set the TransparencyKey in the form's properties

如果您想使用Aero Glass DWM,请使用以下代码:

Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Runtime.InteropServices

Private mExtendedFrameMargins As MARGINS

Protected Overrides Sub _
OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
    e.Graphics.SmoothingMode = SmoothingMode.AntiAlias
    'use either one
    e.Graphics.SmoothingMode = SmoothingMode.HighQuality
End Sub

Private Sub Form1_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
    If IsGlassEnabled() Then
        'You should paint the extended frame black for proper composition, but I'm painting it white as you need it
        e.Graphics.FillRectangle(Brushes.White, 0, 0, Me.ClientRectangle.Width, mExtendedFrameMargins.cyTopHeight)
    End If
End Sub

Private Function IsGlassEnabled() As Boolean
    If Environment.OSVersion.Version.Major < 6 Then
        Return False
    End If

    Dim isGlassSupported As Boolean = False
    DwmIsCompositionEnabled(isGlassSupported)
    Return isGlassSupported
End Function

<DllImport("dwmapi.dll")> _
Private Shared Function DwmIsCompositionEnabled(<MarshalAs(UnmanagedType.Bool)> ByRef pfEnabled As Boolean) As Integer
End Function

<DllImport("dwmapi.dll")> _
Private Shared Function DwmExtendFrameIntoClientArea(ByVal hwnd As IntPtr, ByRef pMarInset As MARGINS) As Integer
End Function


<StructLayout(LayoutKind.Sequential)> _
Private Structure MARGINS
    Public cxLeftWidth As Integer
    Public cxRightWidth As Integer
    Public cyTopHeight As Integer
    Public cyBottomHeight As Integer
End Structure



Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
 If IsGlassEnabled() Then
  mExtendedFrameMargins = New MARGINS
  mExtendedFrameMargins.cyTopHeight = Me.Height 'type height here, this is going to be a number (integer)
  DwmExtendFrameIntoClientArea(Me.Handle, mExtendedFrameMargins)
 End If
End Sub

我在我正在创建的应用中使用了此代码 enter image description here