Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

177 lines
4.3 KiB

  1. /*++
  2. Copyright (c) Microsoft Corporation. All rights reserved.
  3. Module Name:
  4. Tray.cpp
  5. Abstract:
  6. Implements systray functionality.
  7. Notes:
  8. Unicode only.
  9. History:
  10. 05/04/2001 rparsons Created
  11. 01/11/2002 rparsons Cleaned up
  12. --*/
  13. #include "precomp.h"
  14. /*++
  15. Routine Description:
  16. Adds the specified icon to the system tray.
  17. Arguments:
  18. hWnd - Parent window handle.
  19. hIcon - Icon handle to add to the tray.
  20. pwszTip - Tooltip to associate with the icon.
  21. Return Value:
  22. TRUE on success, FALSE otherwise.
  23. --*/
  24. BOOL
  25. AddIconToTray(
  26. IN HWND hWnd,
  27. IN HICON hIcon,
  28. IN LPCWSTR pwszTip
  29. )
  30. {
  31. NOTIFYICONDATA pnid;
  32. pnid.cbSize = sizeof(NOTIFYICONDATA);
  33. pnid.hWnd = hWnd;
  34. pnid.uID = ICON_NOTIFY;
  35. pnid.uFlags = NIF_ICON | NIF_MESSAGE | NIF_TIP;
  36. pnid.uCallbackMessage = WM_NOTIFYICON;
  37. pnid.hIcon = hIcon;
  38. if (pwszTip) {
  39. StringCchCopy(pnid.szTip, ARRAYSIZE(pnid.szTip), pwszTip);
  40. } else {
  41. *pnid.szTip = 0;
  42. }
  43. return Shell_NotifyIcon(NIM_ADD, &pnid);
  44. }
  45. /*++
  46. Routine Description:
  47. Removes the specified icon from the system tray.
  48. Arguments:
  49. hWnd - Parent window handle.
  50. Return Value:
  51. TRUE on success, FALSE otherwise.
  52. --*/
  53. BOOL
  54. RemoveFromTray(
  55. IN HWND hWnd
  56. )
  57. {
  58. NOTIFYICONDATA pnid;
  59. pnid.cbSize = sizeof(NOTIFYICONDATA);
  60. pnid.hWnd = hWnd;
  61. pnid.uID = ICON_NOTIFY;
  62. return Shell_NotifyIcon(NIM_DELETE, &pnid);
  63. }
  64. /*++
  65. Routine Description:
  66. Displays a popup menu for the tray icon.
  67. Arguments:
  68. hWnd - Main window handle.
  69. Return Value:
  70. TRUE on success, FALSE otherwise.
  71. --*/
  72. BOOL
  73. DisplayMenu(
  74. IN HWND hWnd
  75. )
  76. {
  77. MENUITEMINFO mii;
  78. HMENU hMenu;
  79. POINT pt;
  80. BOOL bReturn = FALSE;
  81. const WCHAR wszItemOne[] = L"&Restore";
  82. const WCHAR wszItemTwo[] = L"E&xit";
  83. hMenu = CreatePopupMenu();
  84. if (hMenu) {
  85. mii.cbSize = sizeof(MENUITEMINFO);
  86. mii.fMask = MIIM_DATA | MIIM_ID | MIIM_TYPE | MIIM_STATE;
  87. mii.fType = MFT_STRING;
  88. mii.wID = IDM_RESTORE;
  89. mii.hSubMenu = NULL;
  90. mii.hbmpChecked = NULL;
  91. mii.hbmpUnchecked = NULL;
  92. mii.dwItemData = 0;
  93. mii.dwTypeData = (LPWSTR)wszItemOne;
  94. mii.cch = ARRAYSIZE(wszItemOne);
  95. mii.fState = MFS_ENABLED;
  96. InsertMenuItem(hMenu, 0, TRUE, &mii);
  97. mii.cbSize = sizeof(MENUITEMINFO);
  98. mii.fMask = MIIM_TYPE;
  99. mii.fType = MFT_SEPARATOR;
  100. mii.hSubMenu = NULL;
  101. mii.hbmpChecked = NULL;
  102. mii.hbmpUnchecked = NULL;
  103. mii.dwItemData = 0;
  104. InsertMenuItem(hMenu, 1, TRUE, &mii);
  105. mii.cbSize = sizeof(MENUITEMINFO);
  106. mii.fMask = MIIM_DATA | MIIM_ID | MIIM_TYPE | MIIM_STATE;
  107. mii.fType = MFT_STRING;
  108. mii.wID = IDM_EXIT;
  109. mii.hSubMenu = NULL;
  110. mii.hbmpChecked = NULL;
  111. mii.hbmpUnchecked = NULL;
  112. mii.dwItemData = 0;
  113. mii.dwTypeData = (LPWSTR)wszItemTwo;
  114. mii.cch = ARRAYSIZE(wszItemTwo);
  115. mii.fState = MFS_ENABLED;
  116. InsertMenuItem(hMenu, 2, TRUE, &mii);
  117. GetCursorPos(&pt);
  118. bReturn = TrackPopupMenuEx(hMenu,
  119. TPM_CENTERALIGN | TPM_RIGHTBUTTON,
  120. pt.x,
  121. pt.y,
  122. hWnd,
  123. NULL);
  124. DestroyMenu(hMenu);
  125. }
  126. return bReturn;
  127. }