posts - 50, comments - 134, trackbacks - 8, articles - 0
  博客园 :: 首页 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理

一个新的NotifyIcon

Posted on 2008-01-22 14:33 faib 阅读(382) 评论(0)  编辑 收藏 所属分类: C#控件
    2003的NotifyIcon没有气泡提示功能,所以扩展了一个新的,能达到2005的NotifyIcon的同样功能,并且提供了两个新的功能。
    静态方法 FindNotifyIcon 在系统托盘里查找提示文本相同的托盘句柄,以便以向它发送消息。
    事件 DoWndProc 托盘WndProc时触发。
    下面是程序清单:
using System;
using System.ComponentModel;
using System.Drawing;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace FaibClass.Windows.Forms
{
    
public sealed class NotifyIcon : Component
    
{
        
private bool added;
        
private ContextMenu contextMenu;
        
private bool doubleClick;
        
private ToolTipIcon balloonTipIcon;
        
private string balloonTipText;
        
private string balloonTipTitle;
        
private IntPtr handle = IntPtr.Zero;

        
private Icon icon;
        
private int id;
        
private static int nextId = 0;
        
private string text;
        
private bool visible = true;
        
private NotifyIconNativeWindow window;
        
private static int WM_TASKBARCREATED = RegisterWindowMessage("TaskbarCreated");
        
private const int WM_USER = 0x400;
        
private const int WM_TRAYMOUSEMESSAGE = 2048;
        
private const int WM_MOUSEMOVE = 0x200;
        
private const int WM_LBUTTONDOWN = 0x201;
        
private const int WM_LBUTTONUP = 0x202;
        
private const int WM_LBUTTONDBLCLK = 0x203;
        
private const int WM_RBUTTONDOWN = 0x204;
        
private const int WM_RBUTTONUP = 0x205;
        
private const int WM_RBUTTONDBLCLK = 0x206;
        
private const int WM_MBUTTONDOWN = 0x207;
        
private const int WM_MBUTTONUP = 0x208;
        
private const int WM_MBUTTONDBLCLK = 0x209;
        
private const int NIN_BALLOONSHOW = 0x402;
        
private const int NIN_BALLOONHIDE = 0x403;
        
private const int NIN_BALLOONTIMEOUT = 0x404;
        
private const int NIN_BALLOONUSERCLICK = 0x405;

        
private const int READ_CONTROL = 0x20000;
        
private const int STANDARD_RIGHTS_REQUIRED = 0xF0000;
        
private const int STANDARD_RIGHTS_READ = READ_CONTROL;
        
private const int STANDARD_RIGHTS_EXECUTE = READ_CONTROL;
        
private const int STANDARD_RIGHTS_ALL = 0x1F0000;
        
private const int STANDARD_RIGHTS_WRITE = READ_CONTROL;
        
private const int SYNCHRONIZE = 0x100000;
        
private const int PROCESS_ALL_ACCESS = STANDARD_RIGHTS_REQUIRED | SYNCHRONIZE | 0xFFF;
        
private const int PROCESS_TERMINATE = 0x1;

        
private const int PROCESS_VM_OPERATION = 0x8;
        
private const int PROCESS_VM_READ = 0x10;
        
private const int PROCESS_VM_WRITE = 0x20;
        
private const int MEM_RESERVE = 0x2000;
        
private const int MEM_COMMIT = 0x1000;
        
private const int MEM_RELEASE = 0x8000;
        
private const int PAGE_READWRITE = 0x4;

        
private const int TB_BUTTONCOUNT = (WM_USER + 24);
        
private const int TB_HIDEBUTTON = (WM_USER + 4);
        
private const int TB_GETBUTTON = (WM_USER + 23);
        
private const int TB_GETBITMAP = (WM_USER + 44);
        
private const int TB_DELETEBUTTON = (WM_USER + 22);
        
private const int TB_ADDBUTTONS = (WM_USER + 20);
        
private const int TB_INSERTBUTTON = (WM_USER + 21);
        
private const int TB_ISBUTTONHIDDEN = (WM_USER + 12);
        
private const int ILD_NORMAL = 0x0;

        
private const int TPM_NONOTIFY = 0x80;

        
枚举

        
结构

        
Win32 API 引用

        
public event EventHandler Click;
        
public event EventHandler DoubleClick;
        
public event MouseEventHandler MouseDown;
        
public event MouseEventHandler MouseMove;
        
public event MouseEventHandler MouseUp;
        
public event EventHandler BalloonTipClicked;
        
public event EventHandler BalloonTipClosed;
        
public event EventHandler BalloonTipShown;
        
public event WndProcEventHandler DoWndProc;

        
public NotifyIcon()
        
{
            icon 
= null;
            text 
= "";
            id 
= 0;
            added 
= false;
            window 
= null;
            contextMenu 
= null;
            doubleClick 
= false;
            id 
= ++nextId;
            window 
= new NotifyIconNativeWindow(this);
            
//UpdateIcon(visible);
        }


        
public NotifyIcon(IContainer container) : this()
        
{
            container.Add(
this);
        }


        
属性