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.

423 lines
11 KiB

  1. /*++
  2. Copyright (c) Microsoft Corporation. All rights reserved.
  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. 01/11/2002 rparsons Cleaned up
  13. --*/
  14. #include "precomp.h"
  15. APPINFO g_ai;
  16. /*++
  17. Routine Description:
  18. Application entry point.
  19. Arguments:
  20. hInstance - App instance handle.
  21. hPrevInstance - Always NULL.
  22. lpCmdLine - Pointer to the command line.
  23. nCmdShow - Window show flag.
  24. Return Value:
  25. The wParam of the message or 0 on failure.
  26. --*/
  27. int
  28. APIENTRY
  29. WinMain(
  30. IN HINSTANCE hInstance,
  31. IN HINSTANCE hPrevInstance,
  32. IN LPSTR lpCmdLine,
  33. IN int nCmdShow
  34. )
  35. {
  36. MSG msg;
  37. WNDCLASS wndclass;
  38. WCHAR wszError[MAX_PATH];
  39. RECT rcDialog;
  40. RECT rcDesktopWorkArea;
  41. INITCOMMONCONTROLSEX icex;
  42. POINT pt;
  43. HANDLE hMutex;
  44. int nWidth = 0, nHeight = 0;
  45. int nTaskbarHeight = 0;
  46. OSVERSIONINFOEX osvi = {0};
  47. UNREFERENCED_PARAMETER(lpCmdLine);
  48. UNREFERENCED_PARAMETER(hPrevInstance);
  49. g_ai.hInstance = hInstance;
  50. //
  51. // Make sure we're the only instance running.
  52. //
  53. hMutex = CreateMutex(NULL, FALSE, L"ShimViewer");
  54. if (ERROR_ALREADY_EXISTS == GetLastError()) {
  55. return 0;
  56. }
  57. osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
  58. if (!GetVersionEx((OSVERSIONINFO*)&osvi)) {
  59. MessageBox(
  60. NULL,
  61. L"Failed to get the OS version info",
  62. L"Error",
  63. MB_ICONERROR);
  64. return -1;
  65. }
  66. if (osvi.dwPlatformId == VER_PLATFORM_WIN32_NT &&
  67. osvi.dwMajorVersion == 5 &&
  68. osvi.dwMinorVersion >= 2) {
  69. g_ai.bUsingNewShimEng = TRUE;
  70. //
  71. // If we are using shimeng from NT 5.2 or newer we need
  72. // to create the debug objects to get debug spew from
  73. // OutputDebugString.
  74. //
  75. if (!CreateDebugObjects()) {
  76. MessageBox(
  77. NULL,
  78. L"Failed to create the necessary objects to get debug spew "
  79. L"from OutputDebugString",
  80. L"Error",
  81. MB_ICONERROR);
  82. return 0;
  83. }
  84. } else {
  85. g_ai.bUsingNewShimEng = FALSE;
  86. }
  87. wndclass.style = CS_HREDRAW | CS_VREDRAW;
  88. wndclass.lpfnWndProc = (WNDPROC)MainWndProc;
  89. wndclass.cbClsExtra = 0;
  90. wndclass.cbWndExtra = DLGWINDOWEXTRA;
  91. wndclass.hInstance = hInstance;
  92. wndclass.hIcon = NULL;
  93. wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
  94. wndclass.hbrBackground = (HBRUSH)(COLOR_BTNFACE + 1);
  95. wndclass.lpszMenuName = NULL;
  96. wndclass.lpszClassName = APP_CLASS;
  97. if (!RegisterClass(&wndclass)) {
  98. LoadString(hInstance, IDS_NO_CLASS, wszError, ARRAYSIZE(wszError));
  99. MessageBox(NULL, wszError, APP_NAME, MB_ICONERROR);
  100. return 0;
  101. }
  102. //
  103. // Set up the common controls.
  104. //
  105. icex.dwSize = sizeof(INITCOMMONCONTROLSEX);
  106. icex.dwICC = ICC_LISTVIEW_CLASSES;
  107. if (!InitCommonControlsEx(&icex)) {
  108. InitCommonControls();
  109. }
  110. //
  111. // Get application settings from the registry, if there are any.
  112. //
  113. GetSaveSettings(FALSE);
  114. g_ai.hMainDlg = CreateDialog(hInstance,
  115. (LPCWSTR)IDD_MAIN,
  116. NULL,
  117. MainWndProc);
  118. if (!g_ai.hMainDlg) {
  119. LoadString(hInstance, IDS_NO_MAIN_DLG, wszError, ARRAYSIZE(wszError));
  120. MessageBox(NULL, wszError, APP_NAME, MB_ICONERROR);
  121. return 0;
  122. }
  123. //
  124. // Get the window position info from the registry, if it's there.
  125. //
  126. GetSavePositionInfo(FALSE, &pt);
  127. //
  128. // If previous settings were retrieved from the registry, use them.
  129. //
  130. if (pt.x != 0) {
  131. SetWindowPos(g_ai.hMainDlg,
  132. g_ai.fOnTop ? HWND_TOPMOST : HWND_NOTOPMOST,
  133. pt.x,
  134. pt.y,
  135. 0,
  136. 0,
  137. SWP_NOSIZE | SWP_SHOWWINDOW);
  138. } else {
  139. //
  140. // Get the coords of the desktop window and place the dialog.
  141. // We put it in the bottom-right corner of the desktop above
  142. // the taskbar.
  143. //
  144. SystemParametersInfo(SPI_GETWORKAREA, 0, &rcDesktopWorkArea, 0);
  145. GetWindowRect(g_ai.hMainDlg, &rcDialog);
  146. nWidth = rcDialog.right - rcDialog.left;
  147. nHeight = rcDialog.bottom - rcDialog.top;
  148. SetWindowPos(g_ai.hMainDlg,
  149. g_ai.fOnTop ? HWND_TOPMOST : HWND_NOTOPMOST,
  150. rcDesktopWorkArea.right - nWidth,
  151. rcDesktopWorkArea.bottom - nHeight,
  152. 0,
  153. 0,
  154. SWP_NOSIZE | SWP_SHOWWINDOW);
  155. }
  156. ShowWindow(g_ai.hMainDlg, g_ai.fMinimize ? SW_MINIMIZE : SW_SHOWNORMAL);
  157. while (GetMessage(&msg, (HWND)NULL, 0, 0)) {
  158. if (!IsDialogMessage(g_ai.hMainDlg, &msg)) {
  159. TranslateMessage(&msg);
  160. DispatchMessage(&msg);
  161. }
  162. }
  163. return (int)msg.wParam;
  164. }
  165. /*++
  166. Routine Description:
  167. Runs the message loop for the app.
  168. Arguments:
  169. hWnd - Handle to the window.
  170. uMsg - Windows message.
  171. wParam - Additional message info.
  172. lParam - Additional message info.
  173. Return Value:
  174. TRUE if handled, FALSE otherwise.
  175. --*/
  176. INT_PTR
  177. CALLBACK
  178. MainWndProc(
  179. IN HWND hWnd,
  180. IN UINT uMsg,
  181. IN WPARAM wParam,
  182. IN LPARAM lParam
  183. )
  184. {
  185. switch (uMsg) {
  186. case WM_INITDIALOG:
  187. {
  188. WCHAR wszError[MAX_PATH];
  189. HICON hIcon;
  190. //
  191. // Initialize the list view, menu items, and then create our thread.
  192. //
  193. g_ai.hWndList = GetDlgItem(hWnd, IDC_LIST);
  194. InitListViewColumn();
  195. hIcon = LoadIcon(g_ai.hInstance, MAKEINTRESOURCE(IDI_APPICON));
  196. SetClassLongPtr(hWnd, GCLP_HICON, (LONG_PTR)hIcon);
  197. CheckMenuItem(GetMenu(hWnd),
  198. IDM_ON_TOP,
  199. g_ai.fOnTop ? MF_CHECKED : MF_UNCHECKED);
  200. CheckMenuItem(GetMenu(hWnd),
  201. IDM_START_SMALL,
  202. g_ai.fMinimize ? MF_CHECKED : MF_UNCHECKED);
  203. CheckMenuItem(GetMenu(hWnd),
  204. IDM_MONITOR,
  205. g_ai.fMonitor ? MF_CHECKED : MF_UNCHECKED);
  206. if (g_ai.fMonitor) {
  207. if (!CreateReceiveThread()) {
  208. LoadString(g_ai.hInstance,
  209. IDS_CREATE_FAILED,
  210. wszError,
  211. ARRAYSIZE(wszError));
  212. MessageBox(hWnd, wszError, APP_NAME, MB_ICONERROR);
  213. g_ai.fMonitor = FALSE;
  214. } else {
  215. SetPriorityClass(GetCurrentProcess(), ABOVE_NORMAL_PRIORITY_CLASS);
  216. }
  217. }
  218. break;
  219. }
  220. case WM_CLOSE:
  221. {
  222. RECT rc;
  223. GetWindowRect(hWnd, &rc);
  224. GetSavePositionInfo(TRUE, (LPPOINT)&rc);
  225. GetSaveSettings(TRUE);
  226. RemoveFromTray(hWnd);
  227. EndDialog(hWnd, 0);
  228. PostQuitMessage(0);
  229. break;
  230. }
  231. case WM_SIZE:
  232. {
  233. HICON hIcon;
  234. if (SIZE_MINIMIZED == wParam) {
  235. ShowWindow(hWnd, SW_HIDE);
  236. hIcon = (HICON)LoadImage(g_ai.hInstance,
  237. MAKEINTRESOURCE(IDI_APPICON),
  238. IMAGE_ICON,
  239. 16,
  240. 16,
  241. 0);
  242. AddIconToTray(hWnd, hIcon, APP_NAME);
  243. return TRUE;
  244. }
  245. MoveWindow(g_ai.hWndList, 0, 0, LOWORD(lParam), HIWORD(lParam), TRUE);
  246. break;
  247. }
  248. case WM_NOTIFYICON:
  249. switch (lParam) {
  250. case WM_RBUTTONUP:
  251. DisplayMenu(hWnd);
  252. break;
  253. case WM_LBUTTONDBLCLK:
  254. RemoveFromTray(hWnd);
  255. ShowWindow(hWnd, SW_SHOWNORMAL);
  256. break;
  257. default:
  258. break;
  259. }
  260. case WM_COMMAND:
  261. switch (LOWORD(wParam)) {
  262. case IDM_EXIT:
  263. PostMessage(hWnd, WM_CLOSE, 0, 0);
  264. break;
  265. case IDM_RESTORE:
  266. ShowWindow(hWnd, SW_SHOWNORMAL);
  267. SetWindowPos(hWnd,
  268. g_ai.fOnTop ? HWND_TOPMOST : HWND_NOTOPMOST,
  269. 0,
  270. 0,
  271. 0,
  272. 0,
  273. SWP_NOSIZE | SWP_NOMOVE);
  274. RemoveFromTray(hWnd);
  275. break;
  276. case IDM_ABOUT:
  277. ShellAbout(hWnd,
  278. APP_NAME,
  279. WRITTEN_BY,
  280. LoadIcon(g_ai.hInstance, MAKEINTRESOURCE(IDI_APPICON)));
  281. break;
  282. case IDM_MONITOR:
  283. CheckMenuItem(GetMenu(hWnd),
  284. IDM_MONITOR,
  285. g_ai.fMonitor ? MF_UNCHECKED : MF_CHECKED);
  286. g_ai.fMonitor = g_ai.fMonitor ? FALSE : TRUE;
  287. if (g_ai.fMonitor) {
  288. CreateReceiveThread();
  289. }
  290. break;
  291. case IDM_ON_TOP:
  292. CheckMenuItem(GetMenu(hWnd),
  293. IDM_ON_TOP,
  294. g_ai.fOnTop ? MF_UNCHECKED : MF_CHECKED);
  295. SetWindowPos(hWnd,
  296. g_ai.fOnTop ? HWND_NOTOPMOST : HWND_TOPMOST,
  297. 0,
  298. 0,
  299. 0,
  300. 0,
  301. SWP_NOSIZE | SWP_NOMOVE);
  302. g_ai.fOnTop = g_ai.fOnTop ? FALSE : TRUE;
  303. break;
  304. case IDM_START_SMALL:
  305. CheckMenuItem(GetMenu(hWnd),
  306. IDM_START_SMALL,
  307. g_ai.fMinimize ? MF_UNCHECKED : MF_CHECKED);
  308. g_ai.fMinimize = g_ai.fMinimize ? FALSE : TRUE;
  309. break;
  310. case IDM_CLEAR:
  311. ListView_DeleteAllItems(g_ai.hWndList);
  312. break;
  313. default:
  314. break;
  315. }
  316. default:
  317. break;
  318. }
  319. return FALSE;
  320. }