前天我看了一下Framework 1.1中NotifyIcon的实现,发现了它不支持Balloon Tip的
原因,那么我们怎么来在FX1.1中也能使用NotifyIcon的Balloon Tip功能呢?反正NotifyIcon也不复杂,自己再实现一遍似乎也不是很难得说。
NotifyIcon继承至Component,然后使用NOTIFYICONDATA来控制Tray中的图标,由于在FX1.1中的NotifyIcon不支持Balloon Tip正是因为这个Shell Struct版本太低,所以我们需要使用5.0 or later版本的NOTIFYICONDATA结构。话说回来,FX使用的NOTIFYICONDATA实现在NativeMethods类中,而这个类的访问级别是internal的,我们想用都还不行
。
NOTIFYICONDATA 5.0的C#说明如下:
[StructLayout(LayoutKind.Sequential, CharSet
=
CharSet.Auto)]
public
class
NOTIFYICONDATA
{
public int cbSize;
public IntPtr hWnd;
public int uID;
public int uFlags;
public int uCallbackMessage;
public IntPtr hIcon;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=0x80)]
public string szTip; // 5.0 must be 128 bytes
public int dwState;
public int dwStateMask;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=0x100)]
public string szInfo;
public uint uTimeoutOrVersion;
/**//*union
{
UINT uTimeout;
UINT uVersion;
};*/
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=0x40)]
public string szInfoTitle;
public int dwInfoFlags;
// public Guid guidItem; // Version 6.0. Reserved
public NOTIFYICONDATA()
{
this.cbSize = Marshal.SizeOf(typeof(NOTIFYICONDATA));
}
}
这个结构有一个地方要非常小心,否则怎么也弄不出那个balloon tip来。我们在
原因一文可以看到szTip的C定义是TCHAR szTip[
64
];,它对应的C#定义本来是:
[MarshalAs(UnmanagedType.ByValTStr, SizeConst
=
0x40
)]
public
string
szTip;
问题是什么呢?MSDN里头说:
For
Version 5.0
and later, szTip can have a maximum of 128 characters, including the terminating NULL. 但是实际上要在托管代码中实现balloon tip,这个szTip的size
must be 128 characters!
NOTIFYICONDATA的一个非常重要的参数是需要一个窗口句柄hWnd,因为它需要窗口来接收消息。这里我们按照NotifyIcon的实现方式,创建一个NativeWindows就行了,就能得到一个窗口句柄。不过为了重载WndProc(ref Message)方法,我们需要实现一个NativeWindows的派生类来供NotifyIcon使用。这些代码都可以从NotifyIcon类中reflect出来,通过少量的修改后,新的NotifyIcon类如下:
代码下载:
NotifyIcon.cs
主要的改动是,取消了两个不能访问的调用:
this.contextMenu.OnPopup(EventArgs.Empty);
System.Windows.Forms.IntSecurity.UnrestrictedWindows.Demand();
然后修改了UpdateIcon(bool)方法中的NOTIFYICONDATA.uFlags的管理方式,不过整个新的NotifyIcon类基本和原NotifyIcon类相同。当然还有最重要的一点改动就是,新的NotifyIcon是可以被继承的,而FX1.1中的NotifyIcon类是sealed的
。
附上NotifyIcon所需的Native方法和一些常量的定义:
using
System;
using
System.ComponentModel;
using
System.Drawing;
using
System.Runtime.InteropServices;
namespace
Birdshome
{
/**//// <summary>
/// Summary description for NativeMethods.
/// </summary>
public class NativeMethods
{
[DllImport("shell32.dll", CharSet=CharSet.Auto)]
public static extern int Shell_NotifyIcon(int message, NOTIFYICONDATA pnid);
[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("user32.dll", CharSet=CharSet.Auto)]
public static extern int RegisterWindowMessage(string msg);
[DllImport("user32.dll", CharSet=CharSet.Auto, ExactSpelling=true)]
public static extern bool GetCursorPos([In, Out] POINT pt);
[DllImport("user32.dll", CharSet=CharSet.Auto, ExactSpelling=true)]
public static extern bool SetForegroundWindow(HandleRef hWnd);
[DllImport("user32.dll", CharSet=CharSet.Auto, ExactSpelling=true)]
public static extern bool TrackPopupMenuEx(HandleRef hmenu, int fuFlags, int x, int y, HandleRef hwnd, TPMPARAMS tpm);
public NativeMethods() {}
}
[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Auto)]
public class NOTIFYICONDATA
{
public int cbSize;
public IntPtr hWnd;
public int uID;
public int uFlags;
public int uCallbackMessage;
public IntPtr hIcon;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=0x80)]
public string szTip; // 5.0 must be 128 bytes
public int dwState;
public int dwStateMask;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=0x100)]
public string szInfo;
public uint uTimeoutOrVersion;
/**//*union
{
UINT uTimeout;
UINT uVersion;
};*/
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=0x40)]
public string szInfoTitle;
public int dwInfoFlags;
// public Guid guidItem; // Version 6.0. Reserved
public NOTIFYICONDATA()
{
this.cbSize = Marshal.SizeOf(typeof(NOTIFYICONDATA));
}
}
[StructLayout(LayoutKind.Sequential)]
public class POINT
{
public int x;
public int y;
public POINT()
{
}
public POINT(int x, int y)
{
this.x = x;
this.y = y;
}
}
[StructLayout(LayoutKind.Sequential)]
public class TPMPARAMS
{
public int cbSize;
public int rcExclude_left;
public int rcExclude_top;
public int rcExclude_right;
public int rcExclude_bottom;
public TPMPARAMS()
{
this.cbSize = Marshal.SizeOf(typeof(TPMPARAMS));
}
}
}
to be continued ...