Add Transparent Forms in Delphi : Practical advices


In the http://www.delphipages.com/news/detaildocs.cfm?ID=63]previous article we discussed about using of Windows 2000 function SetLayeredWindowAttributes for creating transparent forms in Delphi.
  1.  
  2. const
  3.  WS_EX_LAYERED = $80000;
  4.  LWA_COLORKEY = 1;
  5.  LWA_ALPHA = 2;
  6.  
  7. type
  8.  TSetLayeredWindowAttributes = function (
  9.      hwnd : HWND; // handle to the layered window
  10.      crKey : TColor; // specifies the color key
  11.      bAlpha : byte; // value for the blend function
  12.      dwFlags : DWORD // action
  13.      ): 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.
  1.  
  2. procedure CaptureScreen(AFileName : string);
  3. const
  4.   CAPTUREBLT = $40000000;
  5. var
  6.   hdcScreen : HDC;
  7.   hdcCompatible : HDC;
  8.   bmp : TBitmap;
  9.   hbmScreen : HBITMAP;
  10. begin
  11. // Create a normal DC and a memory DC for the entire screen. The
  12. // normal DC provides a "snapshot" of the screen contents. The
  13. // memory DC keeps a copy of this "snapshot" in the associated
  14. // bitmap.
  15.  
  16. hdcScreen := CreateDC('DISPLAY', nil, nil, nil);
  17. hdcCompatible := CreateCompatibleDC(hdcScreen);
  18. // Create a compatible bitmap for hdcScreen.
  19.  
  20. hbmScreen := CreateCompatibleBitmap(hdcScreen,
  21.                      GetDeviceCaps(hdcScreen, HORZRES),
  22.                      GetDeviceCaps(hdcScreen, VERTRES));
  23.  
  24. // Select the bitmaps into the compatible DC.
  25. SelectObject(hdcCompatible, hbmScreen);
  26. bmp := TBitmap.Create;
  27. bmp.Handle := hbmScreen;
  28. BitBlt(hdcCompatible,
  29.                0,0,
  30.                bmp.Width, bmp.Height,
  31.                hdcScreen,
  32.                0,0,
  33.                SRCCOPY OR CAPTUREBLT);
  34.  
  35. bmp.SaveToFile(AFileName);
  36. bmp.Free;
  37. DeleteDC(hdcScreen);
  38. DeleteDC(hdcCompatible);
  39. 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

  1. ShellExecute(Application.Handle,'open','notepad.exe',nil,nil,SW_SHOW);
  2.  
  3. and after will make it transparent.
  4.  
  5. procedure ShowTransparentNotepad;
  6. var
  7.   np:HWND;
  8. begin
  9.   np:=FindWindow('Notepad',nil);
  10.   SetTransparentForm(np,100);
  11. end;


The code of the SetTransparentForm procedure you can find in article Transparent Forms in Delphi 5 and 6

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.

  1. type
  2.  TTransparentHint = class(THintWindow)
  3.  private
  4.    FTransparency : byte;
  5.  protected
  6.     procedure CreateParams(var Params: TCreateParams); override;
  7.     procedure CreateWnd; override;
  8. public
  9.    property Transparency : byte read FTransparency write FTransparency;
  10.    constructor Create(AOwner : TComponent); override;
  11.  end;
  12.  
  13. { TTransparentHint }
  14.  
  15. constructor TTransparentHint.Create(AOwner: TComponent);
  16. begin
  17.   inherited;
  18.   FTransparency := 100;
  19. end;
  20.  
  21. procedure TTransparentHint.CreateParams(var Params: TCreateParams);
  22. begin
  23.   inherited;
  24.   Params.ExStyle := Params.ExStyle or WS_EX_LAYERED;
  25. end;
  26.  
  27. procedure TTransparentHint.CreateWnd;
  28. var
  29.  SetLayeredWindowAttributes: TSetLayeredWindowAttributes;
  30. begin
  31.   inherited;
  32.   SetLayeredWindowAttributes :=
  33.   GetProcAddress(GetModulehandle(user32), 'SetLayeredWindowAttributes');
  34.  SetLayeredWindowAttributes(Handle, 0, FTransparency, LWA_ALPHA);
  35. end;
  36.  
  37. Now we have to assign TTransparentHint to the HintWindowClass variable at application startup.
  38.  
  39. procedure TForm1.FormCreate(Sender: TObject);
  40. begin
  41.   HintWindowClass := TTransparentHint;
  42. end;
Related Discussions
  • HOW TO KILL ANYOTHER PROGRAMS WITH DELPHI? (2001-01-02 19:04:42)
    Find the handle of the window using FindWindow or EnumWindows and use PostMessage(ProgramHandle, WM_CLOSE, 0, 0); I have not tried it, but it...
  • HOW TO GREP CHARACTERS IN DELPHI 3 (2001-01-03 03:29:32)
    Var Grep : String; Begin Grep := Copy('2000-46',6,2); End; The first parameter is a string or a dynamic array, the second is the index of...
  • HOW CAN I DEBUG DLL (2001-01-03 02:48:24)
    Do you mean how to find internal errors while writing a DLL file or how to get the exact error while calling a DLL Function ? /Filip
  • USING DLLS (2001-01-04 01:47:50)
    Hi again Goober ... :o) Let's take NETAPI32.DLL as an example. This DLL is created by Microsoft and the explanation of its use is described in...
  • DOT ON SCREEN (2001-01-03 05:42:55)
    Delphi does not support a Pixel function with a HDC, you have to use the Windows-API-function COLORREF SetPixel( HDC hdc, // handle...
  • HOW TO RUN DATABASE APPLICATION ON THE INTERNET (2017-06-09 10:54:22)
    Well, in design I think there is already an issue. Backend is more then just a database with which you connect. There should be a complete layer...
  • HELP WITH CHDIR !!!! TURBO PASCAL VS DELPHI (2001-01-03 08:48:49)
    This is because the ChDir Procedure, the SetCurrentDir and SetCurrentDirectory Functions are changing the directory in the active process. Try...
  • FRIENDS IN DELPHI ?? (OR OTHER ACCESS WORKAROUNDS) (2001-01-04 11:08:37)
    One of the quirks of Delphi is that classes in the same unit can access protected and private methods and variables. Borland does this a lot in...
  • SENDING ICQ MSGS FROM DELPHI (2001-01-03 16:54:03)
    I tried the ICQ Api, but never used it. I've added a simple ICQPager component to delphipages (Winsock/TCPIP).. it might help... Q
  • RUNNING A DATABASE APPLICATION ON ANOTHER COMPUTER (2001-01-07 07:36:33)
    In the BDE on the other PC (the one without the database), you must add an alias to point to the location of the database in the format of:...
Latest News
Submit News Form Past News
Latest Forum Entries