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

  1. {$I DFS.INC}  { Standard defines for all Delphi Free Stuff components }
  2.  
  3. {------------------------------------------------------------------------------}
  4. { TdfsExtProgressBar v2.06                                                     }
  5. {------------------------------------------------------------------------------}
  6. { A progress bar control that enables access to the new style types and large  }
  7. { range values provided by the updated progress bar control.                   }
  8. {                                                                              }
  9. { Copyright 2000-2001, Brad Stowers.  All Rights Reserved.                     }
  10. {                                                                              }
  11. { Copyright:                                                                   }
  12. { All Delphi Free Stuff (hereafter "DFS") source code is copyrighted by        }
  13. { Bradley D. Stowers (hereafter "author"), and shall remain the exclusive      }
  14. { property of the author.                                                      }
  15. {                                                                              }
  16. { Distribution Rights:                                                         }
  17. { You are granted a non-exlusive, royalty-free right to produce and distribute }
  18. { compiled binary files (executables, DLLs, etc.) that are built with any of   }
  19. { the DFS source code unless specifically stated otherwise.                    }
  20. { You are further granted permission to redistribute any of the DFS source     }
  21. { code in source code form, provided that the original archive as found on the }
  22. { DFS web site (http://www.delphifreestuff.com) is distributed unmodified. For }
  23. { example, if you create a descendant of TDFSColorButton, you must include in  }
  24. { the distribution package the colorbtn.zip file in the exact form that you    }
  25. { downloaded it from http://www.delphifreestuff.com/mine/files/colorbtn.zip.   }
  26. {                                                                              }
  27. { Restrictions:                                                                }
  28. { Without the express written consent of the author, you may not:              }
  29. {   * Distribute modified versions of any DFS source code by itself. You must  }
  30. {     include the original archive as you found it at the DFS site.            }
  31. {   * Sell or lease any portion of DFS source code. You are, of course, free   }
  32. {     to sell any of your own original code that works with, enhances, etc.    }
  33. {     DFS source code.                                                         }
  34. {   * Distribute DFS source code for profit.                                   }
  35. {                                                                              }
  36. { Warranty:                                                                    }
  37. { There is absolutely no warranty of any kind whatsoever with any of the DFS   }
  38. { source code (hereafter "software"). The software is provided to you "AS-IS", }
  39. { and all risks and losses associated with it's use are assumed by you. In no  }
  40. { event shall the author of the softare, Bradley D. Stowers, be held           }
  41. { accountable for any damages or losses that may occur from use or misuse of   }
  42. { the software.                                                                }
  43. {                                                                              }
  44. { Support:                                                                     }
  45. { Support is provided via the DFS Support Forum, which is a web-based message  }
  46. { system.  You can find it at http://www.delphifreestuff.com/discus/           }
  47. { All DFS source code is provided free of charge. As such, I can not guarantee }
  48. { any support whatsoever. While I do try to answer all questions that I        }
  49. { receive, and address all problems that are reported to me, you must          }
  50. { understand that I simply can not guarantee that this will always be so.      }
  51. {                                                                              }
  52. { Clarifications:                                                              }
  53. { If you need any further information, please feel free to contact me directly.}
  54. { This agreement can be found online at my site in the "Miscellaneous" section.}
  55. {------------------------------------------------------------------------------}
  56. { The lateset version of my components are always available on the web at:     }
  57. {   http://www.delphifreestuff.com/                                            }
  58. { See ExtProgressBar.txt for notes, known issues, and revision history.        }
  59. { -----------------------------------------------------------------------------}
  60. { Date last modified:  June 28, 2001                                           }
  61. { -----------------------------------------------------------------------------}
  62.  
  63. unit ExtProgressBar;
  64.  
  65. {$IFNDEF DFS_WIN32}
  66.   ERROR!  This unit only available for Delphi 2.0 and above!!!
  67. {$ENDIF}
  68.  
  69. interface
  70.  
  71. uses
  72.   Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  73.   CommCtrl, ComCtrls;
  74.  
  75.  
  76. const
  77.   { This shuts up C++Builder 3 about the redefiniton being different. There
  78.     seems to be no equivalent in C1.  Sorry. }
  79.   {$IFDEF DFS_CPPB_3_UP}
  80.   {$EXTERNALSYM DFS_COMPONENT_VERSION}
  81.   {$ENDIF}
  82.   DFS_COMPONENT_VERSION = 'TdfsExtProgressBar v2.06';
  83.  
  84. { I can't get PBM_SETBKCOLOR (the BkColor property) to work at all.  If you want
  85.   to have a go at it, enable this define. }
  86.  
  87. {.$DEFINE DFS_TRY_BKCOLOR}
  88.  
  89.  
  90. {$IFDEF DFS_COMPILER_2}
  91. // Internal use types and constants.  These are converted from the new COMMCTRL.H file.
  92. type
  93.   PPBRange = ^TPBRange;
  94.   TPBRange = record
  95.     iLow:  integer;
  96.     iHigh: integer;
  97.   end;
  98. {$ENDIF}
  99.  
  100.  
  101. {$IFDEF DFS_COMPILER_2}
  102. const
  103.   PBM_SETRANGE32     = WM_USER+6;      // lParam = high, wParam = low
  104.   PBM_GETRANGE       = WM_USER+7;      // wParam = return (TRUE ? low : high). lParam = PPBRANGE or NULL
  105.   PBM_GETPOS         = WM_USER+8;
  106. {$ENDIF}
  107.  
  108. { C++Builder 3 and Delphi 4 define these in COMMCTRL.PAS, but no others do }
  109. {$IFNDEF DFS_DELPHI_4_UP}
  110. {$IFNDEF DFS_CPPB_3_UP}
  111. const
  112.   CCM_FIRST          = $2000;          // Common control shared messages
  113.   CCM_SETBKCOLOR     = CCM_FIRST + 1;  // lParam is bkColor
  114.  
  115.   PBM_SETBARCOLOR    = WM_USER+9;      // lParam = bar color
  116.   PBM_SETBKCOLOR     = CCM_SETBKCOLOR; // lParam = bkColor
  117.  
  118.   PBS_SMOOTH         = $01;
  119.   PBS_VERTICAL       = $04;
  120. {$ENDIF}
  121. {$ENDIF}
  122.  
  123.  
  124.  
  125. const
  126.   DEF_COLOR     = clBtnFace;
  127.   DEF_SEL_COLOR = clHighlight;
  128.  
  129.  
  130. type
  131.   {$IFNDEF DFS_COMPILER_4_UP}
  132.   TProgressBarOrientation = (pbHorizontal, pbVertical);
  133.   {$ENDIF}
  134.  
  135.   // The new class
  136.   TdfsExtProgressBar = class(TProgressBar)
  137.   private
  138.     // Internal property variables
  139.     {$IFNDEF DFS_COMPILER_4_UP}
  140.     FPosition: integer;
  141.     FMin: integer;
  142.     FMax: integer;
  143.     FOrientation: TProgressBarOrientation;
  144.     FSmooth: boolean;
  145.     {$ENDIF}
  146.     FColor: TColor;
  147.     FSelectionColor: TColor;
  148.  
  149.     {$IFNDEF DFS_TRY_BKCOLOR}
  150.     procedure WMEraseBkGnd(var Msg: TWMEraseBkGnd); message WM_ERASEBKGND;
  151.     {$ENDIF}
  152.  
  153.     // Property methods
  154.     {$IFNDEF DFS_COMPILER_4_UP}
  155.     procedure SetMin(Val: integer);
  156.     procedure SetMax(Val: integer);
  157.     procedure SetParams(AMin, AMax: integer);
  158.     procedure SetPosition(Val: integer);
  159.     function GetPosition: integer;
  160.     procedure SetSmooth(const Value: boolean);
  161.     {$ENDIF}
  162.     function GetOrientation: TProgressBarOrientation;
  163.     procedure SetOrientation(const Value: TProgressBarOrientation);
  164.     procedure SetColor(Val: TColor);
  165.     procedure SetSelectionColor(val: TColor);
  166.     function GetVersion: string;
  167.     procedure SetVersion(const Val: string);
  168.   protected
  169.     // Overriden methods
  170.     procedure CreateWnd; override;
  171.     {$IFDEF DFS_COMPILER_4_UP}
  172.     procedure DestroyWnd; override;
  173.     {$ENDIF}
  174.     {$IFNDEF DFS_COMPILER_4_UP}
  175.     procedure CreateParams(var Params: TCreateParams); override;
  176.     {$ENDIF}
  177.     procedure Loaded; override;
  178.   public
  179.     constructor Create(AOwner: TComponent); override;
  180.   published
  181.     property Version: string
  182.        read GetVersion
  183.        write SetVersion
  184.        stored FALSE;
  185.     property SelectionColor: TColor
  186.        read FSelectionColor
  187.        write SetSelectionColor
  188.        default DEF_SEL_COLOR;
  189.     property Color: TColor
  190.        read FColor
  191.        write SetColor
  192.        default DEF_COLOR;
  193.     property Orientation: TProgressBarOrientation
  194.        read GetOrientation
  195.        write SetOrientation
  196.        default pbHorizontal;
  197.  
  198.     {$IFNDEF DFS_COMPILER_4_UP}
  199.     // Properties overriden from the ancestor.
  200.     property Smooth: boolean
  201.        read FSmooth
  202.        write SetSmooth
  203.        default FALSE;
  204.     property Min: integer
  205.        read FMin
  206.        write SetMin;
  207.     property Max: integer
  208.        read FMax
  209.        write SetMax;
  210.     property Position: integer
  211.        read GetPosition
  212.        write SetPosition
  213.        default 0;
  214.     {$ENDIF}
  215.   end;
  216.  
  217.  
  218. implementation
  219.  
  220. uses
  221.   Consts;
  222.  
  223. constructor TdfsExtProgressBar.Create(AOwner: TComponent);
  224. begin
  225.   inherited Create(AOwner);
  226.   // Zero out the internal variables.
  227.   {$IFNDEF DFS_COMPILER_4_UP}
  228.   FMin := 0;
  229.   FMax := 100;
  230.   FPosition := 0;
  231.   FSmooth := FALSE;
  232.   FOrientation := pbHorizontal;
  233.   {$ENDIF}
  234.   FColor := DEF_COLOR;
  235.   FSelectionColor := DEF_SEL_COLOR;
  236. end;
  237.  
  238. // CreateWnd is responsible for actually creating the window (value of Handle).
  239. // As soon as the window is created, we need to set it to our values.
  240. procedure TdfsExtProgressBar.CreateWnd;
  241. begin
  242.   inherited CreateWnd;
  243.  
  244.   {$IFNDEF DFS_COMPILER_4_UP}
  245.   // Set the 32-bit min and max range.
  246.   SendMessage(Handle, PBM_SETRANGE32, FMin, FMax);
  247.   // Set the 32-bit position value.
  248.   SendMessage(Handle, PBM_SETPOS, FPosition, 0);
  249.   {$ENDIF}
  250.   // Set the colors
  251.   SendMessage(Handle, PBM_SETBARCOLOR, 0, ColorToRGB(FSelectionColor));
  252. {$IFDEF DFS_TRY_BKCOLOR}
  253.   SendMessage(Handle, PBM_SETBKCOLOR, 0, ColorToRGB(FColor));
  254. {$ENDIF}
  255. end;
  256.  
  257. {$IFDEF DFS_COMPILER_4_UP}
  258. // Delphi 4 loses the position on window recreate usually.
  259. procedure TdfsExtProgressBar.DestroyWnd;
  260. var
  261.   TempPos: integer;
  262. begin
  263.   // Get current value
  264.   TempPos := Position;
  265.   // Kill the window handle
  266.   inherited DestroyWnd;
  267.   // Put the position value into TProgressBar's memory variable so it will be
  268.   // reset in inherited CreateWnd
  269.   Position := TempPos;
  270. end;
  271. {$ENDIF}
  272.  
  273. // CreateParams is responsible for providing all the parameters for describing the
  274. // window to create.  The new vertical and smooth styles are window sytle flags, so
  275. // we need to supply them here.
  276.  
  277. {$IFNDEF DFS_COMPILER_4_UP}
  278. procedure TdfsExtProgressBar.CreateParams(var Params: TCreateParams);
  279. begin
  280.   inherited CreateParams(Params);
  281.  
  282.   with Params do
  283.   begin
  284.     if FOrientation = pbVertical then Style := Style or PBS_VERTICAL;
  285.     if FSmooth then Style := Style or PBS_SMOOTH;
  286.   end;
  287. end;
  288. {$ENDIF}
  289.  
  290. // Loaded is called immediately after a component has been loaded from a stream, i.e
  291. // a form (.DFM) file.
  292. procedure TdfsExtProgressBar.Loaded;
  293. var
  294.   Temp: integer;
  295. begin
  296.   inherited Loaded;
  297.   // If it's the new vertical style, and we are in the form designer (IDE), we have
  298.   // to swap the width and height.
  299.   if (csDesigning in ComponentState) and (Orientation = pbVertical) then
  300.   begin
  301.     Temp := Width;
  302.     Width := Height;
  303.     Height := Temp;
  304.   end;
  305. end;
  306.  
  307.  
  308. // Utility function used by both SetMin and SetMax methods.
  309.  
  310. {$IFNDEF DFS_COMPILER_4_UP}
  311. procedure TdfsExtProgressBar.SetParams(AMin, AMax: integer);
  312. begin
  313.   // Maximum can not be less than the minimum.
  314.   if AMax < AMin then
  315.     {$IFDEF DFS_COMPILER_2}
  316.     raise EInvalidOperation.CreateResFmt(SPropertyOutOfRange, [Self.Classname]);
  317.     {$ELSE}
  318.     raise EInvalidOperation.CreateFmt(SPropertyOutOfRange, [Self.Classname]);
  319.     {$ENDIF}
  320.   // If neither value has changed, there's nothing to do.
  321.   if (FMin <> AMin) or (FMax <> AMax) then begin
  322.     // We can only send window messages if the window has been created (CreateWnd).
  323.     if HandleAllocated then begin
  324.       SendMessage(Handle, PBM_SETRANGE32, AMin, AMax);
  325.       if FMin > AMin then // since Windows sets Position when increase Min..
  326.         SendMessage(Handle, PBM_SETPOS, AMin, 0); // set it back if decrease
  327.     end;
  328.     FMin := AMin;
  329.     FMax := AMax;
  330.   end;
  331. end;
  332.  
  333. // Update the Min property.
  334. procedure TdfsExtProgressBar.SetMin(Val: integer);
  335. begin
  336.   SetParams(Val, FMax);
  337. end;
  338.  
  339. // Update the Max property.
  340. procedure TdfsExtProgressBar.SetMax(Val: integer);
  341. begin
  342.   SetParams(FMin, Val);
  343. end;
  344.  
  345. // Read the current position of the progress bar.
  346. function TdfsExtProgressBar.GetPosition: integer;
  347. begin
  348.   if HandleAllocated then
  349.     Result := SendMessage(Handle, PBM_GETPOS, 0, 0)
  350.   else
  351.     Result := FPosition;
  352. end;
  353.  
  354. // Set the current position of the progress bar.
  355. procedure TdfsExtProgressBar.SetPosition(Val: integer);
  356. begin
  357.   if HandleAllocated then
  358.     SendMessage(Handle, PBM_SETPOS, Val, 0);
  359.   FPosition := Val;
  360. end;
  361.  
  362. procedure TdfsExtProgressBar.SetSmooth(const Value: boolean);
  363. begin
  364.   if FSmooth <> Value then
  365.   begin
  366.     FSmooth := Value;
  367.     RecreateWnd;
  368.   end;
  369. end;
  370.  
  371. {$ENDIF}
  372.  
  373. procedure TdfsExtProgressBar.SetOrientation(const Value: TProgressBarOrientation);
  374. begin
  375.   if Orientation <> Value then
  376.   begin
  377.     // Swap width and height if orientation is changing in design mode
  378.     if (csDesigning in ComponentState) then
  379.       SetBounds(Left, Top, Height, Width);
  380.  
  381.     {$IFDEF DFS_COMPILER_4_UP}
  382.     inherited Orientation := Value;
  383.     {$ELSE}
  384.     FOrientation := Value;
  385.     RecreateWnd;
  386.     {$ENDIF}
  387.   end;
  388. end;
  389.  
  390. function TdfsExtProgressBar.GetOrientation: TProgressBarOrientation;
  391. begin
  392.   {$IFDEF DFS_COMPILER_4_UP}
  393.   Result := inherited Orientation;
  394.   {$ELSE}
  395.   Result := FOrientation;
  396.   {$ENDIF}
  397. end;
  398.  
  399.  
  400. // Set the bar background color.
  401. procedure TdfsExtProgressBar.SetSelectionColor(Val: TColor);
  402. begin
  403.   if HandleAllocated then
  404.     SendMessage(Handle, PBM_SETBARCOLOR, 0, ColorToRGB(Val));
  405.   FSelectionColor := Val;
  406. end;
  407.  
  408.  
  409. // Set the bar background color.
  410. procedure TdfsExtProgressBar.SetColor(val: TColor);
  411. begin
  412. {$IFDEF DFS_TRY_BKCOLOR}
  413.   if HandleAllocated then
  414.     SendMessage(Handle, PBM_SETBKCOLOR, 0, ColorToRGB(Val));
  415. {$ELSE}
  416.   Invalidate;
  417. {$ENDIF}
  418.   FColor := Val;
  419. end;
  420.  
  421. {$IFNDEF DFS_TRY_BKCOLOR}
  422. procedure TdfsExtProgressBar.WMEraseBkGnd(var Msg: TWMEraseBkGnd);
  423. var
  424.   Br: HBRUSH;
  425. begin
  426.   Msg.Result := 1;
  427.   Br := CreateSolidBrush(ColorToRGB(FColor));
  428.   try
  429.     FillRect(Msg.DC, ClientRect, Br);
  430.   finally
  431.     DeleteObject(Br);
  432.   end;
  433. end;
  434. {$ENDIF}
  435.  
  436. function TdfsExtProgressBar.GetVersion: string;
  437. begin
  438.   Result := DFS_COMPONENT_VERSION;
  439. end;
  440.  
  441. procedure TdfsExtProgressBar.SetVersion(const Val: string);
  442. begin
  443.   { empty write method, just needed to get it to show up in Object Inspector }
  444. end;
  445.  
  446.  
  447. end.
  448.  
  449.