首先需要更新一下VC6.0的SDK,方法如下: 把ShellAPI.h文件中的关于任务栏提示的函数和常量替换为下面的内容:
//// Tray notification definitions
////
typedef struct _NOTIFYICONDATAA {
DWORD cbSize;
HWND hWnd;
UINT uID;
UINT uFlags;
UINT uCallbackMessage;
HICON hIcon;
#if (_WIN32_IE < 0×0500)
CHAR szTip[64];
#else
CHAR szTip[128];
#endif
#if (_WIN32_IE >= 0×0500)
DWORD dwState;
DWORD dwStateMask;
CHAR szInfo[256];
union {
UINT uTimeout;
UINT uVersion;
} DUMMYUNIONNAME;
CHAR szInfoTitle[64];
DWORD dwInfoFlags;
#endif
#if (_WIN32_IE >= 0×600)
GUID guidItem;
#endif
} NOTIFYICONDATAA, *PNOTIFYICONDATAA;
typedef struct _NOTIFYICONDATAW {
DWORD cbSize;
HWND hWnd;
UINT uID;
UINT uFlags;
UINT uCallbackMessage;
HICON hIcon;
#if (_WIN32_IE < 0×0500)
WCHAR szTip[64];
#else
WCHAR szTip[128];
#endif
#if (_WIN32_IE >= 0×0500)
DWORD dwState;
DWORD dwStateMask;
WCHAR szInfo[256];
union {
UINT uTimeout;
UINT uVersion;
} DUMMYUNIONNAME;
WCHAR szInfoTitle[64];
DWORD dwInfoFlags;
#endif
#if (_WIN32_IE >= 0×600)
GUID guidItem;
#endif
} NOTIFYICONDATAW, *PNOTIFYICONDATAW;
#ifdef UNICODE
typedef NOTIFYICONDATAW NOTIFYICONDATA;
typedef PNOTIFYICONDATAW PNOTIFYICONDATA;
#else
typedef NOTIFYICONDATAA NOTIFYICONDATA;
typedef PNOTIFYICONDATAA PNOTIFYICONDATA;
#endif // UNICODE
#define NOTIFYICONDATAA_V1_SIZE FIELD_OFFSET(NOTIFYICONDATAA, szTip[64])
#define NOTIFYICONDATAW_V1_SIZE FIELD_OFFSET(NOTIFYICONDATAW, szTip[64])
#ifdef UNICODE
#define NOTIFYICONDATA_V1_SIZE NOTIFYICONDATAW_V1_SIZE
#else
#define NOTIFYICONDATA_V1_SIZE NOTIFYICONDATAA_V1_SIZE
#endif
#define NOTIFYICONDATAA_V2_SIZE FIELD_OFFSET(NOTIFYICONDATAA, guidItem)
#define NOTIFYICONDATAW_V2_SIZE FIELD_OFFSET(NOTIFYICONDATAW, guidItem)
#ifdef UNICODE
#define NOTIFYICONDATA_V2_SIZE NOTIFYICONDATAW_V2_SIZE
#else
#define NOTIFYICONDATA_V2_SIZE NOTIFYICONDATAA_V2_SIZE
#endif
#if (_WIN32_IE >= 0×0500)
#define NIN_SELECT (WM_USER + 0)
#define NINF_KEY 0×1
#define NIN_KEYSELECT (NIN_SELECT | NINF_KEY)
#endif
#if (_WIN32_IE >= 0×0501)
#define NIN_BALLOONSHOW (WM_USER + 2)
#define NIN_BALLOONHIDE (WM_USER + 3)
#define NIN_BALLOONTIMEOUT (WM_USER + 4)
#define NIN_BALLOONUSERCLICK (WM_USER + 5)
#endif
#define NIM_ADD 0×00000000
#define NIM_MODIFY 0×00000001
#define NIM_DELETE 0×00000002
#if (_WIN32_IE >= 0×0500)
#define NIM_SETFOCUS 0×00000003
#define NIM_SETVERSION 0×00000004
#define NOTIFYICON_VERSION 3
#endif
#define NIF_MESSAGE 0×00000001
#define NIF_ICON 0×00000002
#define NIF_TIP 0×00000004
#if (_WIN32_IE >= 0×0500)
#define NIF_STATE 0×00000008
#define NIF_INFO 0×00000010
#endif
#if (_WIN32_IE >= 0×600)
#define NIF_GUID 0×00000020
#endif
#if (_WIN32_IE >= 0×0500)
#define NIS_HIDDEN 0×00000001
#define NIS_SHAREDICON 0×00000002
// says this is the source of a shared icon
// Notify Icon Infotip flags
#define NIIF_NONE 0×00000000
// icon flags are mutually exclusive
// and take only the lowest 2 bits
#define NIIF_INFO 0×00000001
#define NIIF_WARNING 0×00000002
#define NIIF_ERROR 0×00000003
#define NIIF_ICON_MASK 0x0000000F
#if (_WIN32_IE >= 0×0501)
#define NIIF_NOSOUND 0×00000010
#endif
#endif
SHSTDAPI_(BOOL) Shell_NotifyIconA(DWORD dwMessage, PNOTIFYICONDATAA lpData);
SHSTDAPI_(BOOL) Shell_NotifyIconW(DWORD dwMessage, PNOTIFYICONDATAW lpData);
#ifdef UNICODE
#define Shell_NotifyIcon Shell_NotifyIconW
#else
#define Shell_NotifyIcon Shell_NotifyIconA
#endif // !UNICODE
////
//// End Tray Notification Icons
////
在stdafx.h文件中添加以下内容:
#ifndef _WIN32_IE // 允许使用 IE 4.0 或更高版本的特定功能。
#define _WIN32_IE 0×0500 //为 IE 5.0 及更新版本改变为适当的值。
#endif
自定义消息及处理函数:
#define MYWM_NOTIFYICON WM_USER+1
afx_msg void OnMyIconNotify(WPARAM wParam,LPARAM lParam);
ON_MESSAGE(MYWM_NOTIFYICON,OnMyIconNotify)
添加变量: NOTIFYICONDATA m_nData;
在任务栏通知区添加图标和提示的代码:
m_nData.cbSize=sizeof(NOTIFYICONDATA);
m_nData.hWnd=GetSafeHwnd();
m_nData.uFlags=NIF_MESSAGE|NIF_INFO|NIF_ICON;
m_nData.uCallbackMessage=MYWM_NOTIFYICON;
m_nData.uID=IDR_MAINFRAME;
m_nData.hIcon=AfxGetApp()->LoadIcon(IDR_MAINFRAME);
m_nData.dwInfoFlags=NIIF_INFO;
_tcscpy(m_nData.szInfoTitle,“管理员”);
_tcscpy(m_nData.szInfo,“欢迎使用”);
Shell_NotifyIcon(NIM_ADD,&m_nData);
修改气泡提示的代码:
void CTipDlg::OnButton1()
{
_tcscpy(m_nData.szInfoTitle,“提醒你”);
_tcscpy(m_nData.szInfo,“内容改变”);
m_nData.uTimeout=1000;
m_nData.uVersion=NOTIFYICON_VERSION;
Shell_NotifyIcon(NIM_MODIFY,&m_nData);
}
删除的代码:
BOOL CTipDlg::DestroyWindow()
{
Shell_NotifyIcon(NIM_DELETE,&m_nData);
return CDialog::DestroyWindow();
}
消息处理函数:
void CTipDlg::OnMyIconNotify(WPARAM wParam,LPARAM lParam)
{
UINT uMouseMsg=LOWORD(lParam);
switch(uMouseMsg)
{
case WM_LBUTTONDBLCLK:
SendMessage(WM_CLOSE,0,0);
break;
case WM_LBUTTONDOWN:
Shell_NotifyIcon(NIM_MODIFY,&m_nData);
break;
}
}
具体使用方法请参考源代码和相关资料!
顺便提供一下在VB中的使用方法:
‘添加两个菜单’注意:要运行本全程,您必须安装 Win2000/XP 或更高版本的操作系统(Shell version 5 or better)
‘在窗口中添加菜单如下:
‘ 标题 名称
‘ 测试 mnuTest
‘ …关于 mnuabout
‘ …退出 mnuexit
‘然后将以下代码复制到窗体代码区:
Option Explicit
‘向窗口发送消息
Private Declare Function SendMessage Lib “user32″ Alias “SendMessageA” (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lparam As Any) As Long
‘获取鼠标坐标(全屏幕)
Private Declare Function GetCursorPos Lib “user32″ (lpPoint As POINTAPI) As Long
Private Type POINTAPI
X As Long
Y As Long
End Type
‘设置窗口为前景窗口
Private Declare Function SetForegroundWindow Lib “user32″ (ByVal hwnd As Long) As Long
‘***********************最小化到任务栏要使用的API、常数、枚举等开始*************************
Private Declare Function Shell_NotifyIcon Lib “shell32.dll” Alias “Shell_NotifyIconA” (ByVal dwMessage As Long, lpData As NOTIFYICONDATA) As Long
Private Type NOTIFYICONDATA
cbSize As Long ‘NOTIFYICONDATA类型的大小,用Len(变量名)获得即可
hwnd As Long ‘窗体的名柄
uId As Long ‘图标资源的ID号,通常使用 vbNull
uFlags As Long ‘使哪些参数有效它是以下枚举类型中的 NIF_INFO Or NIF_ICON Or NIF_TIP Or NIF_MESSAGE 四个常数的组合
uCallBackMessage As Long ‘接受消息的事件
hIcon As Long ‘图标名柄
szTip As String * 128 ‘当鼠标停留在图标上时显示的Tip文本
dwState As Long ‘通常为 0
dwStateMask As Long ‘通常为 0
szInfo As String * 256 ‘Tip文本正文
uTimeoutOrVersion As Long ‘由于VB中没有Union类型,只能用Long型代替
szInfoTitle As String * 64 ‘Tip文本的标题
dwInfoFlags As Long
End Type
Private Const NOTIFYICON_VERSION = 3
Private Const NOTIFYICON_OLDVERSION = 0
‘操作任务栏图标的常数
Private Const NIM_ADD = &H0 ‘添加图标
Private Const NIM_MODIFY = &H1 ‘修改图标
Private Const NIM_DELETE = &H2 ‘删除图标
Private Const NIM_SETFOCUS = &H3
Private Const NIM_SETVERSION = &H4
Private Const NIF_MESSAGE = &H1
Private Const NIF_ICON = &H2
Private Const NIF_TIP = &H4
Private Const NIF_STATE = &H8
Private Const NIF_INFO = &H10
Private Const NIS_HIDDEN = &H1
Private Const NIS_SHAREDICON = &H2
‘Tip上的图标
Private Const NIIF_NONE = &H0 ‘没有图标
Private Const NIIF_WARNING = &H2 ‘“警告”图标(黄色的“!”)
Private Const NIIF_ERROR = &H3 ‘“错误”图标(红色的“X”)
Private Const NIIF_INFO = &H1 ‘“消息”图标(蓝色的“i”)
Private Const WM_MOUSEMOVE = &H200 ‘在图标上移动鼠标
Private Const WM_LBUTTONDOWN = &H201 ‘鼠标左键按下
Private Const WM_LBUTTONUP = &H202 ‘鼠标左键释放
Private Const WM_LBUTTONDBLCLK = &H203 ‘双击鼠标左键
Private Const WM_RBUTTONDOWN = &H204 ‘鼠标右键按下
Private Const WM_RBUTTONUP = &H205 ‘鼠标右键释放
Private Const WM_RBUTTONDBLCLK = &H206 ‘双击鼠标右键
Private Const WM_SETHOTKEY = &H32 ‘响应您定义的热键
Private Const SC_RESTORE = &HF120& ‘还原窗口
Private Const WM_SYSCOMMAND = &H112 ‘系统消息
Private nfIconData As NOTIFYICONDATA
Public Enum TipType
[ttAdd] = NIM_ADD
[ttModify] = NIM_MODIFY
[ttDelete] = NIM_DELETE
End Enum
‘***********************最小化到任务栏要使用的API、常数、枚举等结束*************************
‘显示、修改或删除 Balloon ToolTip
Private Sub ShowToolTip(frmName As Form, ttTipType As TipType, Optional strTip As String = “”, Optional strTitle As String = “”, Optional strInfo As String = “”)
If ttTipType = ttDelete Then
Call Shell_NotifyIcon(NIM_DELETE, nfIconData)
Exit Sub
End If
With nfIconData
.cbSize = Len(nfIconData)
.hwnd = frmName.hwnd
.uId = vbNull
.uFlags = NIF_INFO Or NIF_ICON Or NIF_TIP Or NIF_MESSAGE
.hIcon = frmName.Icon
.szTip = strTip & vbNullChar
.dwState = 0
.dwStateMask = 0
.szInfo = strInfo & vbNullChar
.uTimeoutOrVersion = 5000
.szInfoTitle = strTitle & vbNullChar
.dwInfoFlags = NIIF_INFO
.uCallBackMessage = WM_LBUTTONUP
End With
Call Shell_NotifyIcon(ttTipType, nfIconData)
End Sub
Private Sub Form_Load()
Call ShowToolTip(Me, ttAdd, “Hello!”, “Hello!”, “看到了吗?这个就是 Balloon ToolTip!”)
End Sub
Private Sub Form_Unload(Cancel As Integer)
Call ShowToolTip(Me, ttDelete)
End Sub
Private Sub Form_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
Dim Result As Long
Dim Msg As Long
‘X的值依赖与显示模式的设置
If Me.ScaleMode = vbPixels Then
Msg = X
Else
Msg = X / Screen.TwipsPerPixelX
End If
Select Case Msg
Case WM_LBUTTONDOWN
‘在这里加入鼠标左键释放时你想做的操作
If Me.WindowState = vbMinimized Then
Me.Show
SendMessage Me.hwnd, WM_SYSCOMMAND, SC_RESTORE, 0 ‘还原窗口大小
Else
Me.WindowState = vbMinimized
Me.Hide
End If
End Select
End Sub
>> 本文固定链接: http://www.vcgood.com/archives/264