UTIL.C
上传用户:bangxh
上传日期:2007-01-31
资源大小:42235k
文件大小:3k
源码类别:

Windows编程

开发平台:

Visual C++

  1. //+---------------------------------------------------------------------------
  2. //
  3. //  Microsoft Windows
  4. //  Copyright 1992 - 1997 Microsoft Corporation.
  5. //
  6. //  File:       util.c
  7. //
  8. //  Contents:
  9. //
  10. //  Classes:
  11. //
  12. //  Functions:
  13. //
  14. //  History:    4-20-95   RichardW   Created
  15. //
  16. //----------------------------------------------------------------------------
  17. #include "gina.h"
  18. #pragma hdrstop
  19. HMODULE hNetMsg = NULL;
  20. //+---------------------------------------------------------------------------
  21. //
  22. //  Function:   CenterWindow
  23. //
  24. //  Synopsis:   Centers a window
  25. //
  26. //  Arguments:  [hwnd] --
  27. //
  28. //  Notes:
  29. //
  30. //----------------------------------------------------------------------------
  31. VOID
  32. CenterWindow(
  33.     HWND    hwnd
  34.     )
  35. {
  36.     RECT    rect;
  37.     LONG    dx, dy;
  38.     LONG    dxParent, dyParent;
  39.     LONG    Style;
  40.     // Get window rect
  41.     GetWindowRect(hwnd, &rect);
  42.     dx = rect.right - rect.left;
  43.     dy = rect.bottom - rect.top;
  44.     // Get parent rect
  45.     Style = GetWindowLong(hwnd, GWL_STYLE);
  46.     if ((Style & WS_CHILD) == 0) {
  47.         // Return the desktop windows size (size of main screen)
  48.         dxParent = GetSystemMetrics(SM_CXSCREEN);
  49.         dyParent = GetSystemMetrics(SM_CYSCREEN);
  50.     } else {
  51.         HWND    hwndParent;
  52.         RECT    rectParent;
  53.         hwndParent = GetParent(hwnd);
  54.         if (hwndParent == NULL) {
  55.             hwndParent = GetDesktopWindow();
  56.         }
  57.         GetWindowRect(hwndParent, &rectParent);
  58.         dxParent = rectParent.right - rectParent.left;
  59.         dyParent = rectParent.bottom - rectParent.top;
  60.     }
  61.     // Centre the child in the parent
  62.     rect.left = (dxParent - dx) / 2;
  63.     rect.top  = (dyParent - dy) / 3;
  64.     // Move the child into position
  65.     SetWindowPos(hwnd, HWND_TOPMOST, rect.left, rect.top, 0, 0, SWP_NOSIZE);
  66.     SetForegroundWindow(hwnd);
  67. }
  68. int
  69. ErrorMessage(
  70.     HWND        hWnd,
  71.     PWSTR       pszTitleBar,
  72.     DWORD       Buttons)
  73. {
  74.     WCHAR   szMessage[256];
  75.     DWORD   GLE;
  76.     GLE = GetLastError();
  77.     if (GLE >= NERR_BASE)
  78.     {
  79.         if (!hNetMsg)
  80.         {
  81.             hNetMsg = LoadLibrary(TEXT("netmsg.dll"));
  82.         }
  83.         FormatMessage(
  84.             FORMAT_MESSAGE_FROM_HMODULE | FORMAT_MESSAGE_IGNORE_INSERTS,
  85.             hNetMsg,                               // ignored
  86.             GLE,                                  // message id
  87.             MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US),   // message language
  88.             szMessage,                  // address of buffer pointer
  89.             199,                                  // minimum buffer size
  90.             NULL );                              // no other arguments
  91.     }
  92.     FormatMessage(
  93.             FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
  94.             NULL,                               // ignored
  95.             (GetLastError()),                     // message id
  96.             MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US),   // message language
  97.             szMessage,                  // address of buffer pointer
  98.             199,                                  // minimum buffer size
  99.             NULL );                              // no other arguments
  100.     return(MessageBox(hWnd, szMessage, pszTitleBar, Buttons));
  101. }