public Form1() { // // Required for Windows Form Designer support // InitializeComponent(); // // TODO: Add any constructor code after InitializeComponent call // m_bFlag=true; m_bShowWnd=true;
The DrawAnimatedRects function draws a wire-frame rectangle and animates it to indicate the opening of an icon or the minimizing or maximizing of a window. BOOL DrawAnimatedRects( HWND hwnd, // handle to clipping window int idAni, // type of animation CONST RECT *lprcFrom, // rectangle coordinates (minimized) CONST RECT *lprcTo // rectangle coordinates (restored) );
{
if (this.WindowState == FormWindowState.Minimized)
{
this.notifyIcon1.Visible = true;
this.Visible = false;
}
}
本文通过向大家介绍一个.Net平台上用C#写的可以停放在任务栏上的图标程序(类似于Flashget、OICQ那种系统托盘图标)来和大
家一起体验一下用C#编程的乐趣。
一、概述:
我这里所指的图标程序是类似于在Windows中经常接触的停放在任务栏上,在后台运行的病毒监视、媒体播放等程序。它们一般都具有
单击显示或隐藏主界面,击右键弹出菜单,当鼠标停在图标上时显示提示信息的功能。
程序主要用到了一下几个类:
System.Windows.Fomrs.NotifyIcon类
System.Windows.Forms.ContextMenu类
System.Windows.Forms.MenuItem类
System.Windows.Forms.Label类
System.Windows.Forms.Timer类
二、程序设计和运行的环境:
(1)微软公司视窗2000服务器版或视窗XP版
(2).Net FrameWrok SDK
三、具体实现步骤:
(1).在Visual Studio下新建一个C#的工程,不妨命名为AnimateSystemTray。图示如下:
(2).现在我们来设计程序的主界面:
先将窗体的Text属性设置为"动画系统图标示例",MaximiseBox、MinimiseBox、ShowInTaskbar属性均设置为False。
再往窗体上添加Label控件、NotifyIcon控件、ContextMenu控件、Timer控件各一个。
将Label控件的Text属性设置为"请右击系统托盘图标";将NotifyIcon控件的Icon属性设置为一个图标文件: ,Text属性
设置为"这是个示例程序!",ContextMenu属性设置为"contextMenu1";接下来开始编辑ContextMenu控件,图示如下:
最后还要在导入两个图标 (Icon1.ico), (Icon2.ico)文件到该项目。
(3).现在我们开始编写代码部分:
首先在我们的类中添加以下数据成员:
private Icon m_Icon1;
private Icon m_Icon2;
private bool m_bFlag;
private bool m_bShowWnd;
修改Form1()函数为:
public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
//
m_bFlag=true;
m_bShowWnd=true;
try
{
m_Icon1 = new Icon("Icon1.ico");//导入图标文件
m_Icon2 = new Icon("Icon2.ico");
}
catch ( Exception e )
{
MessageBox.Show("Error " + e.Message,"Animate Tray - Error");
menuItem2.Enabled = false;
menuItem3.Enabled = false;
}
}
添加menuItem1、menuItem2、menuItem3、m_trayIcon的Click事件,消息处理函数如下:
private void menuItem1_Click(object sender, System.EventArgs e)
{
timer1.Start();//打开计时器
}
private void menuItem2_Click(object sender, System.EventArgs e)
{
timer1.Stop();//停止计时器
}
private void menuItem3_Click(object sender, System.EventArgs e)
{
Application.Exit();//退出应用程序
}
private void m_trayIcon_Click(object sender, System.EventArgs e)
{
if(m_bShowWnd == true)//隐藏主界面
{
this.Visible = false;
m_bShowWnd = false;
}
else//显示主界面
{
this.Visible = true;
m_bShowWnd = true;
}
}
最后还要添加timer1的Tick()函数:
private void timer1_Tick(object sender, System.EventArgs e)
{
if ( m_Icon1 != null && m_Icon2 != null ) //如果两个图标文件都被正确载入
{
//只要timer1被启动,则在两个图标之间不断进行选择变换,实现动画效果
if ( m_bFlag == true )
{
m_trayIcon.Icon = m_Icon2;
m_bFlag = false;
}
else
{
m_trayIcon.Icon = m_Icon1;
m_bFlag = true;
}
}
}
HWND hwnd, // handle to clipping window
int idAni, // type of animation
CONST RECT *lprcFrom, // rectangle coordinates (minimized)
CONST RECT *lprcTo // rectangle coordinates (restored)
);
这个函数用于在窗口缩放时产生动画效果在lprcFrom和lprcTo之间描绘一系列动态矩形
hwnd Long,要在其中描绘矩形的窗口。如设为零,表示以桌面为背景
idAni Long,windows 95要设为0
lprcFrom Rect,原始矩形
lprcTo Rect,目标矩形
没时间改例子了..自己多查查MSDN
{
// if the user has not disabled animating windows
if(!this.AnimationDisabled)
{
RECT animateFrom = new RECT();
GetWindowRect(this.Handle, ref animateFrom); RECT animateTo = new RECT ();
IntPtr notifyAreaHandle = GetNotificationAreaHandle(); if (notifyAreaHandle != IntPtr.Zero)
{
if ( GetWindowRect(notifyAreaHandle, ref animateTo) == true)
{
DrawAnimatedRects(this.Handle,
IDANI_CAPTION,ref animateFrom,ref animateTo);
}
}
}
}
private IntPtr GetNotificationAreaHandle()
{
IntPtr hwnd = FindWindowEx(IntPtr.Zero,IntPtr.Zero,"Shell_TrayWnd",null);
hwnd = FindWindowEx(hwnd , IntPtr.Zero ,"TrayNotifyWnd",null);
hwnd = FindWindowEx(hwnd , IntPtr.Zero ,"SysPager",null);
if (hwnd != IntPtr.Zero)
hwnd = FindWindowEx(hwnd , IntPtr.Zero ,null,"Notification Area"); return hwnd;
}