VrSystem.pas
上传用户:hbszzs
上传日期:2008-08-20
资源大小:628k
文件大小:25k
源码类别:

Delphi控件源码

开发平台:

Delphi

  1. {*****************************************************}
  2. {                                                     }
  3. {     Varian Component Workshop                       }
  4. {                                                     }
  5. {     Varian Software NL (c) 1996-2000                }
  6. {     All Rights Reserved                             }
  7. {                                                     }
  8. {*****************************************************}
  9. unit VrSystem;
  10. {$I VRLIB.INC}
  11. interface
  12. uses
  13.   Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  14.   ShellAPI, Menus, VrTypes, VrClasses, VrControls;
  15. type
  16.   TVrBitmapList = class(TVrSharedComponent)
  17.   private
  18.     FBitmaps: TVrBitmaps;
  19.     FOnChange: TNotifyEvent;
  20.     procedure SetBitmaps(Value: TVrBitmaps);
  21.     procedure BitmapsChanged(Sender: TObject);
  22.   protected
  23.     procedure Changed; virtual;
  24.   public
  25.     constructor Create(AOwner: TComponent); override;
  26.     destructor Destroy; override;
  27.     function GetBitmap(Index: Integer): TBitmap;
  28.   published
  29.     property Bitmaps: TVrBitmaps read FBitmaps write SetBitmaps;
  30.     property OnChange: TNotifyEvent read FOnChange write FOnChange;
  31.   end;
  32.   TVrStringList = class(TVrComponent)
  33.   private
  34.     FItems: TStrings;
  35.     FOnChange: TNotifyEvent;
  36.     FOnChanging: TNotifyEvent;
  37.     function GetCount: Integer;
  38.     function GetSorted: Boolean;
  39.     procedure SetItems(Value: TStrings);
  40.     procedure SetSorted(Value: Boolean);
  41.     procedure Change(Sender: TObject);
  42.     procedure Changing(Sender: TObject);
  43.   public
  44.     constructor Create(AOwner: TComponent); override;
  45.     destructor Destroy; override;
  46.     property Count: Integer read GetCount;
  47.   published
  48.     property Strings: TStrings read FItems write SetItems;
  49.     property Sorted: Boolean read GetSorted write SetSorted default false;
  50.     property OnChange: TNotifyEvent read FOnChange write FOnChange;
  51.     property OnChanging: TNotifyEvent read FOnChanging write FOnChanging;
  52.   end;
  53.   TVrKeyStateType = (ksNUM, ksCAPS, ksSCROLL);
  54.   TVrKeyStateTypes = set of TVrKeyStateType;
  55.   TVrKeyStatus = class(TVrComponent)
  56.   private
  57.     FHandle: HWnd;
  58.     FMonitorEvents: Boolean;
  59.     FKeys: TVrKeyStateTypes;
  60.     FOnChange: TNotifyEvent;
  61.     procedure SetKeys(Value: TVrKeyStateTypes);
  62.     procedure SetMonitorEvents(Value: Boolean);
  63.     procedure ChangeState(Key: Word; Active: Boolean);
  64.     procedure UpdateTimer;
  65.     procedure WndProc(var Msg: TMessage);
  66.   protected
  67.     procedure Timer;
  68.     procedure Changed; dynamic;
  69.   public
  70.     constructor Create(AOwner: TComponent); override;
  71.     destructor Destroy; override;
  72.   published
  73.     property Keys: TVrKeyStateTypes read FKeys write SetKeys default [];
  74.     property MonitorEvents: Boolean read FMonitorEvents write SetMonitorEvents default false;
  75.     property OnChange: TNotifyEvent read FOnChange write FOnChange;
  76.   end;
  77. const  
  78.   WM_TOOLTRAYNOTIFY = WM_USER + $44;
  79. type
  80.   TVrCustomTrayIcon = class(TVrComponent)
  81.   private
  82.     FIconData: TNOTIFYICONDATA;
  83.     FIcon: TIcon;
  84.     FEnabled: Boolean;
  85.     FHint: string;
  86.     FShowHint: Boolean;
  87.     FVisible: Boolean;
  88.     FPopupMenu: TPopupMenu;
  89.     FExists: Boolean;
  90.     FClicked: Boolean;
  91.     FHideTaskBtn: Boolean;
  92.     FLeftBtnPopup: Boolean;
  93.     FOnClick: TNotifyEvent;
  94.     FOnDblClick: TNotifyEvent;
  95.     FOnMouseDown: TMouseEvent;
  96.     FOnMouseUp: TMouseEvent;
  97.     FOnMouseMove: TMouseMoveEvent;
  98.     OldAppProc: Pointer;
  99.     NewAppProc: Pointer;
  100.     procedure SetIcon(Value: TIcon);
  101.     procedure SetVisible(Value: Boolean);
  102.     procedure SetHint(const Value: string);
  103.     procedure SetShowHint(Value: Boolean);
  104.     procedure SetPopupMenu(Value: TPopupMenu);
  105.     procedure ShowMenu;
  106.     procedure UpdateHint;
  107.     procedure UpdateSystemTray;
  108.     procedure IconChanged(Sender: TObject);
  109.     procedure HookApp;
  110.     procedure UnhookApp;
  111.     procedure HookAppProc(var Message: TMessage);
  112.   protected
  113.     procedure WndProc(var Msg: TMessage); virtual;
  114.     procedure Notification(AComponent: TComponent;
  115.       Operation: TOperation); override;
  116.     procedure DoHideTaskBtn;
  117.     procedure Click; dynamic;
  118.     procedure DblClick; dynamic;
  119.     procedure MouseDown(Button: TMouseButton; Shift: TShiftState;
  120.       X, Y: Integer); dynamic;
  121.     procedure MouseUp(Button: TMouseButton; Shift: TShiftState;
  122.       X, Y: Integer); dynamic;
  123.     procedure MouseMove(Shift: TShiftState; X, Y: Integer); dynamic;
  124.     property Icon: TIcon read FIcon write SetIcon;
  125.     property Visible: Boolean read FVisible write SetVisible default false;
  126.     property Enabled: Boolean read FEnabled write FEnabled default True;
  127.     property Hint: string read FHint write SetHint;
  128.     property ShowHint: Boolean read FShowHint write SetShowHint default false;
  129.     property PopupMenu: TPopupMenu read FPopupMenu write SetPopupMenu;
  130.     property HideTaskBtn: Boolean read FHideTaskBtn write FHideTaskBtn default false;
  131.     property LeftBtnPopup: Boolean read FLeftBtnPopup write FLeftBtnPopup default false;
  132.     property OnClick: TNotifyEvent read FOnClick write FOnClick;
  133.     property OnDblClick: TNotifyEvent read FOnDblClick write FOnDblClick;
  134.     property OnMouseDown: TMouseEvent read FOnMouseDown write FOnMouseDown;
  135.     property OnMouseUp: TMouseEvent read FOnMouseUp write FOnMouseUp;
  136.     property OnMouseMove: TMouseMoveEvent read FOnMouseMove write FOnMouseMove;
  137.   public
  138.     constructor Create(AOwner: TComponent); override;
  139.     destructor Destroy; override;
  140.     procedure HideMainForm;
  141.     procedure ShowMainForm;
  142.   end;
  143.   TVrTrayIcon = class(TVrCustomTrayIcon)
  144.   published
  145.     property Icon;
  146.     property Visible;
  147.     property Enabled;
  148.     property Hint;
  149.     property ShowHint;
  150.     property PopupMenu;
  151.     property HideTaskBtn;
  152.     property LeftBtnPopup;
  153.     property OnClick;
  154.     property OnDblClick;
  155.     property OnMouseDown;
  156.     property OnMouseUp;
  157.     property OnMouseMove;
  158.   end;
  159.   TVrCopyErrorEvent = procedure(Sender: TObject;
  160.     ErrorCode: Integer) of object;
  161.   TVrOpenEvent = procedure(Sender: TObject;
  162.     Size: Integer; Date, Time: TDateTime) of object;
  163.   TVrOverwriteEvent = procedure(Sender: TObject;
  164.     var Overwrite: Boolean) of object;
  165.   TVrProgressEvent = procedure(Sender: TObject; BytesCopied: Integer;
  166.     var Cancel: Boolean) of object;
  167.   TVrOverwriteMode = (omAlways, omEvent);
  168.   TVrCopyFile = class(TVrComponent)
  169.   private
  170.     FDestFile: string;
  171.     FSourceFile: string;
  172.     FBufferSize: TVrMaxInt;
  173.     FOverwrite: TVrOverwriteMode;
  174.     FCancel: Boolean;
  175.     FCopyDateTime: Boolean;
  176.     FBeforeOverwrite: TVrOverwriteEvent;
  177.     FBeforeOpen: TVrOpenEvent;
  178.     FOnProgress: TVrProgressEvent;
  179.     FAfterCopy: TNotifyEvent;
  180.   protected
  181.     function CheckExists: Boolean;
  182.     function CheckOverwrite: Boolean;
  183.     procedure DoProgress(BytesCopied: Integer; var Cancel: Boolean);
  184.     procedure DoAfterCopy;
  185.   public
  186.     constructor Create(AOwner: TComponent); override;
  187.     destructor Destroy; override;
  188.     procedure Execute;
  189.     procedure Terminate;
  190.   published
  191.     property DestFile: string read FDestFile write FDestFile;
  192.     property SourceFile: string read FSourceFile write FSourceFile;
  193.     property BufferSize: TVrMaxInt read FBufferSize write FBufferSize default 1024;
  194.     property Overwrite: TVrOverwriteMode read FOverwrite write FOverwrite default omAlways;
  195.     property CopyDateTime: Boolean read FCopyDateTime write FCopyDateTime;
  196.     property BeforeOverwrite: TVrOverwriteEvent read FBeforeOverwrite write FBeforeOverwrite;
  197.     property BeforeOpen: TVrOpenEvent read FBeforeOpen write FBeforeOpen;
  198.     property OnProgress: TVrProgressEvent read FOnProgress write FOnProgress;
  199.     property AfterCopy: TNotifyEvent read FAfterCopy write FAfterCopy;
  200.   end;
  201.   TVrLocateEvent = procedure(Sender: TObject; Path: string;
  202.     SearchRec: TSearchRec; var Cancel: Boolean) of object;
  203.   TVrDirScan = class(TVrComponent)
  204.   private
  205.     FMask: string;
  206.     FPath: string;
  207.     FRecursive: Boolean;
  208.     FCancel: Boolean;
  209.     FScanning: Boolean;
  210.     FOnLocate: TVrLocateEvent;
  211.     FOnNotify: TNotifyEvent;
  212.   protected
  213.     procedure Notify;
  214.     procedure LocateFile(Path: string; SearchRec: TSearchRec);
  215.     procedure Scan(Path, Mask: string; Recurse: Boolean);
  216.     function AbortScan: Boolean;
  217.   public
  218.     constructor Create(AOwner: TComponent); override;
  219.     destructor Destroy; override;
  220.     procedure Execute;
  221.     procedure Cancel;
  222.   published
  223.     property Mask: string read FMask write FMask;
  224.     property Path: string read FPath write FPath;
  225.     property Recursive: Boolean read FRecursive write FRecursive default True;
  226.     property OnLocate: TVrLocateEvent read FOnLocate write FOnLocate;
  227.     property OnNotify: TNotifyEvent read FOnNotify write FOnNotify;
  228.   end;
  229. implementation
  230. { TVrBitmapList }
  231. constructor TVrBitmapList.Create(AOwner: TComponent);
  232. begin
  233.   inherited Create(AOwner);
  234.   FBitmaps := TVrBitmaps.Create;
  235.   FBitmaps.OnChange := BitmapsChanged;
  236. end;
  237. destructor TVrBitmapList.Destroy;
  238. begin
  239.   FBitmaps.Free;
  240.   inherited Destroy;
  241. end;
  242. procedure TVrBitmapList.SetBitmaps(Value: TVrBitmaps);
  243. begin
  244.   FBitmaps.Assign(Value);
  245. end;
  246. function TVrBitmapList.GetBitmap(Index: Integer): TBitmap;
  247. begin
  248.   Result := nil;
  249.   if (Index > -1) and (Index < Bitmaps.Count) then
  250.     Result := Bitmaps[Index];
  251. end;
  252. procedure TVrBitmapList.Changed;
  253. begin
  254.   if Assigned(FOnChange) then FOnChange(Self);
  255. end;
  256. procedure TVrBitmapList.BitmapsChanged(Sender: TObject);
  257. begin
  258.   NotifyClients;
  259.   Changed;
  260. end;
  261. { TVrStringList }
  262. constructor TVrStringList.Create(AOwner: TComponent);
  263. begin
  264.   inherited Create(AOwner);
  265.   FItems := TStringList.Create;
  266.   TStringList(FItems).OnChange := Change;
  267.   TStringList(FItems).OnChanging := Changing;
  268. end;
  269. destructor TVrStringList.Destroy;
  270. begin
  271.   FItems.Free;
  272.   inherited Destroy;
  273. end;
  274. procedure TVrStringList.SetItems(Value: TStrings);
  275. begin
  276.   FItems.Assign(Value);
  277. end;
  278. function TVrStringList.GetSorted: Boolean;
  279. begin
  280.   Result := TStringList(FItems).Sorted;
  281. end;
  282. function TVrStringList.GetCount: Integer;
  283. begin
  284.   Result := FItems.Count;
  285. end;
  286. procedure TVrStringList.SetSorted(Value: Boolean);
  287. begin
  288.   TStringList(FItems).Sorted := Value;
  289. end;
  290. procedure TVrStringList.Change(Sender: TObject);
  291. begin
  292.   if Assigned(FOnChange) then FOnChange(Self);
  293. end;
  294. procedure TVrStringList.Changing(Sender: TObject);
  295. begin
  296.   if Assigned(FOnChanging) then FOnChanging(Self);
  297. end;
  298. { TVrKeyStatus }
  299. constructor TVrKeyStatus.Create(AOwner: TComponent);
  300. begin
  301.   inherited Create(AOwner);
  302.   FKeys := [];
  303.   FMonitorEvents := false;
  304.   FHandle := AllocateHWnd(WndProc);
  305. end;
  306. destructor TVrKeyStatus.Destroy;
  307. begin
  308.   FMonitorEvents := false;
  309.   UpdateTimer;
  310.   DeallocateHWnd(FHandle);
  311.   inherited Destroy;
  312. end;
  313. procedure TVrKeyStatus.WndProc(var Msg: TMessage);
  314. begin
  315.   with Msg do
  316.     if Msg = WM_TIMER then
  317.       try
  318.         Timer;
  319.       except
  320.         Application.HandleException(Self);
  321.       end
  322.     else
  323.       Result := DefWindowProc(FHandle, Msg, wParam, lParam);
  324. end;
  325. procedure TVrKeyStatus.UpdateTimer;
  326. begin
  327.   KillTimer(FHandle, 1);
  328.   if MonitorEvents then
  329.     if SetTimer(FHandle, 1, 100, nil) = 0 then
  330.       raise EOutOfResources.Create('Out of resources.');
  331. end;
  332. procedure TVrKeyStatus.Changed;
  333. begin
  334.   if Assigned(FOnChange) then FOnChange(Self);
  335. end;
  336. procedure TVrKeyStatus.Timer;
  337. var
  338.   Current: Integer;
  339.   NewKeys: TVrKeyStateTypes;
  340. begin
  341.   NewKeys := [];
  342.   Current := GetKeyState(VK_NUMLOCK);
  343.   if Current <> 0 then NewKeys := NewKeys + [ksNUM];
  344.   Current := GetKeyState(VK_CAPITAL);
  345.   if Current <> 0 then NewKeys := NewKeys + [ksCAPS];
  346.   Current := GetKeyState(VK_SCROLL);
  347.   if Current <> 0 then NewKeys := NewKeys + [ksSCROLL];
  348.   if not (csDesigning in ComponentState) then
  349.     if Keys <> NewKeys then
  350.     begin
  351.       FKeys := NewKeys;
  352.       Changed;
  353.     end;
  354. end;
  355. procedure TVrKeyStatus.ChangeState(Key: Word; Active: Boolean);
  356. var
  357.   Current: Integer;
  358.   KeyState: TKeyBoardState;
  359. begin
  360.   Current := GetKeyState(Key);
  361.   GetKeyboardState(KeyState);
  362.   if (Current = 0) and (Active) then
  363.   begin
  364.     KeyState[Key] := 1;
  365.     SetKeyboardState(KeyState);
  366.   end
  367.   else
  368.   if (not Active) then
  369.   begin
  370.     KeyState[Key] := 0;
  371.     SetKeyboardState(KeyState);
  372.   end;
  373. end;
  374. procedure TVrKeyStatus.SetMonitorEvents(Value: Boolean);
  375. begin
  376.   if FMonitorEvents <> Value then
  377.   begin
  378.     FMonitorEvents := Value;
  379.     UpdateTimer;
  380.   end;
  381. end;
  382. procedure TVrKeyStatus.SetKeys(Value: TVrKeyStateTypes);
  383. const
  384.   KeyValues: array[TVrKeyStateType] of Word =
  385.     (VK_NUMLOCK, VK_CAPITAL, VK_SCROLL);
  386. var
  387.   I: TVrKeyStateType;
  388. begin
  389.   if FKeys <> Value then
  390.   begin
  391.     FKeys := Value;
  392.     for I := Low(TVrKeyStateType) to High(TVrKeyStateType) do
  393.       ChangeState(KeyValues[I], I in Value);
  394.     Changed;
  395.   end;
  396. end;
  397. { TVrCustomTrayIcon }
  398. constructor TVrCustomTrayIcon.Create(AOwner: TComponent);
  399. begin
  400.   inherited Create(AOwner);
  401.   FIcon := TIcon.Create;
  402.   FIcon.OnChange := IconChanged;
  403.   FEnabled := True;
  404.   FVisible := false;
  405.   FExists := false;
  406.   FShowHint := false;
  407.   FLeftBtnPopup := false;
  408.   FHideTaskBtn := false;
  409.   with FIconData do
  410.   begin
  411.     cbSize := SizeOf(TNOTIFYICONDATA);
  412.     Wnd := AllocateHWnd(WndProc);
  413.     uID := 0;
  414.     uFlags := NIF_MESSAGE + NIF_ICON + NIF_TIP;
  415.     uCallbackMessage := WM_TOOLTRAYNOTIFY;
  416.   end;
  417.   HookApp;
  418. end;
  419. destructor TVrCustomTrayIcon.Destroy;
  420. begin
  421.   Visible := false;
  422.   FIcon.Free;
  423.   DeallocateHWnd(FIconData.Wnd);
  424.   UnhookApp;
  425.   inherited Destroy;
  426. end;
  427. procedure TVrCustomTrayIcon.HookApp;
  428. begin
  429.   if not (csDesigning in ComponentState) then
  430.   begin
  431.     OldAppProc := Pointer(GetWindowLong(Application.Handle, GWL_WNDPROC));
  432.     NewAppProc := MakeObjectInstance(HookAppProc);
  433.     SetWindowLong(Application.Handle, GWL_WNDPROC, LongInt(NewAppProc));
  434.   end;
  435. end;
  436. procedure TVrCustomTrayIcon.UnhookApp;
  437. begin
  438.   if not (csDesigning in ComponentState) then
  439.   begin
  440.     if Assigned(OldAppProc) then
  441.       SetWindowLong(Application.Handle, GWL_WNDPROC, LongInt(OldAppProc));
  442.     if Assigned(NewAppProc) then
  443.       FreeObjectInstance(NewAppProc);
  444.     NewAppProc := nil;
  445.     OldAppProc := nil;
  446.   end;
  447. end;
  448. procedure TVrCustomTrayIcon.HookAppProc(var Message: TMessage);
  449. begin
  450.   with Message do
  451.   begin
  452.     case Msg of
  453.       WM_SIZE:
  454.         if wParam = SIZE_MINIMIZED then
  455.         begin
  456.           if FHideTaskBtn then
  457.             DoHideTaskBtn;
  458.         end;
  459.     end;
  460.     Result := CallWindowProc(OldAppProc, Application.Handle, Msg, wParam, lParam);
  461.   end;
  462. end;
  463. procedure TVrCustomTrayIcon.DoHideTaskBtn;
  464. begin
  465.   HideMainForm;
  466.   Visible := True;
  467. end;
  468. procedure TVrCustomTrayIcon.ShowMainForm;
  469. begin
  470.   ShowWindow(Application.Handle, SW_RESTORE);
  471.   ShowWindow(Application.MainForm.Handle, SW_RESTORE);
  472. end;
  473. procedure TVrCustomTrayIcon.HideMainForm;
  474. begin
  475.   ShowWindow(Application.Handle, SW_HIDE);
  476.   ShowWindow(Application.MainForm.Handle, SW_HIDE);
  477. end;
  478. procedure TVrCustomTrayIcon.SetPopupMenu(Value: TPopupMenu);
  479. begin
  480.   FPopupMenu := Value;
  481.   Value.FreeNotification(Self);
  482. end;
  483. procedure TVrCustomTrayIcon.Notification(AComponent: TComponent;
  484.   Operation: TOperation);
  485. begin
  486.   inherited Notification(AComponent, Operation);
  487.   if (Operation = opRemove) and (AComponent = FPopupMenu) then
  488.     FPopupMenu := nil;
  489. end;
  490. procedure TVrCustomTrayIcon.IconChanged(Sender: TObject);
  491. begin
  492.   UpdateSystemTray;
  493. end;
  494. procedure TVrCustomTrayIcon.SetIcon(Value: TIcon);
  495. begin
  496.   FIcon.Assign(Value);
  497. end;
  498. procedure TVrCustomTrayIcon.SetVisible(Value: Boolean);
  499. begin
  500.   if FVisible <> Value then
  501.   begin
  502.     FVisible := Value;
  503.     UpdateSystemTray;
  504.   end;
  505. end;
  506. procedure TVrCustomTrayIcon.SetHint(const Value: string);
  507. begin
  508.   if FHint <> Value then
  509.   begin
  510.     FHint := Value;
  511.     UpdateHint;
  512.   end;
  513. end;
  514. procedure TVrCustomTrayIcon.SetShowHint(Value: Boolean);
  515. begin
  516.   if FShowHint <> Value then
  517.   begin
  518.     FShowHint := Value;
  519.     UpdateHint;
  520.   end;
  521. end;
  522. procedure TVrCustomTrayIcon.UpdateHint;
  523. begin
  524.   if (FHint <> '') and FShowHint then
  525.     StrLCopy(FIconData.szTip, PChar(FHint), SizeOf(FIconData.szTip))
  526.   else FIconData.szTip := '';
  527.   UpdateSystemTray;
  528. end;
  529. procedure TVrCustomTrayIcon.UpdateSystemTray;
  530. begin
  531.   if (FIcon.Empty) or
  532.     (csDesigning in ComponentState) then Exit;
  533.   if (not Visible) and (FExists) then
  534.   begin
  535.     Shell_NotifyIcon(NIM_DELETE, @FIconData);
  536.     FExists := false;
  537.     Exit;
  538.   end;
  539.   if FVisible then
  540.   begin
  541.     FIconData.hIcon := FIcon.Handle;
  542.     if (not FExists) then
  543.     begin
  544.       Shell_NotifyIcon(NIM_ADD, @FIconData);
  545.       FExists := True;
  546.     end else Shell_NotifyIcon(NIM_MODIFY, @FIconData);
  547.   end;
  548. end;
  549. procedure TVrCustomTrayIcon.WndProc(var Msg: TMessage);
  550.   function ShiftState: TShiftState;
  551.   begin
  552.     Result := [];
  553.     if GetKeyState(Vk_Shift) < 0 then Include(Result, ssShift);
  554.     if GetKeyState(Vk_Control) < 0 then Include(Result, ssCtrl);
  555.     if GetKeyState(Vk_Menu) < 0 then Include(Result, ssAlt);
  556.   end;
  557. var
  558.   P: TPoint;
  559.   Shift: TShiftState;
  560. begin
  561.   with Msg do
  562.     if Msg = WM_TOOLTRAYNOTIFY then
  563.     begin
  564.       case lParam of
  565.         WM_MOUSEMOVE:
  566.           if Enabled then
  567.           begin
  568.             Shift := ShiftState;
  569.             GetCursorPos(P);
  570.             MouseMove(Shift, P.X, P.Y);
  571.           end;
  572.         WM_LBUTTONDOWN:
  573.           if Enabled then
  574.           begin
  575.             Shift := ShiftState + [ssLeft];
  576.             GetCursorPos(P);
  577.             MouseDown(mbLeft, Shift, P.X, P.Y);
  578.             FClicked := True;
  579.             if FLeftBtnPopup then
  580.             begin
  581.               FClicked := false;
  582.               ShowMenu;
  583.             end;
  584.           end;
  585.         WM_LBUTTONUP:
  586.           if Enabled then
  587.           begin
  588.             Shift := ShiftState + [ssLeft];
  589.             GetCursorPos(P);
  590.             if FClicked then
  591.             begin
  592.               FClicked := False;
  593.               Click;
  594.             end;
  595.             MouseUp(mbLeft, Shift, P.X, P.Y);
  596.           end;
  597.         WM_LBUTTONDBLCLK:
  598.           if Enabled then DblClick;
  599.         WM_RBUTTONDOWN:
  600.           if Enabled then
  601.           begin
  602.             Shift := ShiftState + [ssRight];
  603.             GetCursorPos(P);
  604.             MouseDown(mbRight, Shift, P.X, P.Y);
  605.             ShowMenu;
  606.           end;
  607.         WM_RBUTTONUP:
  608.           if Enabled then
  609.           begin
  610.             Shift := ShiftState + [ssRight];
  611.             GetCursorPos(P);
  612.             MouseUp(mbRight, Shift, P.X, P.Y);
  613.           end;
  614.         WM_RBUTTONDBLCLK:
  615.           if Enabled then DblClick;
  616.         WM_MBUTTONDOWN:
  617.           if Enabled then
  618.           begin
  619.             Shift := ShiftState + [ssMiddle];
  620.             GetCursorPos(P);
  621.             MouseDown(mbMiddle, Shift, P.X, P.Y);
  622.           end;
  623.         WM_MBUTTONUP:
  624.           if Enabled then
  625.           begin
  626.             Shift := ShiftState + [ssMiddle];
  627.             GetCursorPos(P);
  628.             MouseUp(mbMiddle, Shift, P.X, P.Y);
  629.           end;
  630.         WM_MBUTTONDBLCLK:
  631.           if Enabled then DblClick;
  632.       end
  633.     end else Result := DefWindowProc(FIconData.Wnd, Msg, wParam, lParam);
  634. end;
  635. procedure TVrCustomTrayIcon.ShowMenu;
  636. var
  637.   P: TPoint;
  638. begin
  639.   if (PopupMenu <> nil) then
  640.   begin
  641.     GetCursorPos(P);
  642.     Application.ProcessMessages;
  643.     SetForegroundWindow(Application.MainForm.Handle);
  644.     PopupMenu.Popup(P.X, P.Y);
  645.     PostMessage(Application.MainForm.Handle, WM_NULL, 0, 0);
  646.   end;
  647. end;
  648. procedure TVrCustomTrayIcon.Click;
  649. begin
  650.   if Assigned(FOnClick) then FOnClick(Self);
  651. end;
  652. procedure TVrCustomTrayIcon.DblClick;
  653. begin
  654.   if Assigned(FOnDblClick) then FOnDblClick(Self);
  655. end;
  656. procedure TVrCustomTrayIcon.MouseDown(Button: TMouseButton;
  657.   Shift: TShiftState; X, Y: Integer);
  658. begin
  659.   if Assigned(FOnMouseDown) then
  660.     FOnMouseDown(Self, Button, Shift, X, Y);
  661. end;
  662. procedure TVrCustomTrayIcon.MouseUp(Button: TMouseButton;
  663.   Shift: TShiftState; X, Y: Integer);
  664. begin
  665.   if Assigned(FOnMouseUp) then
  666.     FOnMouseUp(Self, Button, Shift, X, Y);
  667. end;
  668. procedure TVrCustomTrayIcon.MouseMove(Shift: TShiftState; X, Y: Integer);
  669. begin
  670.   if Assigned(FOnMouseMove) then
  671.     FOnMouseMove(Self, Shift, X, Y);
  672. end;
  673. { TVrCopyFile }
  674. constructor TVrCopyFile.Create(AOwner: TComponent);
  675. begin
  676.   inherited Create(AOwner);
  677.   FBufferSize := 1024;
  678.   FOverwrite := omAlways;
  679.   FCopyDateTime := True;
  680. end;
  681. destructor TVrCopyFile.Destroy;
  682. begin
  683.   Terminate;
  684.   inherited Destroy;
  685. end;
  686. function TVrCopyFile.CheckExists: Boolean;
  687. var
  688.   SearchRec: TSearchRec;
  689. begin
  690.   Result := FindFirst(ExpandFileName(SourceFile), faAnyFile, SearchRec) = 0;
  691.   try
  692.     if Result then
  693.     begin
  694.       with SearchRec do
  695.         if Assigned(FBeforeOpen) then FBeforeOpen(Self, Size, Date, Time);
  696.     end;
  697.   finally
  698.     SysUtils.FindClose(SearchRec);
  699.   end;
  700. end;
  701. function TVrCopyFile.CheckOverwrite: Boolean;
  702. begin
  703.   Result := (Overwrite = omAlways);
  704.   if not Result then
  705.   begin
  706.     Result := True;
  707.     if FileExists(DestFile) then
  708.       if Assigned(FBeforeOverwrite) then
  709.         FBeforeOverwrite(Self, Result);
  710.   end;
  711. end;
  712. procedure TVrCopyFile.DoProgress(BytesCopied: Integer;
  713.   var Cancel: Boolean);
  714. begin
  715.   if Assigned(FOnProgress) then
  716.     FOnProgress(Self, BytesCopied, Cancel);
  717. end;
  718. procedure TVrCopyFile.Terminate;
  719. begin
  720.   FCancel := True;
  721. end;
  722. procedure TVrCopyFile.DoAfterCopy;
  723. begin
  724.   if Assigned(FAfterCopy) then FAfterCopy(Self);
  725. end;
  726. procedure TVrCopyFile.Execute;
  727. var
  728.   Source: TFileStream;
  729.   Dest: TFileStream;
  730.   Buffer: Pointer;
  731.   BytesRead, ByteCount: Integer;
  732.   CanCopy: Boolean;
  733.   Filedate: Integer;
  734. begin
  735.   FCancel := false;
  736.   ByteCount := 0;
  737.   Buffer := nil;
  738.   ReallocMem(Buffer, FBufferSize);
  739.   try
  740.     CheckExists;
  741.     Source := TFileStream.Create(SourceFile, fmOpenRead);
  742.     try
  743.       FileDate := FileGetDate(Source.Handle);
  744.       CanCopy := CheckOverwrite;
  745.       if CanCopy then
  746.       begin
  747.         Dest := TFileStream.Create(DestFile, fmCreate);
  748.         try
  749.           repeat
  750.             Application.ProcessMessages;
  751.             BytesRead := Source.Read(Buffer^, BufferSize);
  752.             if BytesRead > 0 then Dest.Write(Buffer^, BytesRead);
  753.             Inc(ByteCount, BytesRead);
  754.             DoProgress(ByteCount, FCancel);
  755.           until (BytesRead <> FBufferSize) or (FCancel);
  756.           if CopyDateTime then
  757.             FileSetDate(Dest.Handle, FileDate);
  758.         finally
  759.           Dest.Free;
  760.         end;
  761.       end;
  762.     finally
  763.       Source.Free;
  764.     end;
  765.   finally
  766.     ReallocMem(Buffer, 0);
  767.     DoAfterCopy;
  768.   end;
  769. end;
  770. function AddPathSlash(Path: string): string;
  771. begin
  772.   if (Path <> '') and (Path[Length(Path)] <> '') then
  773.     Path := Path + '';
  774.   Result := Path;
  775. end;
  776. { TVrDirScan }
  777. constructor TVrDirScan.Create(AOwner: TComponent);
  778. begin
  779.   inherited Create(AOwner);
  780.   FMask := '*.*';
  781.   FPath := '';
  782.   FRecursive := True;
  783. end;
  784. destructor TVrDirScan.Destroy;
  785. begin
  786.   FCancel := True;
  787.   while FScanning do
  788.     Application.ProcessMessages;
  789.   inherited Destroy;
  790. end;
  791. procedure TVrDirScan.Cancel;
  792. begin
  793.   FCancel := True;
  794. end;
  795. function TVrDirScan.AbortScan: Boolean;
  796. begin
  797.   Result := (FCancel) or (Application.Terminated);
  798. end;
  799. procedure TVrDirScan.LocateFile(Path: string; SearchRec: TSearchRec);
  800. begin
  801.   if Assigned(FOnLocate) then
  802.     FOnLocate(Self, Path, SearchRec, FCancel);
  803. end;
  804. procedure TVrDirScan.Notify;
  805. begin
  806.   if Assigned(FOnNotify) then
  807.     FOnNotify(Self);
  808. end;
  809. procedure TVrDirScan.Scan(Path, Mask: string; Recurse: Boolean);
  810. var
  811.   NewPath: string;
  812.   SRec: TSearchRec;
  813.   ErrorCode: Integer;
  814. begin
  815.   if AbortScan then Abort;
  816.   try
  817.     ErrorCode := FindFirst(Path + Mask, faAnyFile, SRec);
  818.     while ErrorCode = 0 do
  819.     begin
  820.       if (SRec.Attr and (faDirectory or faVolumeID)) = 0 then
  821.         LocateFile(Path, SRec);
  822.       if AbortScan then Abort;
  823.       ErrorCode := FindNext(SRec);
  824.     end;
  825.   finally
  826.     FindClose(SRec);
  827.   end;
  828.   if Recurse then
  829.   begin
  830.     try
  831.       ErrorCode := FindFirst(Path + '*.*', faDirectory, SRec);
  832.       while ErrorCode = 0 do
  833.       begin
  834.         Application.ProcessMessages;
  835.         if (SRec.Attr and faDirectory) <> 0 then
  836.           if (SRec.Name <> '.') and (SRec.Name <> '..') then
  837.           begin
  838.             NewPath := Path + SRec.Name + '';
  839.             Scan(NewPath, Mask, Recurse);
  840.           end;
  841.         if AbortScan then Abort;
  842.         ErrorCode := FindNext(SRec);
  843.       end;
  844.     finally
  845.       FindClose(SRec);
  846.     end;
  847.   end;
  848.   Application.ProcessMessages;
  849. end;
  850. procedure TVrDirScan.Execute;
  851. var
  852.   ScanPath, ScanMask: string;
  853. begin
  854.   FCancel := false;
  855.   FScanning := True;
  856.   try
  857.     ScanPath := AddPathSlash(FPath);
  858.     ScanMask := Trim(FMask);
  859.     if ScanMask = '' then ScanMask := '*.*';
  860.     Scan(ScanPath, ScanMask, FRecursive);
  861.   finally
  862.     FScanning := false;
  863.   end;
  864.   Notify;
  865. end;
  866. end.