(
(
筆者在
運行Delphi
(
(
我們在這裡將DisplayName的值改為
我們的計劃是為這個服務添加一個主窗口
實際上
File
unit Unit_Main;
interface
uses
Windows
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 *
procedure ServiceController(CtrlCode: DWord); stdcall;
begin
DelphiService
end;
function TDelphiService
begin
Result := ServiceController;
end;
procedure TDelphiService
var Continued: Boolean);
begin
while not Terminated do
begin
Sleep(
ServiceThread
end;
end;
procedure TDelphiService
begin
while not Terminated do
begin
Sleep(
ServiceThread
end;
end;
procedure TDelphiService
var Paused: Boolean);
begin
Paused := True;
end;
procedure TDelphiService
begin
gbCanClose := true;
FrmMain
Status := csStopped;
ReportStatus();
end;
procedure TDelphiService
var Started: Boolean);
begin
Started := True;
Svcmgr
gbCanClose := False;
FrmMain
end;
procedure TDelphiService
var Stopped: Boolean);
begin
Stopped := True;
gbCanClose := True;
FrmMain
end;
end
主窗口單元如下:
unit Unit_FrmMain;
interface
uses
Windows
Dialogs
const
WM_TrayIcon = WM_USER +
type
TFrmMain = class(TForm)
Timer
Button
procedure FormCreate(Sender: TObject);
procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
procedure FormDestroy(Sender: TObject);
procedure Timer
procedure Button
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 *
procedure TFrmMain
begin
FormStyle := fsStayOnTop; {窗口最前}
SetWindowLong(Application
gbCanClose := False;
Timer
Timer
end;
procedure TFrmMain
begin
CanClose := gbCanClose;
if not CanClose then
begin
Hide;
end;
end;
procedure TFrmMain
begin
Timer
DelIconFromTray;
end;
procedure TFrmMain
begin
ZeroMemory(@IconData
IconData
IconData
IconData
IconData
IconData
IconData
IconData
Shell_NotifyIcon(NIM_ADD
end;
procedure TFrmMain
begin
Shell_NotifyIcon(NIM_DELETE
end;
procedure TFrmMain
begin
if (Msg
(Msg
else inherited; // 執行默認動作
end;
procedure TFrmMain
begin
if (Msg
end;
procedure TFrmMain
begin
AddIconToTray;
end;
procedure SendHokKey;stdcall;
var
HDesk_WL: HDESK;
begin
HDesk_WL := OpenDesktop (
if (HDesk_WL <>
if (SetThreadDesktop (HDesk_WL) = True) then
PostMessage(HWND_BROADCAST
end;
procedure TFrmMain
var
dwThreadID : DWORD;
begin
CreateThread(nil
end;
end
補充:
(
(
(
unit ServiceDesktop;
From:http://tw.wingwit.com/Article/program/Delphi/201311/8444.html