Creating Partially Translucent/Transparent Windows in VB (2) | CodeGuru

Creating Partially Translucent/Transparent Windows in VB (2)

Introduction In Part 1, we talked about translucent/transparent windows and we mentioned ‘holes’ and color. We will see how to make holes in a window in this part. The steps are the same as in translucency (see Part 1). The only difference is in the parameters to the SetLayeredWindowAttributes function. As we said, SetLayeredWindowAttributes is […]

Written By
CodeGuru Staff
CodeGuru Staff
Apr 22, 2004
1 minute read
CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More

Introduction

In Part 1, we talked about translucent/transparent windows and we mentioned ‘holes’ and color. We will see how to make holes in a window in this part.

The steps are the same as in translucency (see Part 1). The only difference is in the parameters to the SetLayeredWindowAttributes function.

As we said, SetLayeredWindowAttributes is responsible for translucency. It can also make ‘holes’ in a window.

Public Declare Function SetLayeredWindowAttributes Lib "user32" _
   (ByVal hwnd As Long, ByVal color As Long, ByVal bAlpha As Byte, _
    ByVal alpha As Long) As Boolean

hwnd: Handle to layered window. ex Me.hwnd.

color: Color used to make areas of a window completely transparent.

bAlpha: Value between 0 (transparent) and 255 (opaque) for alpha blend function.

Flag: Value between 1 and 3. 1 for color, 2 for alpha, and 3 for both of them together.

Example:

SetLayeredWindowAttributes Me.hwnd, RGB(0, 255, 0), 0, 1

Setting color to a value, and setting flag to 1 will cause anything having our color value to become a ‘hole.’

So, if we set color to RGB(0,255,0) and then we put a control with color value RGB(0, 255, 0), the portion of the window that the control occupies will become a ‘hole’ and we can see whatever’s behind it.

Let’s try it.

I will use a slightly modified version of the SetTranslucent function from Part 1. I’m going to add two parameters to it: Color and Flag. Here’s the new SetTranslucent function:

Sub SetTranslucent(ThehWnd As Long, color As Long, nTrans _
                   As Integer, flag As Byte)
On Error GoTo ErrorRtn

   'SetWindowLong and SetLayeredWindowAttributes are API functions;
   'see MSDN for details
   Dim attrib As Long
   attrib = GetWindowLong(ThehWnd, GWL_EXSTYLE)
   SetWindowLong ThehWnd, GWL_EXSTYLE, attrib Or WS_EX_LAYERED
   'anything with color value color will completely disappear if
   'flag = 1 or flag = 3
      SetLayeredWindowAttributes ThehWnd, color, nTrans, flag
   Exit Sub
ErrorRtn:
MsgBox Err.Description & " Source : " & Err.Source

End Sub

That’s all there is to it.

Download the project, source, and executable all in one ZIP file.

CodeGuru Logo

CodeGuru covers topics related to Microsoft-related software development, mobile development, database management, and web application programming. In addition to tutorials and how-tos that teach programmers how to code in Microsoft-related languages and frameworks like C# and .Net, we also publish articles on software development tools, the latest in developer news, and advice for project managers. Cloud services such as Microsoft Azure and database options including SQL Server and MSSQL are also frequently covered.

Property of TechnologyAdvice. © 2026 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.