让FX1.1的NotifyIcon支持BalloonTip(1)

    前天我看了一下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的,我们想用都还不行 confused_smile.gif

    NOTIFYICONDATA 5.0的C#说明如下:
None.gif [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
None.gif
public   class  NOTIFYICONDATA
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
public int cbSize;
InBlock.gif    
public IntPtr hWnd;
InBlock.gif    
public int uID;
InBlock.gif    
public int uFlags;
InBlock.gif    
public int uCallbackMessage;
InBlock.gif    
public IntPtr hIcon;
InBlock.gif    [MarshalAs(UnmanagedType.ByValTStr, SizeConst
=0x80)]
InBlock.gif    
public string szTip;    // 5.0 must be 128 bytes
InBlock.gif

InBlock.gif    
public int dwState;
InBlock.gif    
public int dwStateMask;
InBlock.gif    [MarshalAs(UnmanagedType.ByValTStr, SizeConst
=0x100)]
InBlock.gif    
public string szInfo;
InBlock.gif    
public uint uTimeoutOrVersion;
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//*union
InBlock.gif    {
InBlock.gif        UINT uTimeout;
InBlock.gif        UINT uVersion;
ExpandedSubBlockEnd.gif    };
*/

InBlock.gif    [MarshalAs(UnmanagedType.ByValTStr, SizeConst
=0x40)]
InBlock.gif    
public string szInfoTitle;
InBlock.gif    
public int dwInfoFlags;
InBlock.gif    
// public Guid guidItem; // Version 6.0. Reserved
InBlock.gif
    public NOTIFYICONDATA()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
this.cbSize = Marshal.SizeOf(typeof(NOTIFYICONDATA));
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

    这个结构有一个地方要非常小心,否则怎么也弄不出那个balloon tip来。我们在 原因一文可以看到szTip的C定义是TCHAR szTip[ 64 ];,它对应的C#定义本来是:
None.gif   [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 0x40 )]
None.gif  
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的 angry_smile.gif

    附上NotifyIcon所需的Native方法和一些常量的定义:
None.gif using  System;
None.gif
using  System.ComponentModel;
None.gif
using  System.Drawing;
None.gif
using  System.Runtime.InteropServices;
None.gif
None.gif
namespace  Birdshome
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// Summary description for NativeMethods.
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    public class NativeMethods
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        [DllImport(
"shell32.dll", CharSet=CharSet.Auto)]
InBlock.gif        
public static extern int Shell_NotifyIcon(int message, NOTIFYICONDATA pnid);
InBlock.gif
InBlock.gif        [DllImport(
"user32.dll", CharSet=CharSet.Auto)]
InBlock.gif        
public static extern IntPtr PostMessage(HandleRef hwnd, int msg, int wparam, int lparam);
InBlock.gif
InBlock.gif        [DllImport(
"user32.dll", CharSet=CharSet.Auto)]
InBlock.gif        
public static extern bool PostMessage(HandleRef hwnd, int msg, IntPtr wparam, IntPtr lparam);
InBlock.gif
InBlock.gif        [DllImport(
"user32.dll", CharSet=CharSet.Auto)]
InBlock.gif        
public static extern int RegisterWindowMessage(string msg);
InBlock.gif
InBlock.gif        [DllImport(
"user32.dll", CharSet=CharSet.Auto, ExactSpelling=true)]
InBlock.gif        
public static extern bool GetCursorPos([In, Out] POINT pt);
InBlock.gif
InBlock.gif        [DllImport(
"user32.dll", CharSet=CharSet.Auto, ExactSpelling=true)]
InBlock.gif        
public static extern bool SetForegroundWindow(HandleRef hWnd);
InBlock.gif
InBlock.gif        [DllImport(
"user32.dll", CharSet=CharSet.Auto, ExactSpelling=true)]
InBlock.gif        
public static extern bool TrackPopupMenuEx(HandleRef hmenu, int fuFlags, int x, int y, HandleRef hwnd, TPMPARAMS tpm);
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
public NativeMethods() dot.gif{}
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    [StructLayout(LayoutKind.Sequential, CharSet
=CharSet.Auto)]
InBlock.gif    
public class NOTIFYICONDATA
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
public int cbSize;
InBlock.gif        
public IntPtr hWnd;
InBlock.gif        
public int uID;
InBlock.gif        
public int uFlags;
InBlock.gif        
public int uCallbackMessage;
InBlock.gif        
public IntPtr hIcon;
InBlock.gif        [MarshalAs(UnmanagedType.ByValTStr, SizeConst
=0x80)]
InBlock.gif        
public string szTip;    // 5.0 must be 128 bytes
InBlock.gif

InBlock.gif        
public int dwState;
InBlock.gif        
public int dwStateMask;
InBlock.gif        [MarshalAs(UnmanagedType.ByValTStr, SizeConst
=0x100)]
InBlock.gif        
public string szInfo;
InBlock.gif        
public uint uTimeoutOrVersion;
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//*union
InBlock.gif        {
InBlock.gif            UINT uTimeout;
InBlock.gif            UINT uVersion;
ExpandedSubBlockEnd.gif        };
*/

InBlock.gif        [MarshalAs(UnmanagedType.ByValTStr, SizeConst
=0x40)]
InBlock.gif        
public string szInfoTitle;
InBlock.gif        
public int dwInfoFlags;
InBlock.gif        
// public Guid guidItem; // Version 6.0. Reserved
InBlock.gif
        public NOTIFYICONDATA()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
this.cbSize = Marshal.SizeOf(typeof(NOTIFYICONDATA));
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    [StructLayout(LayoutKind.Sequential)]
InBlock.gif    
public class POINT
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
public int x;
InBlock.gif        
public int y;
InBlock.gif        
public POINT()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockEnd.gif        }

InBlock.gif        
public POINT(int x, int y)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
this.x = x;
InBlock.gif            
this.y = y;
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    [StructLayout(LayoutKind.Sequential)]
InBlock.gif    
public class TPMPARAMS
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
public int cbSize;
InBlock.gif        
public int rcExclude_left;
InBlock.gif        
public int rcExclude_top;
InBlock.gif        
public int rcExclude_right;
InBlock.gif        
public int rcExclude_bottom;
InBlock.gif        
public TPMPARAMS()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
this.cbSize = Marshal.SizeOf(typeof(TPMPARAMS));
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

    to be continued ...

转载于:https://www.cnblogs.com/birdshome/archive/2005/05/19/158583.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值