I found a way to make a color transparent for any controls in a form. The problem is that you can see through the form and click through it. I want to know how to make a color transparent in a control so that I can see what's behind the control in the form. I have searched A LOT on google and nothing. I would really love to know how to do this.
Option Explicit
Dim DoIt As Boolean
Private Const BDR_SUNKENOUTER = &H2
Private Const BDR_RAISEDINNER = &H4
Private Const DT_EDITCONTROL = &H2000&
Private Const EDGE_ETCHED = (BDR_SUNKENOUTER Or BDR_RAISEDINNER)
Private Const BF_BOTTOM = &H8
Private Const BF_LEFT = &H1
Private Const BF_RIGHT = &H4
Private Const BF_TOP = &H2
Private Const BF_RECT = (BF_LEFT Or BF_TOP Or BF_RIGHT Or BF_BOTTOM)
Private Const DT_LEFT = &H0
Private Const DT_TOP = &H0
Private Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type
Private Declare Function BitBlt Lib "gdi32" _
(ByVal hDCDest As Long, ByVal XDest As Long, ByVal YDest As Long, _
ByVal nWidth As Long, ByVal nHeight As Long, ByVal hDCSrc As Long, _
ByVal xSrc As Long, ByVal ySrc As Long, ByVal dwRop As Long) As Long
Private Declare Function GetDC Lib "user32" (ByVal hWnd As Long) As Long
Private Declare Function DrawText Lib "user32" Alias "DrawTextA" _
(ByVal hdc As Long, ByVal lpStr As String, ByVal nCount As Long, _
lpRect As RECT, ByVal wFormat As Long) As Long
Private Declare Function DrawCaption Lib "user32" _
(ByVal hWnd As Long, ByVal hdc As Long, pcRect As RECT, ByVal un As Long) As Long
Private Declare Function DrawEdge Lib "user32" _
(ByVal hdc As Long, qrc As RECT, ByVal edge As Long, ByVal grfFlags As Long) As Long
Private Declare Function SetRect Lib "user32" _
(lpRect As RECT, ByVal X1 As Long, ByVal Y1 As Long, ByVal X2 As Long, ByVal Y2 As Long) As Long
Sub TextTrans(MyTB As Object)
'==============================
Dim TempDC As Long
Dim Temp As String
Dim MyLoc As RECT
On Error Resume Next
Temp = MyTB.Text
MyLoc.Left = MyTB.Left
MyLoc.Top = MyTB.Top
MyLoc.Right = MyLoc.Left + MyTB.Width
MyLoc.Bottom = MyLoc.Top + MyTB.Height
'MyTB.Parent.Cls
MyTB.Parent.ForeColor = MyTB.ForeColor
Set MyTB.Parent.Font = MyTB.Font
DrawText MyTB.Parent.hdc, Temp, Len(Temp), MyLoc, DT_EDITCONTROL
TempDC = GetDC(MyTB.hWnd)
BitBlt TempDC, 0, 0, MyTB.Width, MyTB.Height, MyTB.Parent.hdc, MyTB.Left, MyTB.Top, vbSrcCopy
End Sub
Private Sub cmdMakeTransparent_Click()
TextTrans txtInfo
TextTrans RichTextBox1
TextTrans Picture1
Dim MyRect As RECT
Dim X1&, Y1&, X2&, Y2&
Dim strText As String
Me.Cls
Me.ScaleMode = vbPixels
X1 = Frame1.Left - 2
Y1 = Frame1.Top - 2
X2 = X1 + Frame1.Width + 4
Y2 = Y1 + Frame1.Height + 4
SetRect MyRect, X1, Y1, X2, Y2
'Me.ScaleMode = vbTwips
SetRect MyRect, X1, Y1, X2, Y2
DrawEdge Me.hdc, MyRect, EDGE_ETCHED, BF_RECT
strText = Frame1.Caption
SetRect MyRect, X1 + 10, Y1 - 5, X2, Y2
DrawText Me.hdc, strText, Len(strText), MyRect, DT_LEFT Or DT_TOP
TextTrans Frame1
TextTrans List1
End Sub
Private Sub Form_Activate()
txtInfo.SetFocus
End Sub
Private Sub Form_DragDrop(Source As Control, X As Single, Y As Single)
Picture1.Move X, Y
TextTrans Picture1
End Sub
Private Sub Form_Load()
'cmdMakeTransparent_Click
End Sub
Private Sub Frame1_Click()
MsgBox "hi"
End Sub
Private Sub Picture1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
Picture1.Drag vbBeginDrag
End Sub
Private Sub Picture1_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
Picture1.Drag vbEndDrag
End Sub
Private Sub txtInfo_Change()
If Not DoIt Then Exit Sub
TextTrans txtInfo
End Sub
Private Sub txtInfo_KeyPress(KeyAscii As Integer)
If Not DoIt Then Exit Sub
TextTrans txtInfo
End Sub
Private Sub txtInfo_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
If Not DoIt Then Exit Sub
TextTrans txtInfo
End Sub
The other is Alpha Blending, which is probably better. Search for it
OK, maybe I was not clear enough. Here is what I want to to. I want to put a picture box over a text box and set a specific background color to the PictureBox which will be transparent. That means, if I apply a picture to the picture box, I will see the image but not the picture box's backcolor. I will see the texte box behind too or what ever other controls that could be hidden behind.
Here is the code I found to make a color totally tranparent but that is NOT what I want. I want to see the contents of the form behind the control NOT through the form itself.
Code:
Private Declare Function SetWindowLongptr Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Private Declare Function SetLayeredWindowAttributes Lib "user32" (ByVal hwnd As Long, ByVal crKey As Long, ByVal bAlpha As Byte, ByVal dwFlags As Long) As Long
Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Private Const GWL_EXSTYLE = (-20) ' Transparent color
Private Const GWL_STYLE = (-16) ' Expandable borders
Private Const WS_EX_LAYERED = &H80000
Private Const WS_EX_TRANSPARENT = &H20
Private Const WS_EX_APPWINDOW = &H40000
Private Const LWA_ALPHA = &H2
Private Const LWA_COLORKEY = &H1
Private Const COL_TRANS = &HFF ' Transparent Red
Private Sub Form_Load()
Dim Alpha As Byte
Dim MaskColour As Long
Alpha = 200
MaskColour = COL_TRANS
' Whatever control you want to make the backcolor transparent
' Set the backcolor to the transparent color
Picture1.BackColor = MaskColour
Option1.BackColor = MaskColour
Check1.BackColor = MaskColour
Me.BackColor = MaskColour
Text1.BackColor = MaskColour
Command1.BackColor = MaskColour
' Apply Transparency
SetWindowLongptr Me.hwnd, GWL_EXSTYLE, WS_EX_LAYERED Or WS_EX_APPWINDOW
SetLayeredWindowAttributes Me.hwnd, MaskColour, Alpha, LWA_COLORKEY ' Or LWA_ALPHA
End Sub
I want the exact same effect but I want to see the form's contents instead of seeing through it!
I dont want the complete form to be alpha blended, I know how to do that. I want a picture box's Background to be totally transparent to see any controls hidden behind it. I still want to see the borders and the picture if I added one. Here is what I want.
Last edited by avidichard; June 1st, 2009 at 08:45 AM.
Take a look at Davids AlphaBlending example.
There are several calls to perform the blending
Code:
lngStyle = GetWindowLong(Me.hWnd, GWL_EXSTYLE)
' Update them to include the Layered flag
SetWindowLong Me.hWnd, GWL_EXSTYLE, lngStyle Or WS_EX_LAYERED
SetLayeredWindowAttributes Me.hWnd, 0&, 255, LWA_ALPHA
' and
SetLayeredWindowAttributes Me.hWnd, 0&, bAlpha, LWA_ALPHA
Try to replace Me.hWnd in the above calls with the window handle of your PictureBox PictureBox1.hWnd
I'd imagine that should work on any control having a hWnd.
As I said above, It does not work when changing Me.hWnd to Picture1.hWnd. See attachment.
And besides, you will see that when you alpha blend a form, the whole contents of the form alpha blends itself, even the title bar. What I want to be transparent is the background only, not the whole container and its contents. See the example code I posted a bit earlier in this thread. It makes any color of the form totally transparent, that is the effect I want but I want to see behind the PictureBox not through the form, see images above fo example. I've tried with the Picture1.hWnd methode but it does not work.
Nothing to do with that, still the same examples with a semi or completely transparent form. I am not searching for a user control with a transparent background, I want to do what I mentionned above! I want to put a color on a form totally transparent. See pictures attached, it is self explanatory.
* The Best Reasons to Target Windows 8
Learn some of the best reasons why you should seriously consider bringing your Android mobile development expertise to bear on the Windows 8 platform.