这个问题不知被问了多少次啦!给你一个托盘组件的源代码。
unit TrayIcon;interfaceuses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,
Dialogs, Menus, ShellAPI;type
TDCPTrayIcon = class(TComponent)
private
{ Private declarations }
FNotificationHandle: HWnd;
FTrayIcon: TIcon;
FShowHint: Boolean;
FHint: string;
FActive: Boolean;
FCurrentlyActive: Boolean;
FOnDblClick: TNotifyEvent;
FPopupMenu: TPopupMenu;
procedure NotificationWndProc(var Message : TMessage);
procedure SetShowHint(const Value: Boolean);
procedure SetHint(const Value: string);
procedure SetTrayIcon(const Value: TIcon);
procedure SetActive(const Value: Boolean);
procedure SetPopupMenu(const Value: TPopupMenu);
protected
{ Protected declarations }
procedure DoDblClick;
procedure Loaded; override;
procedure OnAppMinimize(Sender: TObject);
function SendTrayMessage(AMessage: DWORD): Boolean;
public
{ Public declarations }
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
procedure RemoveTrayIcon;
procedure RestoreApp;
procedure ShowTrayIcon;
procedure RemoveFromTaskbar;
procedure ShowInTaskbar;
published
{ Published declarations }
property Active: Boolean read FActive write SetActive;
property Hint: string read FHint write SetHint;
property OnDblClick: TNotifyEvent read FOnDblClick write FOnDblClick;
property PopupMenu: TPopupMenu read FPopupMenu write SetPopupMenu;
property ShowHint: Boolean read FShowHint write SetShowHint;
property TrayIcon: TIcon read FTrayIcon write SetTrayIcon;
end;procedure Register;implementationconst
ID_TRAYICON = 1;
UWM_TRAYICON = WM_USER + 1;procedure Register;
begin
RegisterComponents('YourPallateName', [TDCPTrayIcon]);
end;{ TDCPTrayIcon }constructor TDCPTrayIcon.Create(AOwner: TComponent);
begin
inherited Create(AOwner); FNotificationHandle := AllocateHWnd(NotificationWndProc);
FTrayIcon := TIcon.Create; Application.OnMinimize := OnAppMinimize;
end;destructor TDCPTrayIcon.Destroy;
begin
if FCurrentlyActive then
RemoveTrayIcon; FTrayIcon.Free; if FNotificationHandle <> 0 then
DeallocateHWnd(FNotificationHandle); inherited Destroy;
end;procedure TDCPTrayIcon.DoDblClick;
begin
if Assigned(OnDblClick) then
OnDblClick(self);
end;procedure TDCPTrayIcon.Loaded;
begin
inherited Loaded; if not (csDesigning in ComponentState) then begin
if FTrayIcon.Handle = 0 then
FTrayIcon.Assign(Application.Icon);
end; SetTrayIcon(FTrayIcon); if FActive then
ShowTrayIcon;
end;procedure TDCPTrayIcon.NotificationWndProc(var Message: TMessage);
var
Pt: TPoint;
begin
if Message.Msg = UWM_TRAYICON then begin
case Message.lParam of
WM_LBUTTONDBLCLK:
DoDblClick; WM_RBUTTONDOWN:
if Assigned(FPopupMenu) then begin
SetForegroundWindow(FNotificationHandle);
GetCursorPos(Pt);
FPopupMenu.Popup(Pt.X, Pt.Y);
end;
end;
end;
end;procedure TDCPTrayIcon.OnAppMinimize(Sender: TObject);
begin
if FCurrentlyActive then
RemoveFromTaskbar;
end;procedure TDCPTrayIcon.RemoveFromTaskbar;
begin
if not (csDesigning in ComponentState) then begin
ShowWindow(Application.Handle, SW_HIDE);
ShowWindow(Application.MainForm.Handle, SW_HIDE);
end;
end;procedure TDCPTrayIcon.RemoveTrayIcon;
begin
if FCurrentlyActive then
if SendTrayMessage(NIM_DELETE) then begin
FCurrentlyActive := False; ShowInTaskbar;
end;
end;procedure TDCPTrayIcon.RestoreApp;
begin
ShowInTaskbar;
SetForegroundWindow(Application.MainForm.Handle);
end;function TDCPTrayIcon.SendTrayMessage(AMessage: DWORD): Boolean;
var
Flags: Integer;
NotifyIconData: TNotifyIconData;
begin
Result := True;
if csDesigning in ComponentState then
exit; Flags := NIF_MESSAGE or NIF_ICON;
if FShowHint then
Flags := Flags or NIF_TIP; FillChar(NotifyIconData, SizeOf(NotifyIconData), 0);
with NotifyIconData do begin
cbSize := SizeOf(TNotifyIconData);
Wnd := FNotificationHandle;
uID := ID_TRAYICON;
uFlags := Flags;
uCallBackMessage := UWM_TRAYICON;
hIcon := FTrayIcon.Handle;
StrLCopy(szTip, PChar(FHint), SizeOf(szTip));
end; Result := Shell_NotifyIcon(AMessage, @NotifyIconData);
end;procedure TDCPTrayIcon.SetActive(const Value: Boolean);
begin
FActive := Value; if FActive then
ShowTrayIcon
else
RemoveTrayIcon;
end;procedure TDCPTrayIcon.SetHint(const Value: string);
begin
FHint := Value; SendTrayMessage(NIM_MODIFY);
end;procedure TDCPTrayIcon.SetPopupMenu(const Value: TPopupMenu);
begin
FPopupMenu := Value;
end;procedure TDCPTrayIcon.SetShowHint(const Value: Boolean);
begin
FShowHint := Value; SendTrayMessage(NIM_MODIFY);
end;procedure TDCPTrayIcon.SetTrayIcon(const Value: TIcon);
begin
FTrayIcon.Assign(Value); SendTrayMessage(NIM_MODIFY);
end;procedure TDCPTrayIcon.ShowInTaskbar;
begin
if not (csDesigning in ComponentState) then begin
if Application.MainForm <> nil then
ShowWindow(Application.MainForm.Handle, SW_SHOWNORMAL);
ShowWindow(Application.Handle, SW_SHOWNORMAL);
end;
end;procedure TDCPTrayIcon.ShowTrayIcon;
begin
if SendTrayMessage(NIM_ADD) then
FCurrentlyActive := True;
end;end.
由菜单Component->Install Component...导入该新组件。
ShowWindow(FindWindow('Shell_TrayWnd',nil),SW_Show); // 显示;
http://www.csdn.net/expert/topicview1.asp
有时间帮忙看一下好吗