When surfing the Internet in Windows, did you notice an animated icon on the taskbar on the right hand side of the screen? It flashes, and the image indicates that the network is transferring data at this time. There are a lot of articles about the taskbar icon programming, but how can I make a dynamic icon? In the C++builder can be more convenient to achieve.
The basic programming idea is to create the animation effect by setting the timer clock control so that the application sends a specific message within a specified time interval so that the taskbar icon changes continuously. The implementation method is to load several image controls in the application's form so that they can load the corresponding drawing, and several pictures are displayed sequentially in order to form the animation.
Here, we use a door switch animation for example, put a timer control on the form, two image, loaded "open door" and "closed" two pictures. Start adding code.
The application must notify the taskbar to add, delete, and modify the icon by sending a message. Sending a message must
Call Shell_NotifyIcon. Its prototype is:
Winshellapi Boll WINAPI Shell_NotifyIcon (
DWORD dwmessage, Pointifycondata Pnid);
The first parameter dwmessage is the flag that sends the message, you can select
Nim_add//Add icon to taskbar notification area
Nim_delete//Remove icon to taskbar notification area
Nim_modify//Notify taskbar notification area Modify icon
To compile a message-sending function traymessage
bool __fastcall TForm1::TrayMessage(DWORD dwMessage)
{
NOTIFYICONDATA tnd;
PSTR pszTip;
pszTip = TipText();
tnd.cbSize= sizeof(NOTIFYICONDATA);
//结构的大小
tnd.uCallbackMessage = MYWM_NOTIFY;
//自定义回调消息,在头文件中声明
tnd.hWnd= Handle;
//接受回调消息的窗口句柄
tnd.uID = IDC_MYICON;
//图标标志号
tnd.uFlags= NIF_MESSAGE | NIF_ICON | NIF_TIP;
//指定以下三个参数哪个包含有效数据
if (dwMessage == NIM_MODIFY)
{
tnd.hIcon =
(HICON)IconHandle(); //取得图标句柄
if (pszTip)
lstrcpyn(tnd.szTip, pszTip,
sizeof(tnd.szTip));
else
tnd.szTip[0] = '\0';
}
else
{
tnd.hIcon = NULL;
tnd.szTip[0] = '\0';
}
return (Shell_NotifyIcon(dwMessage, &tnd));
}
To draw a function to get an icon handle
HICON __fastcall TForm1::IconHandle(void)
{
if (n==1)
{ return (Image1- >Picture->Icon- >Handle);
//n是全局变量,1为显示Image1,0为Image2
}
else
{ return (Image2- >Picture- >Icon- >Handle);
}
}
To draw an icon state conversion function
void __fastcall TForm1::ToggleState(void)
{
if (n==1) //n为图标句柄锁,是全局变量,
1为显示Image1,0为Image2
{
n=n-1;
}
else
{
n=n+1;
}
TrayMessage(NIM_MODIFY);
//发送图标变换消息
}
对Timer控件编制代码,设它的Interval属性为1000,即定时器每一秒响应一次。为 Ontimer事件键入代码:
void __fastcall TForm1::Timer1Timer(TObject *Sender)
{ ToggleState( );
}
Due to limited space, the above only lists the basic part of the Code, other functions, such as closing the program, open the window, and so on, relatively simple, not to repeat. When the program is running, you will see an animated icon with a door open and closed on the taskbar in the lower-right corner of the screen. Isn't it funny, just make up one you like.