How to create a transparent window (hand-coded)?

I am in search of how to create a transparent window. Using the code below (USER32.lib, and SetLayeredWindowAttributes()), the program is not compiling. I think it is because I do not have USER32.lib. Duh!

Is this the best way of creating a transparent window anyway? Or, is there another solution?


    MODULE( 'USER32.LIB' )
      SETLAYEREDWINDOWATTRIBUTES( long, long, byte, long ), long

WIN WINDOW,AT(,,395,224),FONT('MS Sans Serif',8,,FONT:regular),GRAY,NOFRAME

  I# = SetLayeredWindowAttributes(0, 0, 128, 2h) !not sure about the 2h as the last parameter...

I’d first read the doc of SetLayeredWindowAttributes api to not passing random values.

What release of Clarion are you using?
Later releases have SetLayeredWindowAttribute including in the Win API exports,. but earlier releases don’t and so you need to create a custom User32.Lib file using Clarion’s LibMaker.

Note also that your API declaration is missing the required attributes PASCAL and NAME eg

           SetLayeredWindowAttributes(long hwnd,long crKey,long bAlpha,long dwFlags),long,pascal,raw,name('SetLayeredWindowAttributes')

Attached is a complete hand coded project that creates a normal window and then (on a timer tick) gradually makes it more and more transparent.

Created originally in Clarion 9 (where the custom User32.LIB file is required) - in later Clarion releases you’ll need to remove the MyUser32.LIB file from the project to get rid of duplicate declaration (1.9 KB)

The Name() is not required in this case because the Label is the Windows Export name. It is common to add a unique prefix of your own to avoid a future duplicate symbol when you include something that also defines that function without any decoration. e.g. you might prefix with “Bobs_”

Bobs_SetLayeredWindowAttributes(long hwnd,long crKey,long bAlpha,long dwFlags),BOOL,pascal,raw,name('SetLayeredWindowAttributes')

This function returned a BOOL that was chnaged to LONG. The BOOL type exists is Clairon as EQUATE(LONG). I would suggest leaving the Prototype as BOOL so you know it returns True or False with True being success. An API that returns LONG or DWORD you have to look at MSDN to see what kind of info it returns, and what values = success.

And “raw” isn’t really necessary there either. If you’re passing *CSTRING, raw doesn’t pass the extra “length” data. Not sure if there are other data types where raw is useful, but it has to do with passing something by address.

Thanks for all of the replies, Guys! Much appreciated.

I go back and forth between using Clarion (C6 or C10) and PureBasic. Both have their strengths and weaknesses. Clarion is so easy to use for windows, reports, and databases. PureBasic is when you want to do some lower level stuff. Here is the code in PureBasic that I used yesterday to accomplish a transparent window and it meets my needs. PureBasic takes more work to get things done but sometimes it just works. For my needs I need both languages.

Here is the “PureBasic” code:

Procedure   SetWindowTranparency(Window, TransparencyColor = #Cyan, Transparency = 255)
Protected hWnd = WindowID(window)
   ;Transform into opaque layered window
   SetWindowLongPtr_(hWnd, #GWL_EXSTYLE, GetWindowLongPtr_(hWnd, #GWL_EXSTYLE) | #WS_EX_LAYERED)
   SetLayeredWindowAttributes_(hWnd, TransparencyColor, Transparency, #LWA_COLORKEY | #LWA_ALPHA)

! Code to open the window and set it as transparent that happens in the Main() portion of the program:
Define MainWindow = CreateWindow(Width, WindowY)
SetWindowTranparency(MainWindow, #Black, 255)

My goal was to create a transparent window that had colored lines. Using the lines, a driver in iRacing could use the lines as “turn-in points”. I have some more tweaking to do but the “guides” really helped me drive the Dalllara F3 at Oulton Park! A very fast track to drive. This works good for practice but in a race it would be too distracting. We sometimes use the details on the car driven as turn in points. On the F3 we use the rivets (right above the white steering wheel gadget).

I think I am good to go for this project. Thanks again for your ideas and follow through!

Using all this code transparency works, but how to I only make a window transparent, not the controls on it (e.g. transparent window background)?


This does it:
setting a window background to “Gray” color. then

result = SetLayeredWindowAttributes(0{prop:handle},00808080h,transparency,1);

Now the last thing is how do I make it SEMI transparent (background)?

So far I can make whole window (with contents) transparent based on “transparency” value or I can make just a background fully transparent. I need a mix. Semi transparent background only…

Any ideas?

Many years ago, someone had posted a cool Clarion demo about skins or something like that. There were transparencies and weird shapes. Trying to remember who did it.

iAlchemy Pratik Patel. I think he moved into the pizza business after a while or had it running as a parallel venture. I never met him or saw this, its only what the internet said.

That’s the one. Thanks Richard.

Only background semitransparent seems not possible with only one call to SetLayeredWindowAttributes (By reading the MS docs, and some test with different parameters, including setting a value for the alpha channel or setting PROP:Background.

A workaround is attached, based on Graham example app added two windows overlapped and setting both transparencies with different parameters, and some code to follow possible window movements. Transparency.clw (2,9 KB)

1 Like

Thank you, Federico.