Tray (pallet) is a special area on the Windows9x task bar, its technical name is "taskbar Bulletin area", some software (such as Kingsoft Ⅲ) run will put an icon on the tray, so that users can know at a glance that the program is running in the background, to activate it is also very easy, It's usually easy to just click on this icon.
Tray programming is special, but not difficult, mainly including icons, tooltips and messages, such as three aspects, it is part of Shell programming. SHELLAPI provides a Shell-notifyicon function that allows you to add, remove, or modify the icons in the tray, and after placing the icon on the tray, the Windows shell is responsible for notifying the application of mouse events that occur on the icon. The Shell-notifyicon function is defined as follows:
Winshellapi BOOL winapi Shell-notifyicon (DWORD dwmessage,pnotifyicondata Pnid);
Dwmessage represents the action to be completed: Nim-add (add icon), Nim-delete (delete icon), nim-modify (modify icon or hint text), Pnid is a pointer to the NOTIFYICONDATA structure, which is defined as follows:
typedef struct —NOTIFYICONDATA{
DWORD cbSize;//结构所占的字节数,必须用结构的大小来初始化。
HWND hWnd;//接受Tray图标消息的窗口句柄
UINT uID;//由应用程序定义的图标ID
UINT uFlags;//用来鉴别那些需要改变其值的域,NIF_ICON表示hIcon有效,可用来修改图标,NIF_MESSAGE表示uCallbackMessage有效,用来定义消息,NIF—TIP表示szTip参数有效,可修改工具提示。
UINT uCallbackMessage;//应用程序定义的消息
HICON hIcon;//Tray图标的句柄
char szTip[64];//工具提示的文本
}NOTIFYICONDATA;
Below we have a concrete example to illustrate the implementation method, the program will not display the main form when running, only add an icon on the tray, double-click the icon to close the program.
The tray area appears as follows when the program runs:
Create a new project, place a timer control on the form. Open unit1.h file, add header file Description #include <shellapi.h>, add some data members and method declarations in the private section defined in TForm1:
message defined by unsigned int iconmessage;//
void Addtrayicon ()//Add icon to Tray
void Removetrayicon ()//Remove icon from tray
Because you want to increase the processing of custom messages, you must overload the window procedure function WndProc and add the protected segment in the definition of TForm1: Virtual Void--fastcall WndProc (messages::tmessage & message);
Define the appropriate member functions in Unit1.cpp:
void TForm1::AddTrayIcon()
{
NOTIFYICONDATA icondata;
memset(&icondata,0,sizeof(icondata));
//将结构icondata的各域初始化为0
icondata.cbSize=sizeof(icondata);
icondata.hWnd=Handle;
strncpy(icondata.szTip,″未知状态″,sizeof(icondata.szTip));
icondata.hIcon=Application->Icon->Handle;
icondata.uCallbackMessage=iconmessage;
icondata.uFlags=NIF—MESSAGE|NIF—ICON|NIF—TIP;
Shell—NotifyIcon(NIM—ADD,&icondata);
}
void TForm1::RemoveTrayIcon()
{
NOTIFYICONDATA icondata;
memset(&icondata,0,sizeof(icondata));
icondata.cbSize=sizeof(icondata);
icondata.hWnd=Handle;
Shell—NotifyIcon(NIM—DELETE,&icondata);
}