home *** CD-ROM | disk | FTP | other *** search
- unit TDTrayIcon;
-
- interface
-
- uses
- Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs;
-
- const
- WM_MYTRAYICON=WM_USER + 1;
-
- type
- NOTIFYICONDATA=record
- cbSize: DWORD;
- HWND: hWnd;
- uID: UINT;
- uFlags: UINT;
- uCallbackMessage :UINT;
- hIcon: HICON ;
- szTip: String[64];
- end;
-
- TTDTrayIcon = class(TComponent)
- private
- FActive: Boolean;
- FIcon: TIcon;
- FHint: String;
- FOnLeftUp: TNotifyEvent;
- FOnCenterUp: TNotifyEvent;
- FOnRightDown: TNotifyEvent;
- FOnDblClickRight: TNotifyEvent;
- FOnLeftDown: TNotifyEvent;
- FOnDblClickCenter: TNotifyEvent;
- FOnRightUp: TNotifyEvent;
- FOnCenterDown: TNotifyEvent;
- FOnDblClickLeft: TNotifyEvent;
- procedure SetActive(const Value: Boolean);
- procedure SetIcon(const Value: TIcon);
- procedure SetHint(const Value: String);
- { Private declarations }
- protected
- { Protected declarations }
- NotifyIconInfo: NOTIFYICONDATA;
- procedure CreateNotifyIconData(var TrayIconInfo: NOTIFYICONDATA);
- procedure WMMYTRAYICON (var Mes: TMessage); message WM_MYTRAYICON;
- public
- { Public declarations }
- constructor create(AOwner: TComponent); override;
- destructor destroy; override;
- published
- { Published declarations }
- property Active: Boolean read FActive write SetActive;
- property Icon: TIcon read FIcon write SetIcon;
- property Hint: String read FHint write SetHint;
- //Definizione degli eventi
- property OnLeftDown: TNotifyEvent read FOnLeftDown write FOnLeftDown;
- property OnRightDown: TNotifyEvent read FOnRightDown write FOnRightDown;
- property OnCenterDown: TNotifyEvent read FOnCenterDown write FOnCenterDown;
- property OnLeftUp: TNotifyEvent read FOnLeftUp write FOnLeftUp;
- property OnRightUp: TNotifyEvent read FOnRightUp write FOnRightUp;
- property OnCenterUp: TNotifyEvent read FOnCenterUp write FOnCenterUp;
- property OnDblClickLeft: TNotifyEvent read FOnDblClickLeft write FOnDblClickLeft;
- property OnDblClickRight: TNotifyEvent read FOnDblClickRight write FOnDblClickRight;
- property OnDblClickCenter: TNotifyEvent read FOnDblClickCenter write FOnDblClickCenter;
-
- end;
-
- procedure Register;
-
- implementation
-
- uses ShellAPI;
-
- procedure Register;
- begin
- RegisterComponents('TDSoft', [TTDTrayIcon]);
- end;
-
- { TTDTrayIcon }
-
- constructor TTDTrayIcon.create(AOwner: TComponent);
- begin
- inherited;
- if not (AOwner is TForm) then
- begin
- ShowMessage('Parent Control is not a form');
- Free;
- end;
- FIcon:=TIcon.create;
- FActive:=False;
- end;
-
- procedure TTDTrayIcon.CreateNotifyIconData(
- var TrayIconInfo: NOTIFYICONDATA);
- begin
- with TrayIconInfo do
- begin
- cbSize:=sizeOf(TrayIconInfo);
- HWND:=Application.handle;
- uID:=1;
- uCallbackMessage:=WM_MYTRAYICON;
- uFlags:=NIF_ICON or NIF_MESSAGE or NIF_TIP;
- szTip:=FHint;
- hIcon:=FIcon.Handle;
- end;
-
- end;
-
- destructor TTDTrayIcon.destroy;
- begin
- Active:=False;
- FreeAndNil(FIcon);
- inherited;
- end;
-
- procedure TTDTrayIcon.SetActive(const Value: Boolean);
- begin
- FActive := Value;
-
- if not(csDesigning in ComponentState) then
-
- if FActive then
- with NotifyIconInfo do
- begin
- cbSize:=sizeOf(NotifyIconInfo);
- HWND:=(Owner as TForm).Handle;
- uID:=1;
- uCallbackMessage:=WM_MYTRAYICON;
- uFlags:=NIF_ICON or NIF_MESSAGE or NIF_TIP;
- szTip:=FHint;
- hIcon:=FIcon.Handle;
- Shell_NotifyIcon(NIM_ADD,@NotifyIconInfo)
- end
- else
- Shell_NotifyIcon(NIM_DELETE,@NotifyIconInfo);
-
- end;
-
- procedure TTDTrayIcon.SetHint(const Value: String);
- begin
- FHint := Copy(Value,1,64);
- end;
-
- procedure TTDTrayIcon.SetIcon(const Value: TIcon);
- begin
- if FActive then
- SetActive(False);
-
- FIcon.Assign(Value);
-
- SetActive(True);
-
-
- end;
-
- procedure TTDTrayIcon.WMMYTRAYICON(var Mes: TMessage);
- begin
- ShowMessage('Entrato');
- case Mes.LParam of
- WM_LBUTTONDOWN:
- if Assigned(FOnLeftDown) then FOnLeftDown(self);
- WM_LBUTTONUP:
- if Assigned(FOnLeftUp) then FOnLeftUp(self);
- WM_RBUTTONDOWN:
- if Assigned(FOnRightDown) then FOnRightDown(self);
- WM_RBUTTONUP:
- if Assigned(FOnRightDown) then FOnRightDown(self);
- WM_MBUTTONDOWN:
- if Assigned(FOnCenterDown) then FOnCenterDown(self);
- WM_MBUTTONUP:
- if Assigned(FOnCenterUp) then FOnCenterUp(self);
- WM_MBUTTONDBLCLK:
- if Assigned(FOnDblClickCenter) then FOnDblClickCenter(self);
- WM_LBUTTONDBLCLK:
- if Assigned(FOnDblClickLeft) then FOnDblClickLeft(self);
- WM_RBUTTONDBLCLK:
- if Assigned(FOnDblClickRight) then FOnDblClickRight(self);
- end;
-
- end;
-
- end.
-