home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / sdk / winbase / security / winnt / gina / util.c < prev    next >
C/C++ Source or Header  |  1997-10-09  |  3KB  |  123 lines

  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.  
  18. #include "gina.h"
  19. #pragma hdrstop
  20.  
  21. HMODULE hNetMsg = NULL;
  22.  
  23. //+---------------------------------------------------------------------------
  24. //
  25. //  Function:   CenterWindow
  26. //
  27. //  Synopsis:   Centers a window
  28. //
  29. //  Arguments:  [hwnd] --
  30. //
  31. //  Notes:
  32. //
  33. //----------------------------------------------------------------------------
  34. VOID
  35. CenterWindow(
  36.     HWND    hwnd
  37.     )
  38. {
  39.     RECT    rect;
  40.     LONG    dx, dy;
  41.     LONG    dxParent, dyParent;
  42.     LONG    Style;
  43.  
  44.     // Get window rect
  45.     GetWindowRect(hwnd, &rect);
  46.  
  47.     dx = rect.right - rect.left;
  48.     dy = rect.bottom - rect.top;
  49.  
  50.     // Get parent rect
  51.     Style = GetWindowLong(hwnd, GWL_STYLE);
  52.     if ((Style & WS_CHILD) == 0) {
  53.  
  54.         // Return the desktop windows size (size of main screen)
  55.         dxParent = GetSystemMetrics(SM_CXSCREEN);
  56.         dyParent = GetSystemMetrics(SM_CYSCREEN);
  57.     } else {
  58.         HWND    hwndParent;
  59.         RECT    rectParent;
  60.  
  61.         hwndParent = GetParent(hwnd);
  62.         if (hwndParent == NULL) {
  63.             hwndParent = GetDesktopWindow();
  64.         }
  65.  
  66.         GetWindowRect(hwndParent, &rectParent);
  67.  
  68.         dxParent = rectParent.right - rectParent.left;
  69.         dyParent = rectParent.bottom - rectParent.top;
  70.     }
  71.  
  72.     // Centre the child in the parent
  73.     rect.left = (dxParent - dx) / 2;
  74.     rect.top  = (dyParent - dy) / 3;
  75.  
  76.     // Move the child into position
  77.     SetWindowPos(hwnd, HWND_TOPMOST, rect.left, rect.top, 0, 0, SWP_NOSIZE);
  78.  
  79.     SetForegroundWindow(hwnd);
  80. }
  81.  
  82.  
  83. int
  84. ErrorMessage(
  85.     HWND        hWnd,
  86.     PWSTR       pszTitleBar,
  87.     DWORD       Buttons)
  88. {
  89.     WCHAR   szMessage[256];
  90.     DWORD   GLE;
  91.  
  92.     GLE = GetLastError();
  93.  
  94.     if (GLE >= NERR_BASE)
  95.     {
  96.         if (!hNetMsg)
  97.         {
  98.             hNetMsg = LoadLibrary(TEXT("netmsg.dll"));
  99.         }
  100.         FormatMessage(
  101.             FORMAT_MESSAGE_FROM_HMODULE | FORMAT_MESSAGE_IGNORE_INSERTS,
  102.             hNetMsg,                               // ignored
  103.             GLE,                                  // message id
  104.             MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US),   // message language
  105.             szMessage,                  // address of buffer pointer
  106.             199,                                  // minimum buffer size
  107.             NULL );                              // no other arguments
  108.  
  109.     }
  110.  
  111.     FormatMessage(
  112.             FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
  113.             NULL,                               // ignored
  114.             (GetLastError()),                     // message id
  115.             MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US),   // message language
  116.             szMessage,                  // address of buffer pointer
  117.             199,                                  // minimum buffer size
  118.             NULL );                              // no other arguments
  119.  
  120.     return(MessageBox(hWnd, szMessage, pszTitleBar, Buttons));
  121.  
  122. }
  123.