|
|
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;

 枚举#region 枚举
[Flags]
private enum NotifyCommand
 {
Add = 0,
Modify = 1,
Delete = 2
}

[Flags]
private enum NotifyFlags
 {
Message = 1,
Icon = 2,
Tip = 4,
State = 8,
Info = 16
}
#endregion

 结构#region 结构
[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Ansi)]
private struct NotifyIconData
 {
public int cbSize;
public IntPtr hWnd;
public int uID;
public NotifyFlags uFlags;
public int uCallbackMessage;
public IntPtr hIcon;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=0x80)]
public string szTip;
public int dwState;
public int dwStateMask;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=0x100)]
public string szInfo;
public int uTimeoutOrVersion;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=0x40)]
public string szInfoTitle;
public ToolTipIcon dwInfoFlags;
}

[StructLayout(LayoutKind.Sequential)]
private class TPMPARAMS
 {
public int cbSize;
public int rcExclude_left;
public int rcExclude_top;
public int rcExclude_right;
public int rcExclude_bottom;
}
[StructLayout(LayoutKind.Sequential)]
private struct POINT
 {
public int x;
public int y;
}
#endregion

 Win32 API 引用#region Win32 API 引用

[DllImport("user32.dll", CharSet=CharSet.Auto)]
public static extern IntPtr PostMessage(HandleRef hwnd, int msg, int wparam, int lparam);

[DllImport("user32.dll", CharSet=CharSet.Auto)]
public static extern bool PostMessage(HandleRef hwnd, int msg, IntPtr wparam, IntPtr lparam);

[DllImport("shell32.Dll")]
private static extern int Shell_NotifyIcon(NotifyCommand cmd, ref NotifyIconData data);

[DllImport("user32.dll", CharSet=CharSet.Auto, ExactSpelling=true)]
private static extern int TrackPopupMenuEx(HandleRef hMenu, int uFlags,
int x, int y, HandleRef hWnd, TPMPARAMS tpm);

[DllImport("user32.dll", CharSet=CharSet.Auto, ExactSpelling=true)]
private static extern int GetCursorPos(ref POINT point);

[DllImport("user32.dll", CharSet=CharSet.Auto, ExactSpelling=true)]
private static extern bool SetForegroundWindow(HandleRef hWnd);

[DllImport("user32.dll", CharSet=CharSet.Auto)]
private static extern int RegisterWindowMessage(string msg);

[DllImport("kernel32", EntryPoint="OpenProcess")]
private static extern IntPtr OpenProcess (
int dwDesiredAccess,
IntPtr bInheritHandle,
IntPtr dwProcessId
);
[DllImport("kernel32", EntryPoint="CloseHandle")]
private static extern int CloseHandle (
IntPtr hObject
);
[DllImport("user32", EntryPoint="GetWindowThreadProcessId")]
private static extern IntPtr GetWindowThreadProcessId (
IntPtr hwnd,
ref IntPtr lpdwProcessId
);
[DllImport("user32", EntryPoint="FindWindow")]
private static extern IntPtr FindWindow (
string lpClassName,
string lpWindowName
);
[DllImport("user32", EntryPoint="FindWindowEx")]
private static extern IntPtr FindWindowEx (
IntPtr hWnd1,
IntPtr hWnd2,
string lpsz1,
string lpsz2
);
[DllImport("user32", EntryPoint="SendMessage")]
private static extern int SendMessage (
IntPtr hwnd,
int wMsg,
int wParam,
int lParam
);
[DllImport("user32", EntryPoint="SendMessage")]
private static extern int SendMessage (
IntPtr hwnd,
int wMsg,
int wParam,
IntPtr lParam
);
[DllImport("kernel32", EntryPoint="ReadProcessMemory")]
private static extern int ReadProcessMemory (
IntPtr hProcess,
IntPtr lpBaseAddress,
ref IntPtr lpBuffer,
int nSize,
int lpNumberOfBytesWritten
);
[DllImport("kernel32", EntryPoint="ReadProcessMemory")]
private static extern int ReadProcessMemory (
IntPtr hProcess,
IntPtr lpBaseAddress,
byte[] lpBuffer,
int nSize,
int lpNumberOfBytesWritten
);
[DllImport("kernel32", EntryPoint="WriteProcessMemory")]
private static extern int WriteProcessMemory (
IntPtr hProcess,
ref int lpBaseAddress,
ref int lpBuffer,
int nSize,
ref int lpNumberOfBytesWritten
);
[DllImport("kernel32", EntryPoint="VirtualAllocEx")]
private static extern IntPtr VirtualAllocEx (
IntPtr hProcess,
int lpAddress,
int dwSize,
int flAllocationType,
int flProtect
);
[DllImport("kernel32", EntryPoint="VirtualFreeEx")]
private static extern int VirtualFreeEx (
IntPtr hProcess,
IntPtr lpAddress,
int dwSize,
int dwFreeType
);
#endregion

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);
}

 属性#region 属性
public ContextMenu ContextMenu
 {
get
 {
return contextMenu;
}
set
 {
contextMenu = value;
}
}

public IntPtr Handle
 {
get
 {
return this.window.Handle;
}
}

public Icon Icon
 {
get
 {
return icon;
}
set
 {
if (icon != value)
 {
icon = value;
UpdateIcon(visible);
}
}
}

public string Text
 {
get
 {
return text;
}
set
 < |