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.

353 lines
9.4 KiB

  1. /*++
  2. Copyright (c) 2001 Microsoft Corporation
  3. Module Name:
  4. Main.cpp
  5. Abstract:
  6. Implements the entry point and message
  7. pump for the application
  8. Notes:
  9. Unicode only
  10. History:
  11. 05/04/2001 rparsons Created
  12. --*/
  13. #include "precomp.h"
  14. // Disable warning about possible loss of data
  15. #pragma warning (disable : 4244)
  16. APPINFO g_ai;
  17. /*++
  18. Routine Description:
  19. Application entry point
  20. Arguments:
  21. hInstance - App instance handle
  22. hPrevInstance - Always NULL
  23. lpCmdLine - Pointer to the command line
  24. nCmdShow - Window show flag
  25. Return Value:
  26. The wParam of the message or 0 on failure
  27. --*/
  28. int
  29. CALLBACK
  30. WinMain(
  31. IN HINSTANCE hInstance,
  32. IN HINSTANCE hPrevInstance,
  33. IN LPSTR lpCmdLine,
  34. IN int nCmdShow
  35. )
  36. {
  37. MSG msg;
  38. WNDCLASS wndclass;
  39. WCHAR wszError[MAX_PATH];
  40. RECT rcDesktop;
  41. RECT rcDialog;
  42. INITCOMMONCONTROLSEX icex;
  43. POINT pt;
  44. HANDLE hMutex;
  45. UNREFERENCED_PARAMETER(lpCmdLine);
  46. UNREFERENCED_PARAMETER(hPrevInstance);
  47. g_ai.hInstance = hInstance;
  48. //
  49. // Make sure we're the only instance running
  50. //
  51. hMutex = CreateMutex(NULL, FALSE, L"ShimViewer");
  52. if (ERROR_ALREADY_EXISTS == GetLastError()) {
  53. return 0;
  54. }
  55. wndclass.style = CS_HREDRAW | CS_VREDRAW;
  56. wndclass.lpfnWndProc = (WNDPROC) MainWndProc;
  57. wndclass.cbClsExtra = 0;
  58. wndclass.cbWndExtra = DLGWINDOWEXTRA;
  59. wndclass.hInstance = hInstance;
  60. wndclass.hIcon = NULL;
  61. wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
  62. wndclass.hbrBackground = (HBRUSH) (COLOR_BTNFACE+1);
  63. wndclass.lpszMenuName = NULL;
  64. wndclass.lpszClassName = APP_CLASS;
  65. if (!RegisterClass(&wndclass))
  66. {
  67. LoadString(hInstance, IDS_NO_CLASS, wszError, sizeof(wszError));
  68. MessageBox(NULL, wszError, APP_NAME, MB_OK | MB_ICONERROR);
  69. return 0;
  70. }
  71. //
  72. // Set up the common controls
  73. //
  74. icex.dwSize = sizeof(INITCOMMONCONTROLSEX);
  75. icex.dwICC = ICC_LISTVIEW_CLASSES;
  76. if (!InitCommonControlsEx(&icex)) {
  77. InitCommonControls();
  78. }
  79. //
  80. // Get application settings from the registry, if there are any
  81. //
  82. GetSaveSettings(FALSE);
  83. g_ai.hMainDlg = CreateDialog(hInstance, (LPCTSTR) IDD_MAIN,
  84. NULL, (DLGPROC)MainWndProc);
  85. if (!g_ai.hMainDlg)
  86. {
  87. LoadString(hInstance, IDS_NO_MAIN_DLG, wszError, sizeof(wszError));
  88. MessageBox(NULL, wszError, APP_NAME, MB_OK | MB_ICONERROR);
  89. return 0;
  90. }
  91. //
  92. // Get the window position info from the registry, if it's there
  93. //
  94. GetSavePositionInfo(FALSE, &pt);
  95. //
  96. // Get the coords of the desktop window and place the dialog
  97. //
  98. GetWindowRect(GetDesktopWindow(), &rcDesktop);
  99. GetWindowRect(g_ai.hMainDlg, &rcDialog);
  100. double nWidth = rcDialog.right - rcDialog.left;
  101. double nHeight = rcDialog.bottom - rcDialog.top;
  102. //
  103. // If nothing was stored in the registry, use default settings
  104. //
  105. if (pt.x != 0) {
  106. //
  107. // If the flag is set, make the window topmost
  108. //
  109. SetWindowPos(g_ai.hMainDlg,
  110. g_ai.fOnTop ? HWND_TOPMOST : HWND_NOTOPMOST,
  111. pt.x, pt.y, 0, 0,
  112. SWP_NOSIZE | SWP_SHOWWINDOW);
  113. } else {
  114. //
  115. // If the flag is set, make the window topmost
  116. //
  117. SetWindowPos(g_ai.hMainDlg,
  118. g_ai.fOnTop ? HWND_TOPMOST : HWND_NOTOPMOST,
  119. (rcDesktop.right / 1.05) - (nWidth / 1.05),
  120. (rcDesktop.bottom / 1.05) - (nHeight / 1.05),
  121. 0, 0,
  122. SWP_NOSIZE | SWP_SHOWWINDOW);
  123. }
  124. //
  125. // If the flag is set, make the window minimized
  126. //
  127. ShowWindow(g_ai.hMainDlg,
  128. g_ai.fMinimize ? SW_MINIMIZE : SW_SHOWNORMAL);
  129. while (GetMessage(&msg, (HWND) NULL, 0, 0)) {
  130. if (!IsDialogMessage(g_ai.hMainDlg, &msg)) {
  131. TranslateMessage(&msg);
  132. DispatchMessage(&msg);
  133. }
  134. }
  135. return msg.wParam;
  136. }
  137. /*++
  138. Routine Description:
  139. Runs the message loop for the app
  140. Arguments:
  141. hWnd - Handle to the window
  142. uMsg - Windows message
  143. wParam - Additional message info
  144. lParam - Additional message info
  145. Return Value:
  146. TRUE if handled, FALSE otherwise
  147. --*/
  148. LRESULT
  149. CALLBACK
  150. MainWndProc(
  151. IN HWND hWnd,
  152. IN UINT uMsg,
  153. IN WPARAM wParam,
  154. IN LPARAM lParam
  155. )
  156. {
  157. switch (uMsg) {
  158. case WM_INITDIALOG:
  159. {
  160. WCHAR wszError[MAX_PATH];
  161. //
  162. // Get the handle to the listview and do some init stuff
  163. //
  164. g_ai.hWndList = GetDlgItem(hWnd, IDC_LIST);
  165. InitListViewColumn();
  166. //
  167. // Set up our icon, initialize the menu items, and create the thread
  168. //
  169. SendMessage(hWnd,
  170. WM_SETICON,
  171. TRUE,
  172. (LPARAM)LoadIcon(g_ai.hInstance, MAKEINTRESOURCE(IDI_APPICON)));
  173. CheckMenuItem(GetMenu(hWnd),
  174. IDM_ON_TOP,
  175. g_ai.fOnTop ? MF_CHECKED : MF_UNCHECKED);
  176. CheckMenuItem(GetMenu(hWnd),
  177. IDM_START_SMALL,
  178. g_ai.fMinimize ? MF_CHECKED : MF_UNCHECKED);
  179. CheckMenuItem(GetMenu(hWnd),
  180. IDM_MONITOR,
  181. g_ai.fMonitor ? MF_CHECKED : MF_UNCHECKED);
  182. if (!CreateReceiveThread())
  183. {
  184. LoadString(g_ai.hInstance, IDS_CREATE_FAILED, wszError, MAX_PATH);
  185. MessageBox(hWnd, wszError, APP_NAME, MB_OK | MB_ICONERROR);
  186. g_ai.fMonitor = FALSE;
  187. } else {
  188. SetPriorityClass(GetCurrentProcess(), ABOVE_NORMAL_PRIORITY_CLASS);
  189. }
  190. break;
  191. }
  192. case WM_CLOSE:
  193. {
  194. RECT rc;
  195. GetWindowRect(hWnd, &rc);
  196. GetSavePositionInfo(TRUE, (LPPOINT)&rc);
  197. GetSaveSettings(TRUE);
  198. RemoveFromTray(hWnd);
  199. EndDialog(hWnd, 0);
  200. PostQuitMessage(0);
  201. break;
  202. }
  203. case WM_SIZE:
  204. if (SIZE_MINIMIZED == wParam) {
  205. ShowWindow(hWnd, SW_HIDE);
  206. AddIconToTray(hWnd,
  207. (HICON) LoadImage(g_ai.hInstance,
  208. MAKEINTRESOURCE(IDI_APPICON),
  209. IMAGE_ICON, 16, 16, 0),
  210. APP_NAME);
  211. return TRUE;
  212. }
  213. MoveWindow(g_ai.hWndList, 0, 0, LOWORD(lParam), HIWORD(lParam), TRUE);
  214. break;
  215. case WM_NOTIFYICON:
  216. switch (lParam) {
  217. case WM_RBUTTONUP:
  218. DisplayMenu(hWnd);
  219. break;
  220. case WM_LBUTTONDBLCLK:
  221. RemoveFromTray(hWnd);
  222. ShowWindow(hWnd, SW_SHOWNORMAL);
  223. break;
  224. }
  225. break;
  226. case WM_COMMAND:
  227. switch (LOWORD(wParam)) {
  228. case IDM_EXIT:
  229. PostMessage(hWnd, WM_CLOSE, 0, 0);
  230. break;
  231. case IDM_RESTORE:
  232. ShowWindow(hWnd, SW_SHOWNORMAL);
  233. SetWindowPos(hWnd, g_ai.fOnTop ? HWND_TOPMOST : HWND_NOTOPMOST,
  234. 0,0,0,0, SWP_NOSIZE | SWP_NOMOVE);
  235. RemoveFromTray(hWnd);
  236. break;
  237. case IDM_ABOUT:
  238. ShellAbout(hWnd, APP_NAME, WRITTEN_BY, LoadIcon(g_ai.hInstance, MAKEINTRESOURCE(IDI_APPICON)));
  239. break;
  240. case IDM_MONITOR:
  241. CheckMenuItem(GetMenu(hWnd),
  242. IDM_MONITOR,
  243. g_ai.fMonitor ? MF_UNCHECKED : MF_CHECKED);
  244. g_ai.fMonitor = g_ai.fMonitor ? FALSE : TRUE;
  245. if (g_ai.fMonitor) {
  246. CreateReceiveThread();
  247. }
  248. break;
  249. case IDM_ON_TOP:
  250. CheckMenuItem(GetMenu(hWnd),
  251. IDM_ON_TOP,
  252. g_ai.fOnTop ? MF_UNCHECKED : MF_CHECKED);
  253. SetWindowPos(hWnd, g_ai.fOnTop ? HWND_NOTOPMOST : HWND_TOPMOST,
  254. 0,0,0,0, SWP_NOSIZE | SWP_NOMOVE);
  255. g_ai.fOnTop = g_ai.fOnTop ? FALSE : TRUE;
  256. break;
  257. case IDM_START_SMALL:
  258. CheckMenuItem(GetMenu(hWnd),
  259. IDM_START_SMALL,
  260. g_ai.fMinimize ? MF_UNCHECKED : MF_CHECKED);
  261. g_ai.fMinimize = g_ai.fMinimize ? FALSE : TRUE;
  262. break;
  263. case IDM_CLEAR:
  264. ListView_DeleteAllItems(g_ai.hWndList);
  265. break;
  266. default:
  267. break;
  268. }
  269. }
  270. return FALSE;
  271. }