Windows 2000 function SetLayeredWindowAttributes can be used for
creating transparent forms in Delphi.
1 const
2 WS_EX_LAYERED = $80000;
3 LWA_COLORKEY = 1;
4 LWA_ALPHA = 2;
5
6 type
7 TSetLayeredWindowAttributes = function (
8 hwnd : HWND; // handle to the layered window
9 crKey : TColor; // specifies the color key
10 bAlpha : byte; // value for the blend function
11 dwFlags : DWORD // action
12 ): BOOL; stdcall;
Now I would like to talk about practical using of the layered windows.
Tip N1: Capturing screen
If you will try to capture the screen to bitmap using BitBlt function and have a
layered (transparent) window visible, you will get only a picture of the screen
without this window.
To fix it we have to use the new raster-operation code for BitBlt function
CAPTUREBLT that introduced in Windows 2000 for including any windows that are
layered on top of your window in the resulting image.
13 procedure CaptureScreen(AFileName : string);
14 const
15 CAPTUREBLT = $40000000;
16 var
17 hdcScreen : HDC;
18 hdcCompatible : HDC;
19 bmp : TBitmap;
20 hbmScreen : HBITMAP;
21 begin
22 // Create a normal DC and a memory DC for the entire screen. The
23 // normal DC provides a "snapshot" of the screen contents. The
24 // memory DC keeps a copy of this "snapshot" in the associated
25 // bitmap.
26
27 hdcScreen := CreateDC('DISPLAY', nil, nil, nil);
28 hdcCompatible := CreateCompatibleDC(hdcScreen);
29 // Create a compatible bitmap for hdcScreen.
30
31 hbmScreen := CreateCompatibleBitmap(hdcScreen,
32 GetDeviceCaps(hdcScreen, HORZRES),
33 GetDeviceCaps(hdcScreen, VERTRES));
34
35 // Select the bitmaps into the compatible DC.
36 SelectObject(hdcCompatible, hbmScreen);
37 bmp := TBitmap.Create;
38 bmp.Handle := hbmScreen;
39 BitBlt(hdcCompatible,
40 0,0,
41 bmp.Width, bmp.Height,
42 hdcScreen,
43 0,0,
44 SRCCOPY or CAPTUREBLT);
45
46 bmp.SaveToFile(AFileName);
47 bmp.Free;
48 DeleteDC(hdcScreen);
49 DeleteDC(hdcCompatible);
50 end;
Tip N2: Transparent external program
OK, before we made our own forms transparent, but what about making external
programs like Notepad transparent also? Yes, it's also possible.
First, we will start Notepad using ShellExecute function
ShellExecute(Application.Handle,'open','notepad.exe',nil,nil,SW_SHOW);
and after will make it transparent.
51 procedure ShowTransparentNotepad;
52 var
53 np:HWND;
54 begin
55 np:=FindWindow('Notepad',nil);
56 SetTransparentForm(np,100);
57 end;
The code of the SetTransparentForm procedure you can find in article "Transparent
Forms in Delphi"
Tip N3: Transparent Hint
For customizing the standard Delphi hint window we have to override THintWindow
class. After overriding THintWindow to create a new derived type TTransparentHint,
we will assign the new type to the global HintWindowClass variable at application
startup, so that the new transparent hint window type is used for Help Hints.
58 type
59 TTransparentHint = class(THintWindow)
60 private
61 FTransparency : byte;
62 protected
63 procedure CreateParams(var Params: TCreateParams); override;
64 procedure CreateWnd; override;
65 public
66 property Transparency : byte read FTransparency write FTransparency;
67 constructor Create(AOwner : TComponent); override;
68 end;
69
70 { TTransparentHint }
71
72 constructor TTransparentHint.Create(AOwner: TComponent);
73 begin
74 inherited;
75 FTransparency := 100;
76 end;
77
78 procedure TTransparentHint.CreateParams(var Params: TCreateParams);
79 begin
80 inherited;
81 Params.ExStyle := Params.ExStyle or WS_EX_LAYERED;
82 end;
83
84 procedure TTransparentHint.CreateWnd;
85 var
86 SetLayeredWindowAttributes: TSetLayeredWindowAttributes;
87 begin
88 inherited;
89 SetLayeredWindowAttributes :=
90 GetProcAddress(GetModulehandle(user32), 'SetLayeredWindowAttributes');
91 SetLayeredWindowAttributes(Handle, 0, FTransparency, LWA_ALPHA);
92 end;
93
94 {Now we have to assign TTransparentHint to the HintWindowClass variable at
95 application startup.}
96
97 procedure TForm1.FormCreate(Sender: TObject);
98 begin
99 HintWindowClass := TTransparentHint;
100 end;
|