先上测试图
托盘也是属于比较常用的了
其实托盘操作很简单的,基本都是通过Shell_NotifyIcon API进行操作
主要是它的第二个参数
Shell_NotifyIcon原型:
1 BOOL Shell_NotifyIcon( 2 _In_ DWORD dwMessage, 3 _In_ PNOTIFYICONDATA lpdata 4 );
第一个参数是要对托盘进行的操作类型,第二个参数是个结构体。
NOTIFYDATA结构体原型
1 typedef struct _NOTIFYICONDATA { 2 DWORD cbSize; 3 HWND hWnd; 4 UINT uID; 5 UINT uFlags; 6 UINT uCallbackMessage; 7 HICON hIcon; 8 TCHAR szTip[64]; 9 DWORD dwState; 10 DWORD dwStateMask; 11 TCHAR szInfo[256]; 12 union { 13 UINT uTimeout; 14 UINT uVersion; 15 }; 16 TCHAR szInfoTitle[64]; 17 DWORD dwInfoFlags; 18 GUID guidItem; 19 HICON hBalloonIcon; 20 } NOTIFYICONDATA, *PNOTIFYICONDATA;
其实我是从MSDN里复制的 - - ,结构体成员一看就知道什么意思,深入了解还是参考MSDN吧,它更加详细官方
我写了个类把基本操作写好了,没写挂接时间,因为我感觉暂时用不到,用到了再写吧。
TrayClass.h
1 #pragma once 2 #include <Windows.h> 3 4 class Tray 5 { 6 private: 7 NOTIFYICONDATA m_msgNotify; 8 HWND m_hwnd; 9 10 public: 11 Tray(); 12 ~Tray(); 13 // 寻找托盘窗口句柄 14 HWND FindTrayWnd(void); 15 // 寻找托盘句柄 针对win7及以上系统(含有小三角隐藏图标) 16 HWND FindNotifyIconOverflowWindow(void); 17 // 创建托盘(调用者窗口句柄,图标数据NULL为当前图标,提示消息) 18 BOOL Create(HWND hwnd, LPVOID lpszIcon, LPCWCHAR msg); 19 // 修改消息 20 BOOL SetTip(LPCWCHAR msg); 21 // 气泡提示 22 BOOL SetBubble(LPCWCHAR tipTitle, LPCWCHAR tipContent, DWORD tipIcon=0); 23 24 };
TrayClass.cpp
1 #include "TrayClass.h" 2 3 Tray::Tray() 4 { 5 Tray::m_msgNotify = { 0 }; 6 Tray::m_msgNotify.cbSize = sizeof(Tray::m_msgNotify); 7 } 8 9 Tray::~Tray() 10 { 11 memset(Tray::m_msgNotify.szInfo, 0, sizeof(Tray::m_msgNotify)); 12 memset(Tray::m_msgNotify.szInfoTitle, 0, sizeof(Tray::m_msgNotify)); 13 Shell_NotifyIcon(NIM_DELETE, &(Tray::m_msgNotify)); 14 } 15 16 HWND Tray::FindTrayWnd(void) 17 { 18 HWND hwnd = FindWindow(L"Shell_TrayWnd", NULL); 19 hwnd = FindWindowEx(hwnd, NULL, L"TrayNotifyWnd", NULL); 20 hwnd = FindWindowEx(hwnd, NULL, L"SysPager", NULL); 21 hwnd = FindWindowEx(hwnd, NULL, L"ToolbarWindow32", NULL); 22 return hwnd; 23 } 24 25 HWND Tray::FindNotifyIconOverflowWindow(void) 26 { 27 HWND hwnd = FindWindow(L"NotifyIconOverflowWindow", NULL); 28 hwnd = FindWindowEx(hwnd, NULL, L"ToolbarWindow32", NULL); 29 return hwnd; 30 } 31 32 BOOL Tray::Create(HWND hwnd, LPVOID lpszIcon, LPCWCHAR msg) 33 { 34 if (IsWindow(hwnd)) 35 { 36 Tray::m_hwnd = hwnd; 37 Tray::m_msgNotify.hWnd = hwnd; 38 Tray::m_msgNotify.uFlags = NIF_ICON | NIF_TIP | NIF_INFO | NIF_MESSAGE; 39 Tray::m_msgNotify.hIcon = (HICON)SendMessage(hwnd, 127, 0, 0);// 40 Tray::m_msgNotify.uID = 1; 41 Tray;; m_msgNotify.uTimeout = 10000; 42 lstrcpyn(Tray::m_msgNotify.szTip, msg, lstrlen(msg) + 1); 43 return Shell_NotifyIcon(NIM_ADD, &(Tray::m_msgNotify)); 44 } 45 return FALSE; 46 } 47 48 BOOL Tray::SetTip(LPCWCHAR msg) 49 { 50 lstrcpyn(Tray::m_msgNotify.szTip, msg, 128); 51 return Shell_NotifyIcon(NIM_MODIFY, &(Tray::m_msgNotify)); 52 } 53 54 BOOL Tray::SetBubble(LPCWCHAR tipTitle, LPCWCHAR tipContent, DWORD tipIcon) 55 { 56 Tray::m_msgNotify.dwInfoFlags = tipIcon; 57 // 规定提示标题长度限制64 58 lstrcpyn(Tray::m_msgNotify.szInfoTitle, tipTitle, 64); 59 // 规定提示内容长度限制256 60 lstrcpyn(Tray::m_msgNotify.szInfo, tipContent, 256); 61 return Shell_NotifyIcon(NIM_MODIFY, &(Tray::m_msgNotify)); 62 }
Win10下测试也是Ok的,基本上是仿照易语言的精易模块写的,他给了我很大的帮助,感谢!
有一点不明白,他那个刷新图标循环没看懂,怎么看都像死循环。。。