我在2010上有一些图片框,这些图片上有alpha通道,但是当背景色设置为透明时,它不会变得透明,而是成为窗体的颜色。我还是看不见阿尔法地图上的其他东西。有什么问题吗?我不仅希望看到图片框后面的父对象,而且希望看到它下面的所有其他对象。
发布于 2015-09-07 06:07:19
我有一些代码,通过将每个控件后面的控件绘制到它的背景上,从而为控件创建“适当的”透明度。
如何使用:
1)创建自定义类。(来自“添加新项”菜单)
2)给它一个你选择的名字(例如:TransparentPictureBox)
3)使其继承原始PictureBox。
Public Class TransparentPictureBox
Inherits PictureBox
End Class4)将此代码粘贴到类中:
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)从ToolBox中选择组件并将其添加到表单中。
希望这能有所帮助!
结果:

编辑:
除了您的评论之外,从通过Build > Build <your project name>菜单构建项目开始。

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

https://stackoverflow.com/questions/32431300
复制相似问题