【问题标题】:No Transparency on Visual Basic PictureBoxVisual Basic PictureBox 上没有透明度
【发布时间】:2015-12-02 13:47:18
【问题描述】:

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

【问题讨论】:

  • 如果不是表单的颜色,您希望它是什么颜色?
  • @vesan 我希望能够看到它们下面的其他图像和内容
  • 表单上的其他图像,或者您的桌面和其他应用程序?您可以发布您想要的屏幕截图或模型吗?
  • Transparency of picture box 的可能重复项

标签: vb.net transparency picturebox


【解决方案1】:

我有一些代码可以通过将控件后面的每个控件绘制到它的背景上来为控件创建“适当的”透明度。

使用方法:

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) 从工具箱中选择您的组件并将其添加到您的表单中。

希望这会有所帮助!

结果:


编辑:

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

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

【讨论】:

  • 如何从我的工具箱中选择透明图片框?
  • 在 Visual Studio 中打开 Build 菜单并按 Build <your project name>。之后你可以在<your project name> Components下的工具箱中找到它。
  • 我在答案中添加了带图片的解释。
  • 好的,但是当图片框下面的东西发生变化时,它仍然和表单启动时一样......如何刷新?
  • 没关系,我只是使用 Refresh() 函数,非常感谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-11-28
  • 2015-06-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多