Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Creating A Transparent 'Desktop' Window

Status
Not open for further replies.

coburn

Programmer
Jun 27, 2001
2
GB
Hi, thanks for any help in advance.

What I want to do is create a form that is the exact size of the desktop work area, fully transparent with no titlebar, but responds to mouse clicks, which will displau popup menus. I'm creating a new shell like Explorer/LiteStep/etc for people who are familiar with shells.

Is this possible, and if so, how? Thanks.
 

A word of warning: Windows versions prior to W2000 do not handle transparent windows particularly well. You'll encounter a number of minor oddities.

However, it isn't difficult to enable so, if you want to play, try the following:

Const GWL_EXSTYLE = (-20)
Const WS_EX_TRANSPARENT = &H20&

Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long

Private Sub MakeWinInvisible(hwndForm as Long)
Dim result As Long
result = SetWindowLong(hwndForm, GWL_EXSTYLE, WS_EX_TRANSPARENT)
End Sub

Of course, you'll have to refresh the form after doing this to ensure it gets repainted using the newly assigned style bit.
 
Thanks for your help.

You're right, it does cause some graphical glitches, which I would like to do without. :( This program needs to run on Win9x, and not 2000.

However, I KNOW it is possible to create a transparent window without any graphical glitches, but all these programs are written in C. Does anyone know a way of doing it in VB so it works on Win9x? Thanks.
 

Oh, its easy to create windows with HOLES in them which work perfectly as transparent windows. Problem is that the holes are exactly that - you don't get any mouse events from them because there's nothing there! And so they don't address your requirement. Nevertheless, here's an example:

(for the purposes of this example create a form, and set it's scalemode properties to pixels, then drop in this code)

Option Explicit

Private Declare Function CreateRectRgn Lib "gdi32" (ByVal X1 As Long, ByVal Y1 As Long, ByVal X2 As Long, ByVal Y2 As Long) As Long
Private Declare Function CombineRgn Lib "gdi32" (ByVal hDestRgn As Long, ByVal hSrcRgn1 As Long, ByVal hSrcRgn2 As Long, ByVal nCombineMode As Long) As Long
Private Declare Function SetWindowRgn Lib "user32" (ByVal hWnd As Long, ByVal hRgn As Long, ByVal bRedraw As Boolean) As Long
Private Declare Function GetSystemMetrics Lib "user32" (ByVal nIndex As Long) As Long
Private Const SM_CYCAPTION = 4
Private Const RGN_XOR = 3

Private Sub Form_Resize()
Dim hOuterRegion As Long
Dim hInnerRegion As Long
Dim hCombinedRegion As Long

Dim CaptionHeight As Long

CaptionHeight = GetSystemMetrics(SM_CYCAPTION) + 4 'GetSystemMetrics(SM_CYBORDER)

hCombinedRegion = CreateRectRgn(0, 0, Me.ScaleWidth + 8, Me.ScaleHeight + 12)
hOuterRegion = CreateRectRgn(0, 0, Me.ScaleWidth + 8, Me.ScaleHeight + CaptionHeight + 4)
hInnerRegion = CreateRectRgn(4, CaptionHeight, Me.ScaleWidth + 4, Me.ScaleHeight + CaptionHeight)
CombineRgn hCombinedRegion, hInnerRegion, hOuterRegion, RGN_XOR
SetWindowRgn Form1.hWnd, hCombinedRegion, True
End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top