我知道如何把一个图标放置在Windows通知区域(系统托盘)。
最好的方法是怎样让一个图标动起来?可以使用动态GIF图片吗,还是必须依靠一个计时器?
我正在使用C#和WPF,但WinForms也可以接受。
我知道如何把一个图标放置在Windows通知区域(系统托盘)。
最好的方法是怎样让一个图标动起来?可以使用动态GIF图片吗,还是必须依靠一个计时器?
我正在使用C#和WPF,但WinForms也可以接受。
Abhinaba Basu的博客文章使用C#在系统托盘中实现动画和文本解释了如下内容:
步骤如下:
例如:
private void button1_Click(object sender, System.EventArgs e)
{
m_sysTray.StopAnimation();
Bitmap bmp = new Bitmap("tick.bmp");
// the color from the left bottom pixel will be made transparent
bmp.MakeTransparent();
m_sysTray.SetAnimationClip(bmp);
m_sysTray.StartAnimation(150, 5);
}
SetAnimationClip
使用以下代码创建动画帧。
public void SetAnimationClip (Bitmap bitmapStrip)
{
m_animationIcons = new Icon[bitmapStrip.Width / 16];
for (int i = 0; i < m_animationIcons.Length; i++)
{
Rectangle rect = new Rectangle(i*16, 0, 16, 16);
Bitmap bmp = bitmapStrip.Clone(rect, bitmapStrip.PixelFormat);
m_animationIcons[i] = Icon.FromHandle(bmp.GetHicon());
}
}
为了动画帧,StartAnimation
启动一个定时器,在定时器中更改图标以播放整个序列。
public void StartAnimation(int interval, int loopCount)
{
if(m_animationIcons == null)
throw new ApplicationException("Animation clip not set with
SetAnimationClip");
m_loopCount = loopCount;
m_timer.Interval = interval;
m_timer.Start();
}
private void m_timer_Tick(object sender, EventArgs e)
{
if(m_currIndex < m_animationIcons.Length)
{
m_notifyIcon.Icon = m_animationIcons[m_currIndex];
m_currIndex++;
}
....
}
使用系统托盘
创建并连接您的菜单
ContextMenu m_menu = new ContextMenu();
m_menu.MenuItems.Add(0, new MenuItem("Show",new
System.EventHandler(Show_Click)));
获取您想要在托盘中静态显示的图标。
使用所有必需的信息创建SysTray对象。
m_sysTray = new SysTray("Right click for context menu",
new Icon(GetType(),"TrayIcon.ico"), m_menu);
使用动画帧创建图像条。对于6帧的条形图,图像的宽度将为6*16像素,高度为16像素。
Bitmap bmp = new Bitmap("tick.bmp");
// the color from the left bottom pixel will be made transparent
bmp.MakeTransparent();
m_sysTray.SetAnimationClip(bmp);
开始动画,指示需要循环多少次动画以及每帧的延迟时间
m_sysTray.StartAnimation(150, 5);
调用以下方法停止动画:
m_sysTray.StopAnimation();
我认为最好的方法是使用多个小图标,根据速度和时间不断更改系统托盘对象的新图片。