1 Close All
2 New->Service Application
3 NewForm按纽,产生Form2
Unit2.h,Unit2.cpp
4 在Form2上
TImageList *ImageList1;
TPopupMenu *PopupMenu1;
TTrayIcon *TrayIcon1;
5 处理好图标
6 form2上加2个弹出按纽TMenuItem *N1; TMenuItem *N2;
void __fastcall TForm2::N1Click(TObject *Sender)
{
ShowMerssage("显示");
TrayIcon1->Restore();
}
//---------------------------------------------------------------------------
void __fastcall TForm2::N2Click(TObject *Sender)
{
TrayIcon1->Minimize();
}
void __fastcall TForm2::FormClose(TObject *Sender, TCloseAction &Action)
{
Action=caNone;
TrayIcon1->Minimize();
}
7 Unit1.cpp
#include "Unit2.h"
8 __fastcall TService1::TService1(TComponent* Owner)
: TService(Owner)
{
}
中加Interactive=true;
9 Unit1中双击Service1的TService::OnStart事件
///
#ifndef Unit1H
#define Unit1H
#include <SysUtils.hpp>
#include <Classes.hpp>
#include <SvcMgr.hpp>
#include <vcl.h>
//---------------------------------------------------------------------------
class TService1 : public TService
{
__published:
void __fastcall ServiceStart(TService *Sender, bool &Started);
public:
__fastcall TService1(TComponent* Owner);
TServiceController __fastcall GetServiceController(void);
friend void __stdcall ServiceController(unsigned CtrlCode);
};
//---------------------------------------------------------------------------
extern PACKAGE TService1 *Service1;
//---------------------------------------------------------------------------
#endif
#include "Unit1.h"
#include "Unit2.h"
#pragma package(smart_init)
#pragma resource "*.dfm"
TService1 *Service1;
//---------------------------------------------------------------------------
__fastcall TService1::TService1(TComponent* Owner)
: TService(Owner)
{
Interactive=true;
}
//---------------------------------------------------------------------------
TServiceController __fastcall TService1::GetServiceController(void)
{
return (TServiceController) ServiceController;
}
void __stdcall ServiceController(unsigned CtrlCode)
{
Service1->Controller(CtrlCode);
}
//---------------------------------------------------------------------------
void __fastcall TService1::ServiceStart(TService *Sender, bool &Started)
{
Form2->Show();
Form2->TrayIcon1->Visible=true;
Form2->TrayIcon1->Minimize();
}
//---------------------------------------------------------------------------
#ifndef Unit2H
#define Unit2H
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
#include "trayicon.h"
#include <ImgList.hpp>
#include <Menus.hpp>
//---------------------------------------------------------------------------
class TForm2 : public TForm
{
__published:
TImageList *ImageList1;
TPopupMenu *PopupMenu1;
TTrayIcon *TrayIcon1;
TMenuItem *N1;
TMenuItem *N2;
void __fastcall N1Click(TObject *Sender);
void __fastcall N2Click(TObject *Sender);
void __fastcall FormClose(TObject *Sender, TCloseAction &Action);
public:
__fastcall TForm2(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm2 *Form
#include <vcl.h>
#pragma hdrstop
#include "Unit2.h"
#pragma package(smart_init)
#pragma link "trayicon"
#pragma resource "*.dfm"
TForm2 *Form2;
//---------------------------------------------------------------------------
__fastcall TForm2::TForm2(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm2::N1Click(TObject *Sender)
{
ShowMessage("显示");
TrayIcon1->Restore();
}
//---------------------------------------------------------------------------
void __fastcall TForm2::N2Click(TObject *Sender)
{
TrayIcon1->Minimize();
}
//---------------------------------------------------------------------------
void __fastcall TForm2::FormClose(TObject *Sender, TCloseAction &Action)
{
Action=caNone;
TrayIcon1->Minimize();
}
//---------------------------------------------------------------------------
2;
//---------------------------------------------------------------------------
#endif