許多應用程式,如IME管理器、殺毒軟體等均在工作列布告區中放置一個有自已特色的表徵圖,該表徵圖讓使用者知道有一個背景程式正在運行,同時也提供了一種修改系統設定的快捷方法,本文將以C++ Builder為例簡述它的實現方法。
開啟一個新工程,將工程檔案取名為test、單元檔案取名為main。在視窗上放置二個按鈕、一個標籤,其屬性按如下設定
組件 屬性 值
Label1 Caption 按OK按鈕...終止程式
OKButton Caption &OKButton
CancelButton Caption &CancelButton
開啟檔案main.h,加入斜體部分聲明(以手工輸入部分均以斜體表示,以下同)
class TForm1 : public TForm
{
__published: // IDE-managed Components
TButton *OKButton;
TButton *CancelButton;
TLabel *Label1;
void __fastcall FormCreate(TObject *Sender);
void __fastcall FormDestroy(TObject *Sender);
void __fastcall OKButtonClick(TObject *Sender);
void __fastcall CancelButtonClick(TObject *Sender);
private: // User declarations
unsigned ugIconMessage;
void AddTray();
void DeleteTray();
protected:
virtual void __fastcall WndProc(Messages::TMessage &Message);
public: // User declarations
__fastcall TForm1(TComponent* Owner);
};
切換到main.cpp,加入以下函數及聲明
#include
#pragma hdrstop
#include
#include "main.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void TForm1::AddTray()//建立工作列布告區表徵圖
{
NOTIFYICONDATA icondata;①
memset(&icondata,0,sizeof(icondata));②
icondata.cbSize=sizeof(icondata);
icondata.hWnd=Handle;③
strncpy(icondata.szTip,"我的工作",sizeof(icondata.szTip));④
Application->Icon->LoadFromFile("e:\\yxg\\map\\system\\ico\\yxg.ico");
icondata.hIcon=Application->Icon->Handle; ⑤
icondata.uCallbackMessage=ugIconMessage;⑥
icondata.uFlags=NIF_MESSAGE|NIF_ICON|NIF_TIP;⑦
Shell_NotifyIcon(NIM_ADD,&icondata);⑧
}
void TForm1::DeleteTray()//刪除工作列布告區表徵圖
{
NOTIFYICONDATA icondata;
memset(&icondata,0,sizeof(icondata));
icondata.cbSize=sizeof(icondata);
icondata.hWnd=Handle;
Shell_NotifyIcon(NIM_DELETE,&icondata); ⑧
}
void __fastcall TForm1::WndProc(Messages::TMessage &Message)
{
if(Message.Msg==ugIconMessage)//如果產生的是與該表徵圖相關的訊息
{
if(Message.LParam==WM_LBUTTONDBLCLK)
Application->Terminate();⑨
if(Message.LParam==WM_RBUTTONDBLCLK)
{
ShowWindow(Application->Handle,SW_SHOW);//
Application->ShowMainForm=true;
Form1->Visible=true; ⑩
}
return;
}
TForm::WndProc(Message);
}