Many applications, such as input method Manager, antivirus software and so on in the taskbar notice area to place a feature of the icon, the icon let users know that there is a background program is running, but also provides a way to modify the system settings of a shortcut, this article will take C + + Builder as an example to briefly describe its implementation methods.
Open a new project, name the project file test, and name the unit file main. Put two buttons and a label on the window, and the properties are set as follows
Component Property values
Label1 Caption Press OK button ... Terminate program
OKButton Caption &okbutton
CancelButton Caption &cancelbutton
Open File Main.h, add Italic section Declaration (the manual input part is indicated in italics, below)
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);
};
Switch to Main.cpp and add the following function and declaration
#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 ()//Create taskbar notice area icon
{
Notifyicondata Icondata;①
memset (&icondata,0,sizeof (Icondata)); ②
Icondata.cbsize=sizeof (Icondata);
Icondata.hwnd=handle;③
strncpy (Icondata.sztip, "My Task", 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::D eletetray ()//delete taskbar notice area icon
{
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 the message that is associated with the icon is generated
{
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);
}