User Tools

Site Tools


using_20transparent_20windows

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

using_20transparent_20windows [2018/03/31 13:19]
127.0.0.1 external edit
using_20transparent_20windows [2018/04/17 14:03] (current)
richardrussell Added syntax highlighting
Line 1: Line 1:
 =====Using transparent windows===== =====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:\\ \\ +//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   * Window shapes that are (apparently) not rectangular
   * Desktop gadgets (e.g. a clock) that are not contained within a frame   * Desktop gadgets (e.g. a clock) that are not contained within a frame
   * Drawing over the entire desktop area   * 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:\\ \\ + 
 +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 bb4w>
         GWL_STYLE = -16         GWL_STYLE = -16
         SWP_NOSIZE = 1         SWP_NOSIZE = 1
Line 17: Line 20:
         SYS "​SetWindowPos",​ @hwnd%, -1, 0, 0, 0, 0, SWP_FRAMECHANGED+SWP_NOMOVE+SWP_NOSIZE         SYS "​SetWindowPos",​ @hwnd%, -1, 0, 0, 0, 0, SWP_FRAMECHANGED+SWP_NOMOVE+SWP_NOSIZE
         VDU 26         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:\\ \\ +</​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 bb4w>
         GWL_STYLE = -16         GWL_STYLE = -16
         SWP_FRAMECHANGED = 32         SWP_FRAMECHANGED = 32
Line 26: Line 33:
         SYS "​SetWindowPos",​ @hwnd%, -1, x%, y%, dx%, dy%, SWP_FRAMECHANGED         SYS "​SetWindowPos",​ @hwnd%, -1, x%, y%, dx%, dy%, SWP_FRAMECHANGED
         VDU 26         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:\\ \\ +</​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 bb4w>
         GWL_STYLE = -16         GWL_STYLE = -16
         SWP_FRAMECHANGED = 32         SWP_FRAMECHANGED = 32
Line 37: Line 48:
         SYS "​SetWindowPos",​ @hwnd%, -1, 0, 0, xscreen%, yscreen%, SWP_FRAMECHANGED         SYS "​SetWindowPos",​ @hwnd%, -1, 0, 0, xscreen%, yscreen%, SWP_FRAMECHANGED
         VDU 26         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:\\ \\ +</​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 bb4w>
         COLOUR 128+8         COLOUR 128+8
         CLS         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:\\ \\ +</​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 bb4w>
         COLOUR 8,​red%,​green%,​blue%         COLOUR 8,​red%,​green%,​blue%
         COLOUR 128+8         COLOUR 128+8
         CLS         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:\\ \\ +</​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 bb4w>
         GWL_EXSTYLE = -20         GWL_EXSTYLE = -20
         WS_EX_LAYERED = &80000         WS_EX_LAYERED = &80000
Line 51: Line 74:
         SYS "​SetWindowLong",​ @hwnd%, GWL_EXSTYLE,​ WS_EX_LAYERED         SYS "​SetWindowLong",​ @hwnd%, GWL_EXSTYLE,​ WS_EX_LAYERED
         SYS "​SetLayeredWindowAttributes",​ @hwnd%, transparent%,​ 0, LWA_COLORKEY         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. 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.
using_20transparent_20windows.txt · Last modified: 2018/04/17 14:03 by richardrussell