User Tools

Site Tools


Command disabled: register
using_20transparent_20windows

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:

        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

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:

        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

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:

        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

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:

        COLOUR 128+8
        CLS

If you prefer to specify your own RGB colour, rather than using one of the predefined palette entries, you can do that as follows:

        COLOUR 8,red%,green%,blue%
        COLOUR 128+8
        CLS

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:

        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

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.

This website uses cookies for visitor traffic analysis. By using the website, you agree with storing the cookies on your computer.More information
using_20transparent_20windows.txt · Last modified: 2018/04/17 14:03 by richardrussell