Главная » Delphi » Win API » Как запуздырить иконку в трей и восстановить её при крахе експлорера.

Как запуздырить иконку в трей и восстановить её при крахе експлорера.

14:16
unit Unit1;
 
interface
 
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, Menus,shellapi;
 
const
WM_MY_TRAYICON = WM_APP + $300;
 
type
TForm1 = class(TForm)
PopupMenu1: TPopupMenu;
Close1: TMenuItem;
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure Close1Click(Sender: TObject);
private
{ Private declarations }
WM_TaskBarRecreate:Integer;
procedure CreateTaskBarIcon;
procedure DeleteTaskBarIcon;
procedure TaskTrayWndProc(var Msg: TMessage); message WM_MY_TRAYICON;
procedure WndProc(var Message: TMessage); override;
 
public
{ Public declarations }
end;
 
var
Form1: TForm1;
 
implementation
 
{$R *.dfm}
 
procedure TForm1.CreateTaskBarIcon;
var
NotifyData: TNotifyIconData;
begin
with NotifyData do
begin
cbSize := SizeOf(TNotifyIconData);
Wnd := handle;
uID := 0;
uFlags := NIF_MESSAGE or NIF_ICON or NIF_TIP;
szTip := 'Traybar Tip';
hIcon := Application.Icon.Handle;
uCallbackMessage := WM_MY_TRAYICON;
end;
Shell_NotifyIcon( NIM_ADD, @NotifyData );
end;
 
procedure TForm1.DeleteTaskBarIcon;
var
NotifyData: TNotifyIconData;
begin
with NotifyData do
begin
cbSize := SizeOf(TNotifyIconData);
Wnd := handle;
uID := 0;
end;
Shell_NotifyIcon( NIM_DELETE, @NotifyData );
end;
 
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
if not Application.Terminated then
begin
ShowWindow(Application.Handle, SW_HIDE);
Visible := False;
Action := caNone;
end;
end;
 
procedure TForm1.FormCreate(Sender: TObject);
begin
WM_TaskBarRecreate := RegisterWindowMessage('TaskbarCreated');
CreateTaskBarIcon;
ShowWindow(Application.Handle,SW_HIDE);
Application.ShowMainForm := False;
end;
 
procedure TForm1.FormDestroy(Sender: TObject);
begin
DeleteTaskBarIcon;
end;
 
procedure TForm1.TaskTrayWndProc(var Msg: TMessage);
var
ps: TPoint;
begin
Case Msg.LParam of
WM_LBUTTONDBLCLK:
begin
Visible := true;
ShowWindow(Application.Handle,SW_SHOW);
end;
WM_RBUTTONUP:
begin
GetCursorPos(ps);
SetForegroundWindow(Handle);
PopupMenu1.Popup(ps.x,ps.y);
PostMessage(Handle, WM_NULL, 0,0);
end;
end;
end;
 
procedure TForm1.Close1Click(Sender: TObject);
begin
Application.Terminate;
end;
 
procedure TForm1.WndProc(var Message: TMessage);
begin
inherited;
if Message.Msg=WM_TaskBarRecreate then
CreateTaskBarIcon;
end;
 
end.