Using+transparent+windows

//by Richard Russell, August 2006//


 * This article is not applicable to Windows 9x, Windows Me or Windows NT4.**

Normally the main output window of your BASIC program has an opaque background, hiding anything 'underneath' (whether the Desktop or another window). However, with Windows 2000 and Windows XP, it is possible to make the window background transparent. This allows some interesting effects to be achieved:


 * Window shapes that are (apparently) not rectangular
 * Desktop gadgets (e.g. a clock) that are not contained within a frame
 * Drawing over the entire desktop area

Creating a window with a transparent background is fairly straightforward. Firstly you should remove the normal window title bar and frame border since otherwise these will still be visible. You can do that with the following code:

code format="bb4w" GWL_STYLE = -16 SWP_NOSIZE = 1 SWP_NOMOVE = 2 SWP_FRAMECHANGED = 32 WS_VISIBLE = &10000000 WS_CLIPSIBLINGS = &4000000 WS_CLIPCHILDREN = &2000000 SYS "SetWindowLong", @hwnd%, GWL_STYLE, WS_VISIBLE+WS_CLIPSIBLINGS+WS_CLIPCHILDREN SYS "SetWindowPos", @hwnd%, -1, 0, 0, 0, 0, SWP_FRAMECHANGED+SWP_NOMOVE+SWP_NOSIZE VDU 26 code Note that doing this will prevent the user moving, resizing, minimising, maximising or closing the window since the usual controls are absent. The window can still be closed with **Alt-F4** but otherwise you should provide the necessary controls within your program.

If you prefer to change the position and size of your window at the same time (particularly since the user cannot do so) you can do that as follows:

code format="bb4w" GWL_STYLE = -16 SWP_FRAMECHANGED = 32 WS_VISIBLE = &10000000 WS_CLIPSIBLINGS = &4000000 WS_CLIPCHILDREN = &2000000 SYS "SetWindowLong", @hwnd%, GWL_STYLE, WS_VISIBLE+WS_CLIPSIBLINGS+WS_CLIPCHILDREN SYS "SetWindowPos", @hwnd%, -1, x%, y%, dx%, dy%, SWP_FRAMECHANGED VDU 26 code where **x%**, **y%** are the desired position of your window and **dx%**,**dy%** the required width and height, in pixels, respectively. If you want to change only the size, not the position, add the **SWP_NOMOVE** flag; to change only the position add the **SWP_NOSIZE** flag.

In the special case of a full-screen window (such as you would need if you want to be able to draw anywhere on the desktop) you can use this code:

code format="bb4w" GWL_STYLE = -16 SWP_FRAMECHANGED = 32 WS_VISIBLE = &10000000 WS_CLIPSIBLINGS = &4000000 WS_CLIPCHILDREN = &2000000 SYS "GetSystemMetrics", 0 TO xscreen% SYS "GetSystemMetrics", 1 TO yscreen% SYS "SetWindowLong", @hwnd%, GWL_STYLE, WS_VISIBLE+WS_CLIPSIBLINGS+WS_CLIPCHILDREN SYS "SetWindowPos", @hwnd%, -1, 0, 0, xscreen%, yscreen%, SWP_FRAMECHANGED VDU 26 code Next you must nominate a 'transparent' colour, that is a colour which - wherever it appears in your window - will become transparent. You should choose this colour with care, since you will not be able to use it as a foreground colour, i.e. none of your text, graphics, pictures etc. can contain this colour because if they do that region will disappear!

Suppose, for example, you choose the dark grey corresponding to colour 8 in the default palette. To arrange that the window background will ultimately become transparent you must clear it to that colour:

code format="bb4w" COLOUR 128+8 CLS code If you prefer to specify your own RGB colour, rather than using one of the predefined palette entries, you can do that as follows:

code format="bb4w" COLOUR 8,red%,green%,blue% COLOUR 128+8 CLS code Where **red%**, **green%** and **blue%** are the colour components, each in the range 0-255. Any other method of clearing the background to your desired 'transparent' colour may also be used.

To tell Windows that you want that colour to become transparent use the following code:

code format="bb4w" GWL_EXSTYLE = -20 WS_EX_LAYERED = &80000 LWA_COLORKEY = 1 transparent% = TINT(0,0) SYS "SetWindowLong", @hwnd%, GWL_EXSTYLE, WS_EX_LAYERED SYS "SetLayeredWindowAttributes", @hwnd%, transparent%, 0, LWA_COLORKEY code That's all there is to it. Anything you write to your window in a colour other than the nominated 'transparent' colour will appear as normal, but the rest will be invisible. You can use text, graphics, images, sprites etc. in the usual ways.