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()
00059 {
00060 setObjectName("trayiconimpl");
00061
00062
00063 WM_TASKBARCREATED = RegisterWindowMessage(TEXT("TaskbarCreated"));
00064 }
00065
00066
00067 void
00068 TrayIconImpl::updateTrayIcon(DWORD msg)
00069 {
00070 QT_WA(
00071 {
00072 NOTIFYICONDATAW nfd;
00073 nfd.cbSize = sizeof(NOTIFYICONDATAW);
00074 nfd.hWnd = winId();
00075 nfd.uID = TRAY_ICON_ID;
00076 nfd.uCallbackMessage = WM_NOTIFYICON;
00077 nfd.uFlags = NIF_MESSAGE | NIF_ICON | NIF_TIP;
00078 nfd.hIcon = _icon;
00079 lstrcpynW(nfd.szTip, (TCHAR*)_toolTip.unicode(),
00080 qMin(_toolTip.length()+1, 64));
00081 Shell_NotifyIconW(msg, &nfd);
00082 },
00083 {
00084 NOTIFYICONDATAA nfd;
00085 nfd.cbSize = sizeof(NOTIFYICONDATAA);
00086 nfd.hWnd = winId();
00087 nfd.uID = TRAY_ICON_ID;
00088 nfd.uCallbackMessage = WM_NOTIFYICON;
00089 nfd.uFlags = NIF_MESSAGE | NIF_ICON | NIF_TIP;
00090 nfd.hIcon = _icon;
00091 lstrcpynA(nfd.szTip, (const char*)_toolTip.toLocal8Bit(),
00092 qMin(_toolTip.length()+1, 64));
00093 Shell_NotifyIconA(msg, &nfd);
00094 })
00095 }
00096
00097
00098 bool
00099 TrayIconImpl::winEvent(MSG *msg, long *result)
00100 {
00101 if (msg->message == WM_NOTIFYICON) {
00102 switch (msg->lParam) {
00103 case WM_LBUTTONUP:
00104 return sendMouseEvent(QEvent::MouseButtonRelease, Qt::LeftButton);
00105
00106 case WM_RBUTTONUP:
00107 return sendMouseEvent(QEvent::MouseButtonRelease, Qt::RightButton);
00108
00109 case WM_LBUTTONDBLCLK:
00110 return sendMouseEvent(QEvent::MouseButtonDblClick, Qt::LeftButton);
00111
00112 default:
00113 break;
00114 }
00115 } else if (msg->message == WM_TASKBARCREATED) {
00116
00117
00118 updateTrayIcon(NIM_ADD);
00119 }
00120 return QWidget::winEvent(msg, result);
00121 }
00122
00123
00124 bool
00125 TrayIconImpl::sendMouseEvent(QEvent::Type type, Qt::MouseButton button)
00126 {
00127 QPoint pos = QCursor::pos();
00128 QMouseEvent event(type, mapFromGlobal(pos), pos, button, button, Qt::NoModifier);
00129 return QApplication::sendEvent(this, &event);
00130 }
00131
00132
00133 HICON
00134 TrayIconImpl::createIcon(const int resourceId)
00135 {
00136 return LoadIconA((HINSTANCE)GetModuleHandleA(NULL),
00137 MAKEINTRESOURCEA(resourceId));
00138 }
00139
00140
00141 void
00142 TrayIconImpl::show()
00143 {
00144 updateTrayIcon(NIM_ADD);
00145 }
00146
00147
00148 void
00149 TrayIconImpl::hide()
00150 {
00151 updateTrayIcon(NIM_DELETE);
00152 }
00153
00154
00155 void
00156 TrayIconImpl::setToolTip(const QString &toolTip)
00157 {
00158 _toolTip = toolTip;
00159 updateTrayIcon(NIM_MODIFY);
00160 }
00161
00162
00163 void
00164 TrayIconImpl::setIcon(const QString &iconFile)
00165 {
00166 _icon = createIcon(iconFile.toInt());
00167 updateTrayIcon(NIM_MODIFY);
00168 }
00169