Windows taskbar system tray icon Demo Program

Source: Internet
Author: User

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);
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.