我写了一个系统服务的程序,而且还加上了系统托盘,可是为什么系统启动进入桌面后不会显示出托盘呢,如果我把服务关了,再启动服务就会出现托盘?我把程序段贴上大家帮我看看!!!
procedure TAppService.ServiceStart(Sender: TService; var Started: Boolean);
begin
Started := True;
Application.CreateForm(TfrmAppServerState, frmAppServerState);
frmAppServerState.Hide;
iconTrayIcon:=TIcon.Create;
iconTrayIcon.Handle:=LoadIcon(HInstance,'MainIcon');
if iconTrayIcon.Handle=0 then
begin
ShowMessage('无法打开图标');
end; with TrayIcon do
begin
cbSize:=sizeof(TNotifyIconData);
Wnd:=frmAppServerState.Handle;
uID:=1;
uFlags:=NIF_MESSAGE or NIF_ICON or NIF_TIP;
uCallbackMessage:=0;
hIcon:=iconTrayIcon.Handle;
szTip:='当前客户端连接数:0';
end;
Shell_NotifyIcon(NIM_ADD,@TrayIcon);
SetWindowLong(frmAppServerState.Handle, GWL_EXSTYLE,WS_EX_TOOLWINDOW);
end;procedure TAppService.ServiceStop(Sender: TService; var Stopped: Boolean);
begin
Shell_NotifyIcon(NIM_DELETE,@TrayIcon);
Stopped := True;
frmAppServerState.Free;
end;procedure TAppService.ServicePause(Sender: TService; var Paused: Boolean);
begin
Paused:= True;
end;procedure TAppService.ServiceShutdown(Sender: TService);
begin
Shell_NotifyIcon(NIM_DELETE,@TrayIcon);
frmAppServerState.Free;
Status:=csStopped;
ReportStatus();
end;
procedure TAppService.ServiceStart(Sender: TService; var Started: Boolean);
begin
Started := True;
Application.CreateForm(TfrmAppServerState, frmAppServerState);
frmAppServerState.Hide;
iconTrayIcon:=TIcon.Create;
iconTrayIcon.Handle:=LoadIcon(HInstance,'MainIcon');
if iconTrayIcon.Handle=0 then
begin
ShowMessage('无法打开图标');
end; with TrayIcon do
begin
cbSize:=sizeof(TNotifyIconData);
Wnd:=frmAppServerState.Handle;
uID:=1;
uFlags:=NIF_MESSAGE or NIF_ICON or NIF_TIP;
uCallbackMessage:=0;
hIcon:=iconTrayIcon.Handle;
szTip:='当前客户端连接数:0';
end;
Shell_NotifyIcon(NIM_ADD,@TrayIcon);
SetWindowLong(frmAppServerState.Handle, GWL_EXSTYLE,WS_EX_TOOLWINDOW);
end;procedure TAppService.ServiceStop(Sender: TService; var Stopped: Boolean);
begin
Shell_NotifyIcon(NIM_DELETE,@TrayIcon);
Stopped := True;
frmAppServerState.Free;
end;procedure TAppService.ServicePause(Sender: TService; var Paused: Boolean);
begin
Paused:= True;
end;procedure TAppService.ServiceShutdown(Sender: TService);
begin
Shell_NotifyIcon(NIM_DELETE,@TrayIcon);
frmAppServerState.Free;
Status:=csStopped;
ReportStatus();
end;
Windows, Messages, SysUtils, Classes, Graphics, Controls, SvcMgr, Dialogs, Unit_FrmMain;type
TDelphiService = class(TService)
procedure ServiceContinue(Sender: TService; var Continued: Boolean);
procedure ServiceExecute(Sender: TService);
procedure ServicePause(Sender: TService; var Paused: Boolean);
procedure ServiceShutdown(Sender: TService);
procedure ServiceStart(Sender: TService; var Started: Boolean);
procedure ServiceStop(Sender: TService; var Stopped: Boolean);
private
{ Private declarations }
public
function GetServiceController: TServiceController; override;
{ Public declarations }
end;var
DelphiService: TDelphiService;
FrmMain: TFrmMain;
implementation{$R *.DFM}procedure ServiceController(CtrlCode: DWord); stdcall;
begin
DelphiService.Controller(CtrlCode);
end;function TDelphiService.GetServiceController: TServiceController;
begin
Result := ServiceController;
end;procedure TDelphiService.ServiceContinue(Sender: TService;
var Continued: Boolean);
begin
while not Terminated do
begin
Sleep(10);
ServiceThread.ProcessRequests(False);
end;
end;procedure TDelphiService.ServiceExecute(Sender: TService);
begin
while not Terminated do
begin
Sleep(10);
ServiceThread.ProcessRequests(False);
end;
end;procedure TDelphiService.ServicePause(Sender: TService;
var Paused: Boolean);
begin
Paused := True;
end;procedure TDelphiService.ServiceShutdown(Sender: TService);
begin
gbCanClose := true;
FrmMain.Free;
Status := csStopped;
ReportStatus();
end;procedure TDelphiService.ServiceStart(Sender: TService;
var Started: Boolean);
begin
Started := True;
Svcmgr.Application.CreateForm(TFrmMain, FrmMain);
gbCanClose := False;
FrmMain.Hide;
end;procedure TDelphiService.ServiceStop(Sender: TService;
var Stopped: Boolean);
begin
Stopped := True;
gbCanClose := True;
FrmMain.Free;
end;end.
Windows, Messages, SysUtils, Variants, Classes, ShellApi, Graphics, Controls, Forms,
Dialogs, ExtCtrls, StdCtrls;const
WM_TrayIcon = WM_USER + 1234;
type
TFrmMain = class(TForm)
Timer1: TTimer;
Button1: TButton;
procedure FormCreate(Sender: TObject);
procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
procedure FormDestroy(Sender: TObject);
procedure Timer1Timer(Sender: TObject);
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
IconData: TNotifyIconData;
procedure AddIconToTray;
procedure DelIconFromTray;
procedure TrayIconMessage(var Msg: TMessage); message WM_TrayIcon;
procedure SysButtonMsg(var Msg: TMessage); message WM_SYSCOMMAND;
public
{ Public declarations }
end;var
FrmMain: TFrmMain;
gbCanClose: Boolean;
implementation{$R *.dfm}procedure TFrmMain.FormCreate(Sender: TObject);
begin
FormStyle := fsStayOnTop;
SetWindowLong(Application.Handle, GWL_EXSTYLE, WS_EX_TOOLWINDOW);
gbCanClose := False;
Timer1.Interval := 1000;
Timer1.Enabled := True;
end;procedure TFrmMain.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
begin
CanClose := gbCanClose;
if not CanClose then
begin
Hide;
end;
end;procedure TFrmMain.FormDestroy(Sender: TObject);
begin
Timer1.Enabled := False;
DelIconFromTray;
end;procedure TFrmMain.AddIconToTray;
begin
ZeroMemory(@IconData, SizeOf(TNotifyIconData));
IconData.cbSize := SizeOf(TNotifyIconData);
IconData.Wnd := Handle;
IconData.uID := 1;
IconData.uFlags := NIF_MESSAGE or NIF_ICON or NIF_TIP;
IconData.uCallbackMessage := WM_TrayIcon;
IconData.hIcon := Application.Icon.Handle;
IconData.szTip := Delphi服务演示程序;
Shell_NotifyIcon(NIM_ADD, @IconData);
end;procedure TFrmMain.DelIconFromTray;
begin
Shell_NotifyIcon(NIM_DELETE, @IconData);
end;procedure TFrmMain.SysButtonMsg(var Msg: TMessage);
begin
if (Msg.wParam = SC_CLOSE) or
(Msg.wParam = SC_MINIMIZE) then Hide
else inherited; // 执行默认动作
end;procedure TFrmMain.TrayIconMessage(var Msg: TMessage);
begin
if (Msg.LParam = WM_LBUTTONDBLCLK) then Show();
end;procedure TFrmMain.Timer1Timer(Sender: TObject);
begin
AddIconToTray;
end;procedure SendHokKey;stdcall;
var
HDesk_WL: HDESK;
begin
HDesk_WL := OpenDesktop (Winlogon, 0, False, DESKTOP_JOURNALPLAYBACK);
if (HDesk_WL <> 0) then
if (SetThreadDesktop (HDesk_WL) = True) then
PostMessage(HWND_BROADCAST, WM_HOTKEY, 0, MAKELONG (MOD_ALT or MOD_CONTROL, VK_DELETE));
end;procedure TFrmMain.Button1Click(Sender: TObject);
var
dwThreadID : DWORD;
begin
CreateThread(nil, 0, @SendHokKey, nil, 0, dwThreadID);
end;end.
HDesk_WL := OpenDesktop (Winlogon, 0, False, DESKTOP_JOURNALPLAYBACK);
if (HDesk_WL <> 0) then 你要不断循环判断能不能打开 desktop, 如果可以,再将 icon 加到 系统托盘