您的位置:首页 > 产品设计 > UI/UE

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();
 }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息