Source code of Windows XP (NT5)
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.

186 lines
4.2 KiB

  1. /*++
  2. Copyright (c) 2001 Microsoft Corporation
  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. --*/
  12. #include "precomp.h"
  13. /*++
  14. Routine Description:
  15. Adds the specified icon to the system tray
  16. Arguments:
  17. hWnd - Parent window handle
  18. hIcon - Icon hanle to add to the tray
  19. lpwTip - Tooltip to associate with the icon
  20. Return Value:
  21. TRUE on success, FALSE otherwise
  22. --*/
  23. BOOL
  24. AddIconToTray(
  25. IN HWND hWnd,
  26. IN HICON hIcon,
  27. IN LPCWSTR lpwTip
  28. )
  29. {
  30. NOTIFYICONDATA pnid;
  31. BOOL fReturn = FALSE;
  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 (lpwTip) {
  39. wcsncpy(pnid.szTip, lpwTip, wcslen(lpwTip)*sizeof(WCHAR));
  40. } else {
  41. pnid.szTip[0] = '\0';
  42. }
  43. fReturn = Shell_NotifyIcon(NIM_ADD, &pnid);
  44. if (hIcon) {
  45. DestroyIcon(hIcon);
  46. }
  47. return (fReturn ? TRUE : FALSE);
  48. }
  49. /*++
  50. Routine Description:
  51. Removes the specified icon from the system tray
  52. Arguments:
  53. hWnd - Parent window handle
  54. Return Value:
  55. TRUE on success, FALSE otherwise
  56. --*/
  57. BOOL
  58. RemoveFromTray(
  59. IN HWND hWnd
  60. )
  61. {
  62. NOTIFYICONDATA pnid;
  63. BOOL fReturn = FALSE;
  64. pnid.cbSize = sizeof(NOTIFYICONDATA);
  65. pnid.hWnd = hWnd;
  66. pnid.uID = ICON_NOTIFY;
  67. fReturn = Shell_NotifyIcon(NIM_DELETE, &pnid);
  68. return (fReturn);
  69. }
  70. /*++
  71. Routine Description:
  72. Displays a popup menu for the tray icon.
  73. Arguments:
  74. hWnd - Main window handle
  75. Return Value:
  76. TRUE on success, FALSE otherwise.
  77. --*/
  78. BOOL
  79. DisplayMenu(
  80. IN HWND hWnd
  81. )
  82. {
  83. MENUITEMINFO mii;
  84. HMENU hMenu = NULL, hPopupMenu = NULL;
  85. POINT pt;
  86. BOOL fReturn = FALSE;
  87. hMenu = CreatePopupMenu();
  88. if (hMenu) {
  89. mii.cbSize = sizeof(MENUITEMINFO);
  90. mii.fMask = MIIM_DATA | MIIM_ID | MIIM_TYPE | MIIM_STATE;
  91. mii.fType = MFT_STRING;
  92. mii.wID = IDM_RESTORE;
  93. mii.hSubMenu = NULL;
  94. mii.hbmpChecked = NULL;
  95. mii.hbmpUnchecked = NULL;
  96. mii.dwItemData = 0L;
  97. mii.dwTypeData = L"&Restore";
  98. mii.cch = 14;
  99. mii.fState = MFS_ENABLED;
  100. InsertMenuItem(hMenu, 0, TRUE, &mii);
  101. mii.cbSize = sizeof(MENUITEMINFO);
  102. mii.fMask = MIIM_TYPE;
  103. mii.fType = MFT_SEPARATOR;
  104. mii.hSubMenu = NULL;
  105. mii.hbmpChecked = NULL;
  106. mii.hbmpUnchecked = NULL;
  107. mii.dwItemData = 0L;
  108. InsertMenuItem(hMenu, 1, TRUE, &mii);
  109. mii.cbSize = sizeof(MENUITEMINFO);
  110. mii.fMask = MIIM_DATA | MIIM_ID | MIIM_TYPE | MIIM_STATE;
  111. mii.fType = MFT_STRING;
  112. mii.wID = IDM_EXIT;
  113. mii.hSubMenu = NULL;
  114. mii.hbmpChecked = NULL;
  115. mii.hbmpUnchecked = NULL;
  116. mii.dwItemData = 0L;
  117. mii.dwTypeData = L"E&xit";
  118. mii.cch = 10;
  119. mii.fState = MFS_ENABLED;
  120. InsertMenuItem(hMenu, 2, TRUE, &mii);
  121. // Get the coordinates of the cursor
  122. GetCursorPos(&pt);
  123. // Show the popup menu - the menu is left aligned and
  124. // we're tracking the left button or right button
  125. fReturn = TrackPopupMenuEx(hMenu, TPM_CENTERALIGN | TPM_RIGHTBUTTON,
  126. pt.x, pt.y, hWnd, NULL);
  127. }
  128. if (hMenu) {
  129. DestroyMenu(hMenu);
  130. }
  131. return (fReturn);
  132. }