没有透明度在Visual Basic PictureBox上

3

我在Visual Basic Express 2010上有一些图片框,这些图片上有alpha通道,但当将背景色设置为透明时,它并不变成透明的,而是成为表单的颜色。我仍然不能通过alpha图像看到其他任何东西。问题是什么? 我不仅想看到图片框后面的父对象,还想看到它下面的所有其他对象。


1
如果不是表单的颜色,你会期望它是什么颜色? - vesan
@vesan 我想要能够在它们下面看到其他的图片和东西。 - christai
表单上的其他图片,或者你的桌面和其他应用程序?你能否发布一个截图或模型,展示你想要它看起来的样子? - vesan
可能是图片框的透明度的重复问题。 - Zohar Peled
1个回答

8
我有一些代码可以通过将每个位于控件后面的控件绘制到其背景上来为控件创建“透明度”。 使用方法: 1)创建一个自定义类。(从“添加新项”菜单中)
2)给它一个你选择的名称(例如:TransparentPictureBox
3)使它继承自原始的PictureBox。
Public Class TransparentPictureBox
    Inherits PictureBox

End Class

4) 将此代码粘贴到类中:

Protected Overrides Sub OnPaintBackground(e As System.Windows.Forms.PaintEventArgs)
    MyBase.OnPaintBackground(e)

    If Parent IsNot Nothing Then
        Dim index As Integer = Parent.Controls.GetChildIndex(Me)

        For i As Integer = Parent.Controls.Count - 1 To index + 1 Step -1
            Dim c As Control = Parent.Controls(i)
            If c.Bounds.IntersectsWith(Bounds) AndAlso c.Visible = True Then
                Dim bmp As New Bitmap(c.Width, c.Height, e.Graphics)
                c.DrawToBitmap(bmp, c.ClientRectangle)
                e.Graphics.TranslateTransform(c.Left - Left, c.Top - Top)
                e.Graphics.DrawImageUnscaled(bmp, Point.Empty)
                e.Graphics.TranslateTransform(Left - c.Left, Top - c.Top)
                bmp.Dispose()
            End If
        Next
    End If
End Sub

这段代码将覆盖 PictureBox 的 OnPaintBackground 事件,从而绘制自己的透明背景。

5) 构建项目。

6) 从工具箱中选择你的组件并添加到你的窗体上。

希望这可以帮到你!

结果:

Result using two custom picture boxes and one button.


编辑:

除了您的评论之外,首先通过 Build > Build <your project name> 菜单构建您的项目。

Building your project.

然后你可以在工具箱的顶部找到您的自定义控件,它位于<your project name> Components 类别下方。

Custom project Components.


我该如何从工具箱中选择透明的图片框? - christai
在Visual Studio中打开“生成”菜单,然后按下“生成<你的项目名称>”。之后你可以在工具箱中找到它,在“<你的项目名称>组件”下面。 - Visual Vincent
我在我的答案中添加了带图片的解释。 - Visual Vincent
好的,但是当图片框下面的某些东西改变时,它仍然看起来与启动表单时一样...怎么才能刷新它呢? - christai
1
没关系,我只需使用Refresh()函数,非常感谢! - christai

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