十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
Vb6只有Label控件可以设置背景透明,VB.net基本上都可以,可以按住CTRL多选,或者用鼠标框选,选中多个控件后,设置它们的backcolor属性为透明,或者用代码:
目前成都创新互联公司已为近千家的企业提供了网站建设、域名、网站空间、网站托管、企业网站设计、阳谷网站维护等服务,公司将坚持客户导向、应用为本的策略,正道将秉承"和谐、参与、激情"的文化,与客户和合作伙伴齐心协力一起成长,共同发展。
For Each ctl As Control In Me.Controls
ctl.BackColor = Color.Transparent
Next
下面就介绍一下在VB.NET中可以是图片框透明的两种方法(两种方法的原理都是利用GDI+使图片透明,然后放到背景色为透明色的图片框中实现的) 第一种方法:利用Bitma..
测试了一下,在我的VS2013中的VB中做一个windows常规应用,生成的.exe文件的图标是正常的,透明的。
参见附件。
picturebox设置透明了,你放上图片就会覆盖。
这个道理就像你在墙上挂一个相框,不管相框有没有背板,你在相框中放上照片的话,相框所在的墙面就背覆盖了。
要想不覆盖,就直接添加窗体背景。
Private Sub Picture1_Paint()
Dim l As Long, t As Long
l = (Picture1.Width - Picture1.ScaleWidth) / 2 '求左右边偏差
t = (Picture1.Height - Picture1.ScaleHeight) / 2 '求上下边偏差
Picture1.PaintPicture Me.Picture, 0, 0, , , Picture1.Left + l, Picture1.Top + t, Picture1.ScaleWidth, Picture1.ScaleHeight '载入图片,就用这行
'Picture1.PaintPicture Me.Image, 0, 0, , , Picture1.Left + l, Picture1.Top + t, Picture1.ScaleWidth, Picture1.ScaleHeight '复制的图片,就用这行
End Sub
Public Function TranBlt(DestHdc As Long, X As Long, Y As Long, W As Long, H As Long, srcHdcOrBmp As Long, Optional srcX As Integer, Optional srcY As Integer, Optional srcW As Long, Optional srcH As Long, Optional tc As Long = -1, Optional IsBmp As Boolean) As Long
'srcHdcOrBmp参数 传入的可以是hdc也可以是Bmp对象的Handle,
'IsBmp参数 为真时srcHdcOrBmp代表Bmp对象的Handle,为假时代表hdc
'返回值 成功时返回透明色,不成功时返回-1
Dim tHdc(3) As MemHdc
Dim J As Long, oc As Long, I As Long, Bm As BITMAP, cc As Long, NewDc As Long
Dim sw As Long, sh As Long, sBmp As Long, sHdc As Long, obm As Long, NewX As Long, NewY As Long
If DestHdc = 0 Or srcHdcOrBmp = 0 Or W = 1 And H = 1 Then GoTo fail
If IsBmp Then '若传入的是Bmp句柄,需为其创建一个临时DC
sBmp = srcHdcOrBmp
tHdc(3) = NewMyHdc(DestHdc, 0, 0, srcHdcOrBmp)
sHdc = tHdc(3).hdc
Else
sHdc = srcHdcOrBmp
If srcW = 0 Then sBmp = GetCurrentObject(sHdc, 7)
End If
If sHdc = 0 Or sBmp = 0 Then GoTo fail
If srcW = 0 Then '若没有提供源图大小,需取得整个源图大小
GetObj sBmp, Len(Bm), Bm
sw = Bm.bmWidth - srcX
sh = Bm.bmHeight - srcY
Else
sw = srcW
sh = srcH
End If
If sw 1 Or sh 1 Then GoTo fail
If tc = -1 Then
cc = GetPixel(sHdc, srcX, srcY) '将左上角第一个像素作为源图背景色,用于透明
Else
cc = tc
End If
If W sw Or H sh Then
tHdc(2) = NewMyHdc(DestHdc, W, H)
StretchBlt tHdc(2).hdc, 0, 0, W, H, sHdc, srcX, srcY, sw, sh, vbSrcCopy
'先将源图缩放,下面步骤就一样了。
NewDc = tHdc(2).hdc
Else
NewDc = sHdc
NewX = srcX
NewY = srcY
End If
BitBlt DestHdc, X, Y, W, H, NewDc, NewX, NewY, vbSrcInvert
'将源图先反色(XOR)绘入目标图,若源图背景为黑色,此步可省
'下面是制作Mask图的方法
I = CreateBitmap(W, H, 1, 1, ByVal 0) '建立单色位图
tHdc(0) = NewMyHdc(DestHdc, 0, 0, I) '为单色图建立新DC,并选入
tHdc(1) = NewMyHdc(DestHdc, W, H) '另建一个彩色图及DC,用于存放Mask图
oc = SetBkColor(NewDc, cc) '将源图背景色改为透明色
BitBlt tHdc(0).hdc, 0, 0, W, H, NewDc, NewX, NewY, vbSrcCopy
'先将源图绘入单色DC,由此产生只有正反的Mask图,背景色(透明色)为黑,其它为白
SetBkColor NewDc, oc '恢复源图背景色,不是必须的,但这是个好习惯。
BitBlt tHdc(1).hdc, 0, 0, W, H, tHdc(0).hdc, 0, 0, vbSrcCopy
'单色DC必须复制进彩色DC才能进行后面的的AND运算
'Mask图完成,并已放入彩色DC
BitBlt DestHdc, X, Y, W, H, tHdc(1).hdc, 0, 0, vbSrcAnd '标准透明绘图:选将Mask图用And运算绘入,
BitBlt DestHdc, X, Y, W, H, NewDc, NewX, NewY, vbSrcInvert '再将源图以反色(XOR)绘入一次
DelMyHdc tHdc(0)
DelMyHdc tHdc(1)
If tHdc(2).hdc 0 Then DelMyHdc tHdc(2)
If tHdc(3).hdc 0 Then DelMyHdc tHdc(3)
TranBlt = cc
Exit Function
fail:
If tHdc(3).hdc 0 Then DelMyHdc tHdc(3)
TranBlt = -1
End Function
Private Function NewMyHdc(dHdc As Long, W As Long, H As Long, Optional Bm As Long) As MemHdc
With NewMyHdc
.hdc = CreateCompatibleDC(dHdc)
If Bm = 0 Then
.Bmp = CreateCompatibleBitmap(dHdc, W, H)
Else
.Bmp = Bm
End If
.obm = SelectObject(.hdc, .Bmp)
End With
End Function
Private Function DelMyHdc(MyHdc As MemHdc, Optional nobmp As Boolean) As MemHdc
With MyHdc
If .hdc 0 And .obm 0 Then SelectObject .hdc, .obm
If nobmp = False And .Bmp 0 Then DeleteObject .Bmp
If .hdc 0 Then DeleteDC .hdc
End With
End Function
把这个函数放在一个BAS里,然后在窗体里
TranBlt pic.hdc, X,Y,W,H,PIC2.Picture.Handle, _
TX,TY,TW,TH, , True
就OK