Cli*_* Ok 16 c# wpf trayicon system-tray notifyicon
我知道如何在Windows通知区域(系统托盘)中放置一个图标.
有图标动画的最佳方法是什么?你可以使用动画gif,还是你必须依赖计时器?
我正在使用C#和WPF,但WinForms也接受了.
Sco*_*man 23
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);
}
Run Code Online (Sandbox Code Playgroud)
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());
}
}
Run Code Online (Sandbox Code Playgroud)
为框架设置动画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++;
}
....
}
Run Code Online (Sandbox Code Playgroud)
使用SysTray
创建并连接菜单
ContextMenu m_menu = new ContextMenu();
m_menu.MenuItems.Add(0, new MenuItem("Show",new
System.EventHandler(Show_Click)));
Run Code Online (Sandbox Code Playgroud)
获取要在托盘中静态显示的图标.
使用所有必需信息创建SysTray对象
m_sysTray = new SysTray("Right click for context menu",
new Icon(GetType(),"TrayIcon.ico"), m_menu);
Run Code Online (Sandbox Code Playgroud)
使用动画帧创建图像条.对于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);
Run Code Online (Sandbox Code Playgroud)
启动动画,指示需要循环动画的次数和帧延迟
m_sysTray.StartAnimation(150, 5);
Run Code Online (Sandbox Code Playgroud)
停止动画通话
m_sysTray.StopAnimation();
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
16816 次 |
| 最近记录: |