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.

301 lines
7.1 KiB

  1. /*++
  2. Copyright (c) 1994 Microsoft Corporation
  3. Module Name:
  4. WinUtils.C
  5. Abstract:
  6. Windows utility functions
  7. Author:
  8. Bob Watson (a-robw)
  9. Revision History:
  10. 24 Jun 94 Written
  11. --*/
  12. //
  13. // Windows Include Files
  14. //
  15. #include <windows.h>
  16. #include <stdio.h>
  17. #include <malloc.h>
  18. #include <tchar.h> // unicode macros
  19. #include <stdlib.h> // string to number conversions
  20. #include <shellapi.h> // for common about box function
  21. //
  22. // app include files
  23. //
  24. #include "otnboot.h"
  25. #include "otnbtdlg.h"
  26. BOOL
  27. Dlg_WM_SYSCOMMAND (
  28. IN HWND hwndDlg,
  29. IN WPARAM wParam,
  30. IN LPARAM lParam
  31. )
  32. /*++
  33. Routine Description:
  34. Processes system command (e.g. from system menu) messages sent to
  35. dialog boxes:
  36. SC_CLOSE: closes the application
  37. SC_MINIMIZE: saves the dlg caption, set's the window
  38. caption to the app name, then minimizes the dlg.
  39. SC_RESTORE: resets the dlg caption to the store name and
  40. restores the dlg window
  41. NCDU_ID_ABOUT: displays the appropriate about box
  42. Arguments:
  43. Std. Window message args
  44. Return Value:
  45. TRUE if message is processed by this routine
  46. FALSE if not
  47. --*/
  48. {
  49. LPTSTR szWindowName;
  50. static TCHAR szLastDlgTitle[MAX_PATH];
  51. switch (wParam & 0x0000FFF0) {
  52. case SC_CLOSE:
  53. PostMessage (GetParent(hwndDlg), NCDU_SHOW_EXIT_MESSAGE_DLG, 0, 0);
  54. SetCursor(LoadCursor(NULL, IDC_WAIT));
  55. return TRUE; // done
  56. case SC_MINIMIZE:
  57. SetSysMenuMinimizeEntryState (hwndDlg, FALSE); // disable minimize
  58. // save dialog title for restoration
  59. GetWindowText (hwndDlg, szLastDlgTitle, MAX_PATH);
  60. // set title to app name
  61. szWindowName = (LPTSTR)GlobalAlloc(GPTR, MAX_PATH_BYTES);
  62. if (szWindowName != NULL) {
  63. #ifdef TERMSRV
  64. if (_tcschr(GetCommandLine(),TEXT('/')) != NULL )
  65. {
  66. if (LoadString (
  67. (HINSTANCE)GetWindowLongPtr(hwndDlg, GWLP_HINSTANCE),
  68. WFC_STRING_BASE,
  69. szWindowName,
  70. MAX_PATH) > 0) {
  71. SetWindowText (hwndDlg, szWindowName);
  72. }
  73. }
  74. else
  75. {
  76. #endif // TERMSRV
  77. if (LoadString (
  78. (HINSTANCE)GetWindowLongPtr(hwndDlg, GWLP_HINSTANCE),
  79. STRING_BASE,
  80. szWindowName,
  81. MAX_PATH) > 0) {
  82. SetWindowText (hwndDlg, szWindowName);
  83. }
  84. #ifdef TERMSRV
  85. }
  86. #endif // TERMSRV
  87. FREE_IF_ALLOC (szWindowName);
  88. }
  89. // minimize window
  90. ShowWindow (hwndDlg, SW_MINIMIZE);
  91. return TRUE;
  92. case SC_RESTORE:
  93. SetSysMenuMinimizeEntryState (hwndDlg, TRUE); // enable minimize
  94. if (szLastDlgTitle[0] != 0) SetWindowText (hwndDlg, szLastDlgTitle);
  95. szLastDlgTitle[0] = 0; // clear buffer
  96. ShowWindow (hwndDlg, SW_RESTORE);
  97. return ERROR_SUCCESS;
  98. case NCDU_ID_ABOUT:
  99. lstrcpy (szLastDlgTitle, GetStringResource (CSZ_ABOUT_TITLE));
  100. lstrcat (szLastDlgTitle, cszPoundSign);
  101. ShellAbout (hwndDlg,
  102. szLastDlgTitle,
  103. GetStringResource (SZ_APP_TITLE),
  104. LoadIcon (
  105. (HINSTANCE)GetWindowLongPtr(hwndDlg, GWLP_HINSTANCE),
  106. MAKEINTRESOURCE (NCDU_APP_ICON)));
  107. szLastDlgTitle[0] = 0;
  108. return TRUE;
  109. default:
  110. return FALSE;
  111. }
  112. }
  113. LRESULT
  114. Dlg_WM_MOVE (
  115. IN HWND hwndDlg,
  116. IN WPARAM wParam,
  117. IN LPARAM lParam
  118. )
  119. /*++
  120. Routine Description:
  121. updates the current position of the window, whenever the window
  122. is moved so the next window (dialog) can be position in the same
  123. location.
  124. Arguments:
  125. IN HWND hwndDlg
  126. Handle to dialog window
  127. IN WPARAM wParam
  128. Not used
  129. IN LPARAM lParam
  130. Not used
  131. Return Value:
  132. ERROR_SUCCESS
  133. --*/
  134. {
  135. RECT rWnd;
  136. GetWindowRect(hwndDlg, &rWnd);
  137. PostMessage (GetParent(hwndDlg), NCDU_UPDATE_WINDOW_POS,
  138. (WPARAM)rWnd.left, (LPARAM)rWnd.top);
  139. return (LRESULT)ERROR_SUCCESS;
  140. }
  141. BOOL
  142. Dlg_WM_PAINT (
  143. IN HWND hwndDlg,
  144. IN WPARAM wParam,
  145. IN LPARAM lParam
  146. )
  147. /*++
  148. Routine Description:
  149. paints app icon in minimized dialog box window.
  150. Arguments:
  151. Std. Window message arguments
  152. Return Value:
  153. TRUE if message is processed by this routine
  154. FALSE if not.
  155. --*/
  156. {
  157. PAINTSTRUCT ps;
  158. if (IsIconic(hwndDlg)) {
  159. HICON hIcon;
  160. hIcon =
  161. LoadIcon(
  162. (HINSTANCE)GetWindowLongPtr(GetParent(hwndDlg), GWLP_HINSTANCE),
  163. MAKEINTRESOURCE(NCDU_APP_ICON));
  164. if( hIcon != NULL ) {
  165. BOOL bDI;
  166. BeginPaint (hwndDlg, &ps);
  167. bDI = DrawIcon (ps.hdc, 0, 0, hIcon);
  168. EndPaint (hwndDlg, &ps);
  169. return bDI;
  170. }
  171. }
  172. return FALSE;
  173. }
  174. BOOL
  175. CenterWindow (
  176. HWND hwndChild,
  177. HWND hwndParent
  178. )
  179. /*++
  180. Routine Description:
  181. Centers the child window in the Parent window
  182. Arguments:
  183. HWND hwndChild,
  184. handle of child window to center
  185. HWND hwndParent
  186. handle of parent window to center child window in
  187. ReturnValue:
  188. Return value of SetWindowPos
  189. --*/
  190. {
  191. RECT rChild, rParent;
  192. LONG wChild, hChild, wParent, hParent;
  193. LONG wScreen, hScreen, xNew, yNew;
  194. HDC hdc;
  195. // Get the Height and Width of the child window
  196. GetWindowRect (hwndChild, &rChild);
  197. wChild = rChild.right - rChild.left;
  198. hChild = rChild.bottom - rChild.top;
  199. // Get the Height and Width of the parent window
  200. GetWindowRect (hwndParent, &rParent);
  201. wParent = rParent.right - rParent.left;
  202. hParent = rParent.bottom - rParent.top;
  203. // Get the display limits
  204. hdc = GetDC (hwndChild);
  205. if( hdc != NULL ) {
  206. wScreen = GetDeviceCaps (hdc, HORZRES);
  207. hScreen = GetDeviceCaps (hdc, VERTRES);
  208. ReleaseDC (hwndChild, hdc);
  209. // Calculate new X position, then adjust for screen
  210. xNew = rParent.left + ((wParent - wChild) /2);
  211. if (xNew < 0) {
  212. xNew = 0;
  213. } else if ((xNew+wChild) > wScreen) {
  214. xNew = wScreen - wChild;
  215. }
  216. // Calculate new Y position, then adjust for screen
  217. yNew = rParent.top + ((hParent - hChild) /2);
  218. if (yNew < 0) {
  219. yNew = 0;
  220. } else if ((yNew+hChild) > hScreen) {
  221. yNew = hScreen - hChild;
  222. }
  223. }
  224. else {
  225. xNew = 0;
  226. yNew = 0;
  227. }
  228. // Set it, and return
  229. return SetWindowPos (hwndChild, NULL,
  230. (int)xNew, (int)yNew, 0, 0, SWP_NOSIZE | SWP_NOZORDER);
  231. }