各位朋友,小弟原来有一程序,可以实现启动时变成托盘图标在右下角出现,但现在将这个程序注册成系统的一个服务后,虽然该服务可以启动,进程按Ctrl+Alt+Del键可以在任务栏出现,但在右下角就不能出现托盘图标了,自己试过在注册表将该程序加载进去,也是不行,不知道是什么原因,请各位朋友赐教,多谢!!!另外,怎样才可以保持该服务不会被中止,一直在启动状态呢?以下是窗体文件和服务的源码,请各位朋友帮忙指出,多谢!!!
unit u_main;//窗体interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, shellapi, menus;
const
WM_TRAYNOTIFY = 2000;
MY_TRAY_ICON = 1000;
type
TForm1 = class(TForm)
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
private
{ Private declarations }
public
procedure OnMin(Sender: TObject);
procedure WMTrayNotify(var Msg: TMessage); message WM_TRAYNOTIFY; { Public declarations }
end;var
Form1: TForm1;
nid: TNOTIFYICONDATA;
implementation{$R *.dfm}procedure TForm1.FormCreate(Sender: TObject);
begin
Application.OnMinimize := OnMin;
end;procedure TForm1.OnMin(Sender: TObject);
begin
nid.cbSize := Sizeof(TNOTIFYICONDATA);
nid.Wnd := Handle;
nid.uID := MY_TRAY_ICON;
nid.uFlags := NIF_TIP or NIF_ICON or NIF_MESSAGE;
nid.hIcon := Application.Icon.Handle;
nid.szTip := '我的程序';
nid.uCallbackMessage := WM_TRAYNOTIFY;
Shell_NotifyIcon(NIM_ADD, @nid);
ShowWindow(Application.Handle, SW_HIDE);end;procedure TForm1.WMTrayNotify(var Msg: TMessage);
var
p: TPoint;
begin
if Msg.LParam = WM_LBUTTONDBLCLK then
begin
nid.cbSize := Sizeof(TNOTIFYICONDATA);
nid.Wnd := Application.Handle;
nid.uID := MY_TRAY_ICON;
nid.uFlags := NIF_TIP or NIF_ICON or NIF_MESSAGE;
nid.uCallbackMessage := WM_TRAYNOTIFY;
nid.szTip := '';
Shell_NotifyIcon(NIM_MODIFY, @nid);
ShowWindow(Application.Handle, SW_SHOW);
Application.Restore;
end
else if Msg.LParam = WM_RBUTTONUP then
begin
GetCursorpos(p);
end;
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
Shell_NotifyIcon(NIM_DELETE, @nid);end;end.unit u_svr; //服务interfaceuses
Windows, Messages, SysUtils, Classes, Graphics, Controls, SvcMgr, Dialogs;type
Tfrm_svr = class(TService)
private
{ Private declarations }
public
function GetServiceController: TServiceController; override;
{ Public declarations }
end;var
frm_svr: Tfrm_svr;implementation{$R *.DFM}procedure ServiceController(CtrlCode: DWord); stdcall;
begin
frm_svr.Controller(CtrlCode);
end;function Tfrm_svr.GetServiceController: TServiceController;
begin
Result := ServiceController;
end;end.
unit u_main;//窗体interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, shellapi, menus;
const
WM_TRAYNOTIFY = 2000;
MY_TRAY_ICON = 1000;
type
TForm1 = class(TForm)
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
private
{ Private declarations }
public
procedure OnMin(Sender: TObject);
procedure WMTrayNotify(var Msg: TMessage); message WM_TRAYNOTIFY; { Public declarations }
end;var
Form1: TForm1;
nid: TNOTIFYICONDATA;
implementation{$R *.dfm}procedure TForm1.FormCreate(Sender: TObject);
begin
Application.OnMinimize := OnMin;
end;procedure TForm1.OnMin(Sender: TObject);
begin
nid.cbSize := Sizeof(TNOTIFYICONDATA);
nid.Wnd := Handle;
nid.uID := MY_TRAY_ICON;
nid.uFlags := NIF_TIP or NIF_ICON or NIF_MESSAGE;
nid.hIcon := Application.Icon.Handle;
nid.szTip := '我的程序';
nid.uCallbackMessage := WM_TRAYNOTIFY;
Shell_NotifyIcon(NIM_ADD, @nid);
ShowWindow(Application.Handle, SW_HIDE);end;procedure TForm1.WMTrayNotify(var Msg: TMessage);
var
p: TPoint;
begin
if Msg.LParam = WM_LBUTTONDBLCLK then
begin
nid.cbSize := Sizeof(TNOTIFYICONDATA);
nid.Wnd := Application.Handle;
nid.uID := MY_TRAY_ICON;
nid.uFlags := NIF_TIP or NIF_ICON or NIF_MESSAGE;
nid.uCallbackMessage := WM_TRAYNOTIFY;
nid.szTip := '';
Shell_NotifyIcon(NIM_MODIFY, @nid);
ShowWindow(Application.Handle, SW_SHOW);
Application.Restore;
end
else if Msg.LParam = WM_RBUTTONUP then
begin
GetCursorpos(p);
end;
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
Shell_NotifyIcon(NIM_DELETE, @nid);end;end.unit u_svr; //服务interfaceuses
Windows, Messages, SysUtils, Classes, Graphics, Controls, SvcMgr, Dialogs;type
Tfrm_svr = class(TService)
private
{ Private declarations }
public
function GetServiceController: TServiceController; override;
{ Public declarations }
end;var
frm_svr: Tfrm_svr;implementation{$R *.DFM}procedure ServiceController(CtrlCode: DWord); stdcall;
begin
frm_svr.Controller(CtrlCode);
end;function Tfrm_svr.GetServiceController: TServiceController;
begin
Result := ServiceController;
end;end.
begin
Application.OnMinimize := OnMin;
end;
-------------------------------------------
//改为试试,因为OnMinimize根本就就执行呀:
procedure TForm1.FormCreate(Sender: TObject);
begin
OnMin;
end;