Create See-Through Controls

Create See-Through Controls

Most standard controls send WM_CTLCOLORXXXXX messages to their parent when they’re about to draw themselves. VB normally handles these messages and responds appropriately with the ForeColor and BackColor properties that you have set for the control. However, it’s possible to override the standard behavior and achieve a transparent background with several basic control types. This example uses the ubiquitous MsgHook for subclassing, but you can use whichever method you prefer. You must ensure that ClipControls is set to False for the form; otherwise, you’ll see whatever is below your form as the background for the controls instead of the background bitmap:

 Option ExplicitPrivate Declare Function SetBkMode Lib "gdi32" (ByVal hdc _	As Long, ByVal nBkMode As Long) As LongPrivate Declare Function SetTextColor Lib "gdi32" _	(ByVal hdc As Long, ByVal crColor As Long) As LongPrivate Declare Function GetStockObject Lib "gdi32" _	(ByVal nIndex As Long) As LongPrivate Const WM_CTLCOLORSTATIC = &H138Private Const TRANSPARENT = 1Private Const NULL_BRUSH = 5Private Sub Form_Load()	' Me.ClipControls = False	' Must be set at design-time!	Msghook1.HwndHook = Me.hWnd	Msghook1.Message(WM_CTLCOLORSTATIC) = TrueEnd SubPrivate Sub Msghook1_Message(ByVal msg As Long, ByVal wp _	As Long, ByVal lp As Long, result As Long)	Select Case msg		Case WM_CTLCOLORSTATIC			' Call the original windowproc to handle the			' foreground colour for the Controls etc			Call Msghook1.InvokeWindowProc(msg, wp, lp)			' Set the background mode to transparent			Call SetBkMode(wp, TRANSPARENT)			' Get the stock null brush and return it			' The brush does nothing when the control			' paints using it, hence giving the			' transparency effect			result = GetStockObject(NULL_BRUSH)		Case Else			' [Replace this line with your own code 			' to call the original windowproc]			result = Msghook1.InvokeWindowProc(msg, wp, lp)	End SelectEnd Sub

This code works for option buttons and checkboxes. I haven’t found any side effects yet. You can make other controls transparent in a similar way, but some of them-including textboxes and listboxes-don’t work correctly because the background doesn’t get erased. You can probably get them to work correctly with additional code. Frames are even harder to get to work correctly; I resorted to using a button created using CreateWindowEx with the BS_GROUPBOX style. Even then, I had to return a brush of approximately the color of the background image; otherwise, you could see the line under the text for the frame.

Share the Post:
Heading photo, Metadata.

What is Metadata?

What is metadata? Well, It’s an odd concept to wrap your head around. Metadata is essentially the secondary layer of data that tracks details about the “regular” data. The regular

XDR solutions

The Benefits of Using XDR Solutions

Cybercriminals constantly adapt their strategies, developing newer, more powerful, and intelligent ways to attack your network. Since security professionals must innovate as well, more conventional endpoint detection solutions have evolved

AI is revolutionizing fraud detection

How AI is Revolutionizing Fraud Detection

Artificial intelligence – commonly known as AI – means a form of technology with multiple uses. As a result, it has become extremely valuable to a number of businesses across

AI innovation

Companies Leading AI Innovation in 2023

Artificial intelligence (AI) has been transforming industries and revolutionizing business operations. AI’s potential to enhance efficiency and productivity has become crucial to many businesses. As we move into 2023, several

data fivetran pricing

Fivetran Pricing Explained

One of the biggest trends of the 21st century is the massive surge in analytics. Analytics is the process of utilizing data to drive future decision-making. With so much of

kubernetes logging

Kubernetes Logging: What You Need to Know

Kubernetes from Google is one of the most popular open-source and free container management solutions made to make managing and deploying applications easier. It has a solid architecture that makes