Windows UI - TrayIcon Window
2007-04-28 15:14
141 查看
// TrayIconWindow.cpp : Defines the entry point for the application.
//
#include "stdafx.h"
#include "TrayIconWindow.h"
#include "shellapi.h"
#define MAX_LOADSTRING 100
#define WM_TASKBAR WM_APP + 1000
// Global Variables:
HINSTANCE hInst; // current instance
HWND mainForm;
TCHAR szTitle[MAX_LOADSTRING]; // The title bar text
TCHAR szWindowClass[MAX_LOADSTRING]; // the main window class name
enum WindowStatus
{
WFormal,
WTrayIcon
};
WindowStatus wStatus = WFormal;
// Forward declarations of functions included in this code module:
ATOM MyRegisterClass(HINSTANCE hInstance);
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
void AddIcon();
void DeleteIcon();
void RestoreWindow();
void HideWindow();
int APIENTRY _tWinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPTSTR lpCmdLine,
int nCmdShow)
{
UNREFERENCED_PARAMETER(hPrevInstance);
UNREFERENCED_PARAMETER(lpCmdLine);
// TODO: Place code here.
MSG msg;
// Initialize global strings
LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
LoadString(hInstance, IDC_TRAYICONWINDOW, szWindowClass, MAX_LOADSTRING);
MyRegisterClass(hInstance);
// Perform application initialization:
if (!InitInstance (hInstance, nCmdShow))
{
return FALSE;
}
// Main message loop:
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return (int) msg.wParam;
}
//
// FUNCTION: MyRegisterClass()
//
// PURPOSE: Registers the window class.
//
// COMMENTS:
//
// This function and its usage are only necessary if you want this code
// to be compatible with Win32 systems prior to the 'RegisterClassEx'
// function that was added to Windows 95. It is important to call this function
// so that the application will get 'well formed' small icons associated
// with it.
//
ATOM MyRegisterClass(HINSTANCE hInstance)
{
WNDCLASSEX wcex;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_TRAYICONWINDOW));
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wcex.lpszMenuName = NULL;
wcex.lpszClassName = szWindowClass;
wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));
return RegisterClassEx(&wcex);
}
//
// FUNCTION: InitInstance(HINSTANCE, int)
//
// PURPOSE: Saves instance handle and creates main window
//
// COMMENTS:
//
// In this function, we save the instance handle in a global variable and
// create and display the main program window.
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
HWND hWnd;
hInst = hInstance; // Store instance handle in our global variable
hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);
if (!hWnd)
{
return FALSE;
}
ShowWindow(hWnd,SW_HIDE); //hide main window
UpdateWindow(hWnd);
return TRUE;
}
//
// FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM)
//
// PURPOSE: Processes messages for the main window.
//
// WM_COMMAND - process the application menu
// WM_PAINT - Paint the main window
// WM_DESTROY - post a quit message and return
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
static HMENU hMenu;
switch (message)
{
case WM_CREATE:
mainForm =hWnd;
HWND hIcon;
hIcon = (HWND)::LoadIcon(hInst, NULL);
if (hIcon)
{
::SendMessage(hWnd, WM_SETICON, ICON_BIG, (LPARAM) (HICON)hIcon);
}
hMenu = LoadMenu (hInst, (LPCWSTR)IDC_TRAYICON) ;
hMenu = GetSubMenu (hMenu, 0) ;
AddIcon();
break;;
case WM_SIZE:
if (wParam == SIZE_MINIMIZED)
{
HideWindow();
}
break;
case WM_TASKBAR:
if (lParam == WM_RBUTTONUP)
{
// If In TrayIcon Status Let's Pop Up Menu.
//::SetForegroundWindow(hWnd);
if (wStatus == WTrayIcon)
{
POINT point ;
GetCursorPos(&point);
TrackPopupMenu (hMenu, IDC_TRAYICONWINDOW, point.x, point.y,
0, hWnd, NULL) ;
}
}
else if ((lParam == WM_LBUTTONDOWN)||(lParam == WM_LBUTTONDBLCLK))
{
// Let's Active Main Window Again.
RestoreWindow();
}
break;
case WM_COMMAND:
if (wParam == IDM_EXIT)
{
if (wStatus == WTrayIcon)
{
DeleteIcon();
}
::DestroyWindow(mainForm);
}
break;
case WM_DESTROY:
DeleteIcon();
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
// Message handler for about box.
void AddIcon()
{
HICON hTrayIcon = LoadIcon(hInst, (LPCWSTR)IDI_CUS);
BOOL bSucess =FALSE;
if (hTrayIcon)
{
NOTIFYICONDATA tnid;
tnid.cbSize = sizeof(NOTIFYICONDATA);
tnid.hWnd = mainForm;
tnid.uID = IDI_CUS;
tnid.uFlags = NIF_ICON|NIF_MESSAGE|NIF_TIP;
tnid.uCallbackMessage = WM_TASKBAR;
tnid.hIcon = hTrayIcon;
TCHAR tstring[] = TEXT("I am TryIcon");
//lstrcpyn(tnid.szTip,tstring,sizeof(tstring));
int size = sizeof(tnid.szTip);
lstrcpyn(tnid.szTip,tstring,sizeof(tnid.szTip)/2);
bSucess = Shell_NotifyIcon(NIM_ADD, &tnid);
}
wStatus = bSucess?WTrayIcon:WFormal;
}
void DeleteIcon()
{
BOOL bSucess =FALSE;
NOTIFYICONDATA tnid;
tnid.cbSize = sizeof(NOTIFYICONDATA);
tnid.hWnd = mainForm;
tnid.uID = IDI_CUS;
bSucess = Shell_NotifyIcon(NIM_DELETE, &tnid);
wStatus = WFormal;
}
void RestoreWindow()
{
wStatus = WFormal;
ShowWindow(mainForm, SW_RESTORE);
::SetForegroundWindow(mainForm);
DeleteIcon();
}
void HideWindow()
{
if (wStatus == WFormal)
{
ShowWindow(mainForm, SW_HIDE);
AddIcon();
}
}
//
#include "stdafx.h"
#include "TrayIconWindow.h"
#include "shellapi.h"
#define MAX_LOADSTRING 100
#define WM_TASKBAR WM_APP + 1000
// Global Variables:
HINSTANCE hInst; // current instance
HWND mainForm;
TCHAR szTitle[MAX_LOADSTRING]; // The title bar text
TCHAR szWindowClass[MAX_LOADSTRING]; // the main window class name
enum WindowStatus
{
WFormal,
WTrayIcon
};
WindowStatus wStatus = WFormal;
// Forward declarations of functions included in this code module:
ATOM MyRegisterClass(HINSTANCE hInstance);
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
void AddIcon();
void DeleteIcon();
void RestoreWindow();
void HideWindow();
int APIENTRY _tWinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPTSTR lpCmdLine,
int nCmdShow)
{
UNREFERENCED_PARAMETER(hPrevInstance);
UNREFERENCED_PARAMETER(lpCmdLine);
// TODO: Place code here.
MSG msg;
// Initialize global strings
LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
LoadString(hInstance, IDC_TRAYICONWINDOW, szWindowClass, MAX_LOADSTRING);
MyRegisterClass(hInstance);
// Perform application initialization:
if (!InitInstance (hInstance, nCmdShow))
{
return FALSE;
}
// Main message loop:
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return (int) msg.wParam;
}
//
// FUNCTION: MyRegisterClass()
//
// PURPOSE: Registers the window class.
//
// COMMENTS:
//
// This function and its usage are only necessary if you want this code
// to be compatible with Win32 systems prior to the 'RegisterClassEx'
// function that was added to Windows 95. It is important to call this function
// so that the application will get 'well formed' small icons associated
// with it.
//
ATOM MyRegisterClass(HINSTANCE hInstance)
{
WNDCLASSEX wcex;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_TRAYICONWINDOW));
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wcex.lpszMenuName = NULL;
wcex.lpszClassName = szWindowClass;
wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));
return RegisterClassEx(&wcex);
}
//
// FUNCTION: InitInstance(HINSTANCE, int)
//
// PURPOSE: Saves instance handle and creates main window
//
// COMMENTS:
//
// In this function, we save the instance handle in a global variable and
// create and display the main program window.
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
HWND hWnd;
hInst = hInstance; // Store instance handle in our global variable
hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);
if (!hWnd)
{
return FALSE;
}
ShowWindow(hWnd,SW_HIDE); //hide main window
UpdateWindow(hWnd);
return TRUE;
}
//
// FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM)
//
// PURPOSE: Processes messages for the main window.
//
// WM_COMMAND - process the application menu
// WM_PAINT - Paint the main window
// WM_DESTROY - post a quit message and return
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
static HMENU hMenu;
switch (message)
{
case WM_CREATE:
mainForm =hWnd;
HWND hIcon;
hIcon = (HWND)::LoadIcon(hInst, NULL);
if (hIcon)
{
::SendMessage(hWnd, WM_SETICON, ICON_BIG, (LPARAM) (HICON)hIcon);
}
hMenu = LoadMenu (hInst, (LPCWSTR)IDC_TRAYICON) ;
hMenu = GetSubMenu (hMenu, 0) ;
AddIcon();
break;;
case WM_SIZE:
if (wParam == SIZE_MINIMIZED)
{
HideWindow();
}
break;
case WM_TASKBAR:
if (lParam == WM_RBUTTONUP)
{
// If In TrayIcon Status Let's Pop Up Menu.
//::SetForegroundWindow(hWnd);
if (wStatus == WTrayIcon)
{
POINT point ;
GetCursorPos(&point);
TrackPopupMenu (hMenu, IDC_TRAYICONWINDOW, point.x, point.y,
0, hWnd, NULL) ;
}
}
else if ((lParam == WM_LBUTTONDOWN)||(lParam == WM_LBUTTONDBLCLK))
{
// Let's Active Main Window Again.
RestoreWindow();
}
break;
case WM_COMMAND:
if (wParam == IDM_EXIT)
{
if (wStatus == WTrayIcon)
{
DeleteIcon();
}
::DestroyWindow(mainForm);
}
break;
case WM_DESTROY:
DeleteIcon();
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
// Message handler for about box.
void AddIcon()
{
HICON hTrayIcon = LoadIcon(hInst, (LPCWSTR)IDI_CUS);
BOOL bSucess =FALSE;
if (hTrayIcon)
{
NOTIFYICONDATA tnid;
tnid.cbSize = sizeof(NOTIFYICONDATA);
tnid.hWnd = mainForm;
tnid.uID = IDI_CUS;
tnid.uFlags = NIF_ICON|NIF_MESSAGE|NIF_TIP;
tnid.uCallbackMessage = WM_TASKBAR;
tnid.hIcon = hTrayIcon;
TCHAR tstring[] = TEXT("I am TryIcon");
//lstrcpyn(tnid.szTip,tstring,sizeof(tstring));
int size = sizeof(tnid.szTip);
lstrcpyn(tnid.szTip,tstring,sizeof(tnid.szTip)/2);
bSucess = Shell_NotifyIcon(NIM_ADD, &tnid);
}
wStatus = bSucess?WTrayIcon:WFormal;
}
void DeleteIcon()
{
BOOL bSucess =FALSE;
NOTIFYICONDATA tnid;
tnid.cbSize = sizeof(NOTIFYICONDATA);
tnid.hWnd = mainForm;
tnid.uID = IDI_CUS;
bSucess = Shell_NotifyIcon(NIM_DELETE, &tnid);
wStatus = WFormal;
}
void RestoreWindow()
{
wStatus = WFormal;
ShowWindow(mainForm, SW_RESTORE);
::SetForegroundWindow(mainForm);
DeleteIcon();
}
void HideWindow()
{
if (wStatus == WFormal)
{
ShowWindow(mainForm, SW_HIDE);
AddIcon();
}
}
相关文章推荐
- Windows UI - TrayIcon Dialog
- window位图光栅运算 - ExtractAssociatedIcon
- WPF:Window Icon
- QT中QSystemTrayIcon
- 用MASM32写的 系统托盘图标管理员 TrayIconMan 0.0.0001
- windows UI: 即时绘制 窗口某区域 RedrawWindow RDW_UPDATENOW
- Qt之系统托盘(QSystemTrayIcon详解)
- Making a System Tray Icon for your application
- Programmatically display system tray icon on Windows Mobile
- 托盘 tray MainWindowHandle 0
- WPF:Window Icon
- 在QML中使用QSystemTrayIcon(系统托盘)
- ShowBalloonTips @ TrayIcon
- QSystemTrayIcon menu无法消失问题
- Qt浅谈之三十系统托盘(QSystemTrayIcon)
- Delphi7使用控件CoolTrayIcon时如何做到程序启动就最小化到托盘
- Delphi TrayIcon任务栏图标毛边问题[部分解决]
- 安装成功两个组件:coolTrayIcon和RxLib2.75-D7
- S+ hidden tray with window start
- 用MASM32写了个系统托盘图标管理者 TrayIconMan