cmf
尊榮會員
發表:84 回覆:918 積分:1032 註冊:2002-06-26
發送簡訊給我
|
//TEST.H
//......................................... #include "stdio.h"
#include "process.h"
#include "assert.h"
#include "crtdbg.h"
#include "locale.h"
#include "time.h"
#include
#include
#include class TEST
{
public:
Daemon;
virtual ~TEST();
static LRESULT CALLBACK WndProc(HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam);
protected:
void AddTrayIcon();
void CheckTrayIcon();
void RemoveTrayIcon();
bool SendTrayMsg(DWORD msg); HWND m_hwnd;
HMENU m_hmenu;
UINT m_timer;
NOTIFYICONDATA m_nid; }; //...........................................
//TEST.CPP
//........................................... #include "stdio.h"
#include "process.h"
#include "assert.h"
#include "crtdbg.h"
#include "locale.h"
#include "time.h"
#include "tchar.h"
#include "windows.h"
#include "io.h"
#include "TEST.h" TEST::TEST
{ // Create a dummy window
WNDCLASSEX wndclass; wndclass.cbSize = sizeof(wndclass);
wndclass.style = CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS;
wndclass.lpfnWndProc = TEST::WndProc;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hInstance = pApp->m_instance;
wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
wndclass.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH);
wndclass.lpszMenuName = (const char *) NULL;
wndclass.lpszClassName = DAEMON_CLASS_NAME;
wndclass.hIconSm = LoadIcon(NULL, IDI_APPLICATION); RegisterClassEx(&wndclass); m_hwnd = CreateWindow(DAEMON_CLASS_NAME,
DAEMON_CLASS_NAME,
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
200, 200,
NULL,
NULL,
pApp->m_instance,
NULL); // record which client created this window
SetWindowLong(m_hwnd, GWL_USERDATA, (LONG) this); // Load a popup menu
m_hmenu = LoadMenu(pApp->m_instance, MAKEINTRESOURCE(IDR_TRAYMENU)); // Create the tray icon
AddTrayIcon(); // A timer checks that the tray icon is intact
m_timer = SetTimer( m_hwnd, IDT_TRAYTIMER, 15000, NULL);
} void TEST::AddTrayIcon() { SendTrayMsg(NIM_ADD);
} void TEST::CheckTrayIcon() { if (!SendTrayMsg(NIM_MODIFY)) { AddTrayIcon();
};
} void TEST::RemoveTrayIcon() { SendTrayMsg(NIM_DELETE);
} bool TEST::SendTrayMsg(DWORD msg)
{
m_nid.hWnd = m_hwnd;
m_nid.cbSize = sizeof(m_nid);
m_nid.uID = IDR_TRAY; // never changes after construction
m_nid.hIcon = LoadIcon(pApp->m_instance, MAKEINTRESOURCE(IDR_TRAY));
m_nid.uFlags = NIF_ICON | NIF_MESSAGE;
m_nid.uCallbackMessage = WM_TRAYNOTIFY;
m_nid.szTip[0] = '\0';
// Use resource string as tip if there is one
if (LoadString(pApp->m_instance, IDR_TRAY, m_nid.szTip, sizeof(m_nid.szTip))) {
m_nid.uFlags |= NIF_TIP;
}
return (bool) (Shell_NotifyIcon(msg, &m_nid) != 0);
} // Process window messages
LRESULT CALLBACK TEST::WndProc(HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam) { TEST *_this = (TEST *) GetWindowLong(hwnd, GWL_USERDATA); switch (iMsg) { case WM_CREATE:
{
return 0;
} case WM_COMMAND:
switch (LOWORD(wParam)) {
case ID_MENU1: break;
case ID_MENU2: break;
case ID_MENU3: break;
case IDD_APP_ABOUT:
ShowAboutBox();
break;
}
return 0;
case WM_TRAYNOTIFY:
{
HMENU hSubMenu = GetSubMenu(_this->m_hmenu, 0);
if (lParam==WM_LBUTTONDBLCLK) {
// double click: execute first menu item
::SendMessage(_this->m_nid.hWnd, WM_COMMAND,
GetMenuItemID(hSubMenu, 0), 0);
} else if (lParam==WM_RBUTTONUP || lParam==WM_LBUTTONUP) {
if (hSubMenu == NULL) { return 0;
}
// Make first menu item the default (bold font)
::SetMenuDefaultItem(hSubMenu, 0, TRUE); POINT mouse;
GetCursorPos(&mouse);
::SetForegroundWindow(_this->m_nid.hWnd);
::TrackPopupMenu(hSubMenu, 0, mouse.x, mouse.y, 0,
_this->m_nid.hWnd, NULL); }
return 0;
}
case WM_TIMER:
_this->CheckTrayIcon();
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
} return DefWindowProc(hwnd, iMsg, wParam, lParam);
} TEST::~TEST()
{
KillTimer(m_hwnd, m_timer);
RemoveTrayIcon();
DestroyMenu(m_hmenu); } 發表人 - cmf 於 2003/01/29 15:41:18
------ ︿︿
|