home *** CD-ROM | disk | FTP | other *** search
/ Chip 2001 September / Chip_2001-09_cd1.bin / zkuste / delphi / kolekce / d123456 / DFS.ZIP / DFSLayeredForm.pas < prev    next >
Pascal/Delphi Source File  |  2001-06-28  |  14KB  |  357 lines

  1. {$I DFS.INC}  { Standard defines for all Delphi Free Stuff components }
  2.  
  3. {------------------------------------------------------------------------------}
  4. { TdfsLayeredForm v1.00 Beta 3                                                 }
  5. {------------------------------------------------------------------------------}
  6. { A TForm descendent that enables the new transparency features of windows in  }
  7. { Windows 2000.  This feature is not available on older Windows versions, i.e. }
  8. { Win95, Win98, NT 4.                                                          }
  9. {                                                                              }
  10. { Copyright 2000-2001, Brad Stowers.  All Rights Reserved.                     }
  11. {                                                                              }
  12. { Copyright:                                                                   }
  13. { All Delphi Free Stuff (hereafter "DFS") source code is copyrighted by        }
  14. { Bradley D. Stowers (hereafter "author"), and shall remain the exclusive      }
  15. { property of the author.                                                      }
  16. {                                                                              }
  17. { Distribution Rights:                                                         }
  18. { You are granted a non-exlusive, royalty-free right to produce and distribute }
  19. { compiled binary files (executables, DLLs, etc.) that are built with any of   }
  20. { the DFS source code unless specifically stated otherwise.                    }
  21. { You are further granted permission to redistribute any of the DFS source     }
  22. { code in source code form, provided that the original archive as found on the }
  23. { DFS web site (http://www.delphifreestuff.com) is distributed unmodified. For }
  24. { example, if you create a descendant of TDFSColorButton, you must include in  }
  25. { the distribution package the colorbtn.zip file in the exact form that you    }
  26. { downloaded it from http://www.delphifreestuff.com/mine/files/colorbtn.zip.   }
  27. {                                                                              }
  28. { Restrictions:                                                                }
  29. { Without the express written consent of the author, you may not:              }
  30. {   * Distribute modified versions of any DFS source code by itself. You must  }
  31. {     include the original archive as you found it at the DFS site.            }
  32. {   * Sell or lease any portion of DFS source code. You are, of course, free   }
  33. {     to sell any of your own original code that works with, enhances, etc.    }
  34. {     DFS source code.                                                         }
  35. {   * Distribute DFS source code for profit.                                   }
  36. {                                                                              }
  37. { Warranty:                                                                    }
  38. { There is absolutely no warranty of any kind whatsoever with any of the DFS   }
  39. { source code (hereafter "software"). The software is provided to you "AS-IS", }
  40. { and all risks and losses associated with it's use are assumed by you. In no  }
  41. { event shall the author of the softare, Bradley D. Stowers, be held           }
  42. { accountable for any damages or losses that may occur from use or misuse of   }
  43. { the software.                                                                }
  44. {                                                                              }
  45. { Support:                                                                     }
  46. { Support is provided via the DFS Support Forum, which is a web-based message  }
  47. { system.  You can find it at http://www.delphifreestuff.com/discus/           }
  48. { All DFS source code is provided free of charge. As such, I can not guarantee }
  49. { any support whatsoever. While I do try to answer all questions that I        }
  50. { receive, and address all problems that are reported to me, you must          }
  51. { understand that I simply can not guarantee that this will always be so.      }
  52. {                                                                              }
  53. { Clarifications:                                                              }
  54. { If you need any further information, please feel free to contact me directly.}
  55. { This agreement can be found online at my site in the "Miscellaneous" section.}
  56. {------------------------------------------------------------------------------}
  57. { The lateset version of my components are always available on the web at:     }
  58. {   http://www.delphifreestuff.com/                                            }
  59. { See DFSLayeredForm.txt for notes, known issues, and revision history.        }
  60. {------------------------------------------------------------------------------}
  61. { Date last modified:  June 28, 2001                                           }
  62. {------------------------------------------------------------------------------}
  63.  
  64. unit DFSLayeredForm;
  65.  
  66. interface
  67.  
  68. uses
  69.   Windows,
  70.   Forms,
  71.   Controls,
  72.   Graphics,
  73.   Classes;
  74.  
  75. // The new API stuff.  It's not defined in Delphi 5, let's assume it will be in
  76. // Delphi 6.
  77. {$IFNDEF DFS_COMPILER_6_UP}
  78. const
  79.   WS_EX_LAYERED = $00080000;
  80.   LWA_COLORKEY = $00000001;
  81.   LWA_ALPHA = $00000002;
  82.   ULW_COLORKEY = $00000001;
  83.   ULW_ALPHA = $00000002;
  84.   ULW_OPAQUE = $00000004;
  85.   AC_SRC_ALPHA = $1;
  86.  
  87. {$IFNDEF DFS_COMPILER_4_UP}
  88. type
  89.   PBlendFunction = ^TBlendFunction;
  90.   {$IFDEF DFS_CPPB_3_UP} {$EXTERNALSYM _BLENDFUNCTION} {$ENDIF}
  91.   _BLENDFUNCTION = packed record
  92.     BlendOp: BYTE;
  93.     BlendFlags: BYTE;
  94.     SourceConstantAlpha: BYTE;
  95.     AlphaFormat: BYTE;
  96.   end;
  97.   TBlendFunction = _BLENDFUNCTION;
  98.   {$IFDEF DFS_CPPB_3_UP} {$EXTERNALSYM BLENDFUNCTION} {$ENDIF}
  99.   BLENDFUNCTION = _BLENDFUNCTION;
  100.  
  101. const
  102.   { currentlly defined blend function }
  103.   {$IFDEF DFS_CPPB_3_UP} {$EXTERNALSYM AC_SRC_OVER} {$ENDIF}
  104.   AC_SRC_OVER = $00;
  105. {$ENDIF}
  106.  
  107. {$ENDIF}
  108.  
  109. const
  110.   { This shuts up C++Builder 3 about the redefiniton being different. There
  111.     seems to be no equivalent in C1.  Sorry. }
  112.   {$IFDEF DFS_CPPB_3_UP}
  113.   {$EXTERNALSYM DFS_COMPONENT_VERSION}
  114.   {$ENDIF}
  115.   DFS_COMPONENT_VERSION = 'TdfsLayeredForm v1.00 Beta 3';
  116.  
  117. // The new APIs.  They're declared as types here so we can have variables that
  118. // hold the address of the real functions.  This allows us to gracefully deal
  119. // with systems that don't have these functions available.
  120. type
  121.   TSetLayeredWindowAttributes = function(
  122.     hWnd: HWND;             // handle to the layered window
  123.     crKey: COLORREF;        // specifies the color key
  124.     bAlpha: byte;           // value for the blend function
  125.     dwFlags: DWORD          // action
  126.   ): BOOL; stdcall;
  127.  
  128. { This function is supposed to allow a lot more flexibility in the way things
  129.   work and better performance, but I've yet to figure out how to make it work.
  130.   If anyone has a working demo showing the use of this function, I'd love to see
  131.   it.  C/C++ code is fine. }
  132.   TUpdateLayeredWindow = function(
  133.     hWnd: HWND;             // handle to the layered window
  134.     hdcDst: HDC;            // handle to screen DC
  135.     pptDst: PPoint;         // new screen position
  136.     pSize: PSize;           // new size of the layered screen
  137.     hdcSrc: HDC;            // handle to surface DC
  138.     pptSrc: PPoint;         // layer position
  139.     crKey: COLORREF;        // specifies the color key
  140.     const bBlend: TBlendFunction; // value for the blend function
  141.     dwFlags: DWORD          // action
  142.   ): BOOL; stdcall;
  143.  
  144. type
  145.   TdfsLayeredForm = class(TForm)
  146.   private
  147.     FMousePassthrough: boolean;
  148.     FOpacity: byte;
  149.     FTransparentColor: TColor;
  150.     FUseOpacity: boolean;
  151.     FUseTransparentColor: boolean;
  152.   protected
  153.     function GetVersion: string;
  154.     procedure SetVersion(const Val: string);
  155.     procedure SetMousePassthrough(const Value: boolean);
  156.     procedure SetOpacity(const Value: byte);
  157.     procedure SetTransparentColor(const Value: TColor);
  158.     procedure SetUseOpacity(const Value: boolean);
  159.     procedure SetUseTransparentColor(const Value: boolean);
  160.  
  161.     procedure UpdateLayeredAttrs; virtual;
  162.  
  163.     procedure CreateParams(var Params: TCreateParams); override;
  164.     procedure CreateWnd; override;
  165.     procedure Loaded; override;
  166.   public
  167.     constructor Create(AOwner: TComponent); override;
  168.   published
  169.     property MousePassthrough: boolean read FMousePassthrough
  170.       write SetMousePassthrough default FALSE;
  171.     property Opacity: byte read FOpacity write SetOpacity default 128;
  172.     property TransparentColor: TColor read FTransparentColor
  173.       write SetTransparentColor default clWhite;
  174.     property UseOpacity: boolean read FUseOpacity write SetUseOpacity
  175.       default TRUE;
  176.     property UseTransparentColor: boolean read FUseTransparentColor
  177.       write SetUseTransparentColor default FALSE;
  178.     property Version: string read GetVersion write SetVersion stored FALSE;
  179.   end;
  180.  
  181.  
  182. function SetLayeredWindowAttributes(
  183.   hWnd: HWND;             // handle to the layered window
  184.   crKey: COLORREF;        // specifies the color key
  185.   bAlpha: byte;           // value for the blend function
  186.   dwFlags: DWORD          // action
  187. ): BOOL;
  188.  
  189. { This function is supposed to allow a lot more flexibility in the way things
  190.   work and better performance, but I've yet to figure out how to make it work.
  191.   If anyone has a working demo showing the use of this function, I'd love to see
  192.   it.  C/C++ code is fine. }
  193. function UpdateLayeredWindow(
  194.   hWnd: HWND;             // handle to the layered window
  195.   hdcDst: HDC;            // handle to screen DC
  196.   pptDst: PPoint;         // new screen position
  197.   pSize: PSize;           // new size of the layered screen
  198.   hdcSrc: HDC;            // handle to surface DC
  199.   pptSrc: PPoint;         // layer position
  200.   crKey: COLORREF;        // specifies the color key
  201.   const bBlend: TBlendFunction; // value for the blend function
  202.   dwFlags: DWORD          // action
  203. ): BOOL;
  204.  
  205. implementation
  206.  
  207. var
  208.   FDLLHandle: HINST;
  209.   FSetLayeredWindowAttrFunc: TSetLayeredWindowAttributes;
  210.   FUpdateLayeredWindowFunc: TUpdateLayeredWindow;
  211.  
  212.  
  213. function SetLayeredWindowAttributes(hWnd: HWND; crKey: COLORREF; bAlpha: byte;
  214.   dwFlags: DWORD): BOOL;
  215. begin
  216.   if assigned(FSetLayeredWindowAttrFunc) then
  217.     Result := FSetLayeredWindowAttrFunc(hWnd, crKey, bAlpha, dwFlags)
  218.   else
  219.     Result := FALSE;
  220. end;
  221.  
  222. function UpdateLayeredWindow(hWnd: HWND; hdcDst: HDC; pptDst: PPoint;
  223.   pSize: PSize; hdcSrc: HDC; pptSrc: PPoint; crKey: COLORREF;
  224.   const bBlend: TBlendFunction; dwFlags: DWORD): BOOL;
  225. begin
  226.   if assigned(FUpdateLayeredWindowFunc) then
  227.     Result := FUpdateLayeredWindowFunc(hWnd, hdcDst, pptDst, pSize, hdcSrc,
  228.       pptSrc, crKey, bBlend, dwFlags)
  229.   else
  230.     Result := FALSE;
  231. end;
  232.  
  233. { TdfsLayeredForm }
  234.  
  235. constructor TdfsLayeredForm.Create(AOwner: TComponent);
  236. begin
  237.   FMousePassthrough := FALSE;
  238.   FOpacity := 128;
  239.   FTransparentColor := clWhite;
  240.   FUseOpacity := TRUE;
  241.   FUseTransparentColor := FALSE;
  242.   inherited;
  243. end;
  244.  
  245. procedure TdfsLayeredForm.CreateParams(var Params: TCreateParams);
  246. begin
  247.   inherited;
  248.   if not (csDesigning in ComponentState) then
  249.   begin
  250.     Params.ExStyle := Params.ExStyle or WS_EX_LAYERED;
  251.     if FMousePassthrough then
  252.       Params.ExStyle := Params.ExStyle or WS_EX_TRANSPARENT;
  253.   end;
  254. end;
  255.  
  256. procedure TdfsLayeredForm.CreateWnd;
  257. begin
  258.   inherited;
  259.   UpdateLayeredAttrs;
  260. end;
  261.  
  262. function TdfsLayeredForm.GetVersion: string;
  263. begin
  264.   Result := DFS_COMPONENT_VERSION;
  265. end;
  266.  
  267. procedure TdfsLayeredForm.Loaded;
  268. begin
  269.   inherited;
  270.   UpdateLayeredAttrs;
  271. end;
  272.  
  273. procedure TdfsLayeredForm.SetMousePassthrough(const Value: boolean);
  274. begin
  275.   if FMousePassthrough <> Value then
  276.   begin
  277.     FMousePassthrough := Value;
  278.     if HandleAllocated and not (csDesigning in ComponentState) then
  279.       SetWindowLong(Handle, GWL_EXSTYLE, GetWindowLong(Handle, GWL_EXSTYLE) or
  280.         WS_EX_TRANSPARENT);
  281.   end;
  282. end;
  283.  
  284. procedure TdfsLayeredForm.SetOpacity(const Value: byte);
  285. begin
  286.   if FOpacity <> Value then
  287.   begin
  288.     FOpacity := Value;
  289.     UpdateLayeredAttrs;
  290.   end;
  291. end;
  292.  
  293. procedure TdfsLayeredForm.SetTransparentColor(const Value: TColor);
  294. begin
  295.   if FTransparentColor <> Value then
  296.   begin
  297.     FTransparentColor := Value;
  298.     UpdateLayeredAttrs;
  299.   end;
  300. end;
  301.  
  302. procedure TdfsLayeredForm.SetUseOpacity(const Value: boolean);
  303. begin
  304.   if FUseOpacity <> Value then
  305.   begin
  306.     FUseOpacity := Value;
  307.     UpdateLayeredAttrs;
  308.   end;
  309. end;
  310.  
  311. procedure TdfsLayeredForm.SetUseTransparentColor(const Value: boolean);
  312. begin
  313.   if FUseTransparentColor <> Value then
  314.   begin
  315.     FUseTransparentColor := Value;
  316.     UpdateLayeredAttrs;
  317.   end;
  318. end;
  319.  
  320. procedure TdfsLayeredForm.SetVersion(const Val: string);
  321. begin
  322.   { empty write method, just needed to get it to show up in Object Inspector }
  323. end;
  324.  
  325. procedure TdfsLayeredForm.UpdateLayeredAttrs;
  326. var
  327.   Color: COLORREF;
  328.   Flags: DWORD;
  329. begin
  330.   if HandleAllocated and (([csLoading, csDesigning] * ComponentState) = []) then
  331.   begin
  332.     if UseOpacity then
  333.       Flags := LWA_ALPHA
  334.     else
  335.       Flags := 0;
  336.     if UseTransparentColor then
  337.     begin
  338.       Color := ColorToRGB(TransparentColor);
  339.       Flags := Flags or LWA_COLORKEY;
  340.     end
  341.     else
  342.       Color := 0;
  343.  
  344.     SetLayeredWindowAttributes(Handle, Color, FOpacity, Flags);
  345.   end;
  346. end;
  347.  
  348. initialization
  349.   FDLLHandle := LoadLibrary(user32);
  350.   FSetLayeredWindowAttrFunc := GetProcAddress(FDLLHandle,
  351.     'SetLayeredWindowAttributes');
  352.   FUpdateLayeredWindowFunc := GetProcAddress(FDLLHandle,
  353.     'UpdateLayeredWindow');
  354. finalization
  355.   FreeLibrary(FDLLHandle);
  356. end.
  357.