2013-11-22 124 views
3

我正在使用网络浏览器从网络中自动加载图像,但VB.Net窗体应用程序中存在白色背景,图像未填充窗体上的整个导航器对象。如何在VB.Net中为WebBrowser设置透明背景颜色?

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

感谢,
C.

+0

你只使用webbrowser的图像? – K3rnel31

+0

是的。但是它们总是从网页加载(随着图像的变化),然后它会在网页上显示第一个(也是唯一的)图像。 – Masutatsu

回答

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

+0

5个单词的答案?!?!自WebBrowser控件以来窗体上的属性没有该属性?或者你是什么意思? –

+0

那么,你选择透明度的颜色是透明的。整个表单上的任何内容都会变成透明的。所以浏览器的背景是白色的,所以透明的白色按键会使它透明,你可以使用Windows Aero Glass dwm效果来获得透明的效果,但它只能在Windows Vista之后使用,对于以前的Windows版本,你会必须手动绘制它有点长。对你而言,最简单也是最快捷的事情就是透明键:) –

+0

我知道(以及我认为你的意思)...更重要的是,我暗示你应该通过复制/粘贴你的评论到你的答案... 5个单词的答案没有得到upvotes,包含详细/格式化/例子和适当的一级裁判获得upvoted的长答案。 –