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

标签 vb.net webbrowser-control

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

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

谢谢,
C.

最佳答案

将窗体的透明键设置为白色。 您选择作为透明键的颜色被透明化。整个表格上任何具有该颜色的东西都会变成透明的。由于浏览器的背景是白色的,白色的透明键会使其透明,你可以使用 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

关于vb.net - 如何在 VB.Net 中为 WebBrowser 设置透明背景色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20141220/

相关文章:

c# - 使用 Excel Interop VB.Net 在 Excel 中设置边距

c# - 如何以编程方式关闭 IE8 WebBrowser 控件中的怪癖模式?

delphi - 在默认浏览器中打开 TWebBrowser 链接

c# - 如何在 C# 中更新 WebBrowser 控件内的 DOM 内容?

c# 在 webbrowser.url 上使用 .ToString() 方法会自动转换 %26 符号?

vb.net - 从 vb.net 发布推文

vb.net - 尝试使用 linq 对对象列表进行排序时出现无效的强制转换异常

vb.net - 将文件从资源复制到临时文件夹

.net - 如何实现多行文本框的花式滚动条?

c# - Web 浏览器控件 LoadCompleted 事件未触发