00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048 #include <QApplication>
00049
00050 #include "trayicon_win.h"
00051
00052 #define WM_NOTIFYICON WM_APP
00053 #define TRAY_ICON_ID 0
00054 UINT WM_TASKBARCREATED;
00055
00056
00057
00058 TrayIconImpl::TrayIconImpl(const QString &iconFile, const QString &toolTip)
00059 {
00060 setObjectName("trayiconimpl");
00061
00062
00063 WM_TASKBARCREATED = RegisterWindowMessage(TEXT("TaskbarCreated"));
00064
00065
00066 setIcon(iconFile);
00067
00068
00069 setToolTip(toolTip);
00070 }
00071
00072
00073 void
00074 TrayIconImpl::updateTrayIcon(DWORD msg)
00075 {
00076 QT_WA(
00077 {
00078 NOTIFYICONDATAW nfd;
00079 nfd.cbSize = sizeof(NOTIFYICONDATAW);
00080 nfd.hWnd = winId();
00081 nfd.uID = TRAY_ICON_ID;
00082 nfd.uCallbackMessage = WM_NOTIFYICON;
00083 nfd.uFlags = NIF_MESSAGE | NIF_ICON | NIF_TIP;
00084 nfd.hIcon = _icon;
00085 lstrcpynW(nfd.szTip, (TCHAR*)_toolTip.unicode(),
00086 qMin(_toolTip.length()+1, 64));
00087 Shell_NotifyIconW(msg, &nfd);
00088 },
00089 {
00090 NOTIFYICONDATAA nfd;
00091 nfd.cbSize = sizeof(NOTIFYICONDATAA);
00092 nfd.hWnd = winId();
00093 nfd.uID = TRAY_ICON_ID;
00094 nfd.uCallbackMessage = WM_NOTIFYICON;
00095 nfd.uFlags = NIF_MESSAGE | NIF_ICON | NIF_TIP;
00096 nfd.hIcon = _icon;
00097 lstrcpynA(nfd.szTip, (const char*)_toolTip.toLocal8Bit(),
00098 qMin(_toolTip.length()+1, 64));
00099 Shell_NotifyIconA(msg, &nfd);
00100 })
00101 }
00102
00103
00104 bool
00105 TrayIconImpl::winEvent(MSG *msg, long *result)
00106 {
00107 if (msg->message == WM_NOTIFYICON) {
00108 switch (msg->lParam) {
00109 case WM_LBUTTONUP:
00110 return sendMouseEvent(QEvent::MouseButtonRelease, Qt::LeftButton);
00111
00112 case WM_RBUTTONUP:
00113 return sendMouseEvent(QEvent::MouseButtonRelease, Qt::RightButton);
00114
00115 case WM_LBUTTONDBLCLK:
00116 return sendMouseEvent(QEvent::MouseButtonDblClick, Qt::LeftButton);
00117
00118 default:
00119 break;
00120 }
00121 } else if (msg->message == WM_TASKBARCREATED) {
00122
00123
00124 updateTrayIcon(NIM_ADD);
00125 }
00126 return QWidget::winEvent(msg, result);
00127 }
00128
00129
00130 bool
00131 TrayIconImpl::sendMouseEvent(QEvent::Type type, Qt::MouseButton button)
00132 {
00133 QPoint pos = QCursor::pos();
00134 QMouseEvent event(type, mapFromGlobal(pos), pos, button, button, Qt::NoModifier);
00135 return QApplication::sendEvent(this, &event);
00136 }
00137
00138
00139 HICON
00140 TrayIconImpl::createIcon(const int resourceId)
00141 {
00142 return LoadIconA((HINSTANCE)GetModuleHandleA(NULL),
00143 MAKEINTRESOURCEA(resourceId));
00144 }
00145
00146
00147 void
00148 TrayIconImpl::show()
00149 {
00150 updateTrayIcon(NIM_ADD);
00151 }
00152
00153
00154 void
00155 TrayIconImpl::hide()
00156 {
00157 updateTrayIcon(NIM_DELETE);
00158 }
00159
00160
00161 void
00162 TrayIconImpl::setToolTip(const QString &toolTip)
00163 {
00164 _toolTip = toolTip;
00165 updateTrayIcon(NIM_MODIFY);
00166 }
00167
00168
00169 void
00170 TrayIconImpl::setIcon(const QString &iconFile)
00171 {
00172 _icon = createIcon(iconFile.toInt());
00173 updateTrayIcon(NIM_MODIFY);
00174 }
00175