home *** CD-ROM | disk | FTP | other *** search
/ PC Plus SuperCD (UK) 2000 May / PCP163A.iso / Runimage / Cbuilder4 / Include / MULTIMON.H < prev    next >
Encoding:
C/C++ Source or Header  |  1999-01-26  |  10.6 KB  |  366 lines

  1. #pragma option push -b -a8 -pc -A- /*P_O_Push_S*/
  2. //=============================================================================
  3. //
  4. // multimon.h -- Stub module that fakes multiple monitor apis on Win32 OSes
  5. //               without them.
  6. //
  7. // By using this header your code will get back default values from
  8. // GetSystemMetrics() for new metrics, and the new multimonitor APIs
  9. // will act like only one display is present on a Win32 OS without
  10. // multimonitor APIs.
  11. //
  12. // Exactly one source must include this with COMPILE_MULTIMON_STUBS defined.
  13. //
  14. // [ This is done for the user in the RTL, the new functionality will always
  15. //   be used if availible on the target machine -- Borland ]
  16. //
  17. // Copyright (c) 1985-1996, Microsoft Corporation
  18. //
  19. //=============================================================================
  20.  
  21. #ifdef __cplusplus
  22. extern "C" {            // Assume C declarations for C++
  23. #endif // __cplusplus
  24.  
  25. //
  26. // If we are building with Win95/NT4 headers, we need to declare
  27. // the multimonitor-related metrics and APIs ourselves.
  28. //
  29. #ifndef SM_CMONITORS
  30.  
  31. #define SM_XVIRTUALSCREEN       76
  32. #define SM_YVIRTUALSCREEN       77
  33. #define SM_CXVIRTUALSCREEN      78
  34. #define SM_CYVIRTUALSCREEN      79
  35. #define SM_CMONITORS            80
  36. #define SM_SAMEDISPLAYFORMAT    81
  37.  
  38. // HMONITOR is already declared if _WIN32_WINNT >= 0x0500 in windef.h
  39. // This is for components built with an older version number.
  40. //
  41. #if !defined(HMONITOR_DECLARED) && (_WIN32_WINNT < 0x0500)
  42. DECLARE_HANDLE(HMONITOR);
  43. #define HMONITOR_DECLARED
  44. #endif
  45.  
  46. #define MONITOR_DEFAULTTONULL       0x00000000
  47. #define MONITOR_DEFAULTTOPRIMARY    0x00000001
  48. #define MONITOR_DEFAULTTONEAREST    0x00000002
  49.  
  50. #define MONITORINFOF_PRIMARY        0x00000001
  51.  
  52. typedef struct tagMONITORINFO
  53. {
  54.     DWORD   cbSize;
  55.     RECT    rcMonitor;
  56.     RECT    rcWork;
  57.     DWORD   dwFlags;
  58. } MONITORINFO, *LPMONITORINFO;
  59.  
  60. #ifndef CCHDEVICENAME
  61. #define CCHDEVICENAME 32
  62. #endif
  63.  
  64. #ifdef __cplusplus
  65. typedef struct tagMONITORINFOEX : public tagMONITORINFO
  66. {
  67.     TCHAR       szDevice[CCHDEVICENAME];
  68. } MONITORINFOEX, *LPMONITORINFOEX;
  69. #else
  70. typedef struct
  71. {
  72.     MONITORINFO;
  73.     TCHAR       szDevice[CCHDEVICENAME];
  74. } MONITORINFOEX, *LPMONITORINFOEX;
  75. #endif
  76.  
  77. typedef BOOL (CALLBACK* MONITORENUMPROC)(HMONITOR, HDC, LPRECT, LPARAM);
  78.  
  79. #endif  // SM_CMONITORS
  80.  
  81. #undef GetMonitorInfo
  82. #undef GetSystemMetrics
  83. #undef MonitorFromWindow
  84. #undef MonitorFromRect
  85. #undef MonitorFromPoint
  86. #undef EnumDisplayMonitors
  87.  
  88. //
  89. // Define COMPILE_MULTIMON_STUBS to compile the stubs;
  90. // otherwise, you get the declarations.
  91. //
  92. #if defined(COMPILE_MULTIMON_STUBS) && defined(__BORLANDC__) && defined(__RTL_BUILD__)
  93.  
  94. //-----------------------------------------------------------------------------
  95. //
  96. // Implement the API stubs.
  97. //
  98. //-----------------------------------------------------------------------------
  99.  
  100. #ifndef MULTIMON_FNS_DEFINED
  101.  
  102. int      (WINAPI* g_pfnGetSystemMetrics)(int) = NULL;
  103. HMONITOR (WINAPI* g_pfnMonitorFromWindow)(HWND, DWORD) = NULL;
  104. HMONITOR (WINAPI* g_pfnMonitorFromRect)(LPCRECT, DWORD) = NULL;
  105. HMONITOR (WINAPI* g_pfnMonitorFromPoint)(POINT, DWORD) = NULL;
  106. BOOL     (WINAPI* g_pfnGetMonitorInfo)(HMONITOR, LPMONITORINFO) = NULL;
  107. BOOL     (WINAPI* g_pfnEnumDisplayMonitors)(HDC, LPCRECT, MONITORENUMPROC, LPARAM) = NULL;
  108. BOOL     g_fMultiMonInitDone = FALSE;
  109.  
  110. #endif
  111.  
  112. BOOL InitMultipleMonitorStubs(void)
  113. {
  114.     HMODULE hUser32;
  115.  
  116.     if (g_fMultiMonInitDone)
  117.     {
  118.         return g_pfnGetMonitorInfo != NULL;
  119.     }
  120.  
  121.     if ((hUser32 = GetModuleHandle(TEXT("USER32"))) &&
  122.         (*(FARPROC*)&g_pfnGetSystemMetrics    = GetProcAddress(hUser32,"GetSystemMetrics")) &&
  123.         (*(FARPROC*)&g_pfnMonitorFromWindow   = GetProcAddress(hUser32,"MonitorFromWindow")) &&
  124.         (*(FARPROC*)&g_pfnMonitorFromRect     = GetProcAddress(hUser32,"MonitorFromRect")) &&
  125.         (*(FARPROC*)&g_pfnMonitorFromPoint    = GetProcAddress(hUser32,"MonitorFromPoint")) &&
  126.         (*(FARPROC*)&g_pfnEnumDisplayMonitors = GetProcAddress(hUser32,"EnumDisplayMonitors")) &&
  127. #ifdef UNICODE
  128.         (*(FARPROC*)&g_pfnGetMonitorInfo      = GetProcAddress(hUser32,"GetMonitorInfoW")) )
  129. #else
  130.         (*(FARPROC*)&g_pfnGetMonitorInfo      = GetProcAddress(hUser32,"GetMonitorInfoA")) )
  131. #endif
  132.  
  133.     {
  134.         g_fMultiMonInitDone = TRUE;
  135.         return TRUE;
  136.     }
  137.     else
  138.     {
  139.         g_pfnGetSystemMetrics    = NULL;
  140.         g_pfnMonitorFromWindow   = NULL;
  141.         g_pfnMonitorFromRect     = NULL;
  142.         g_pfnMonitorFromPoint    = NULL;
  143.         g_pfnGetMonitorInfo      = NULL;
  144.         g_pfnEnumDisplayMonitors = NULL;
  145.  
  146.         g_fMultiMonInitDone = TRUE;
  147.         return FALSE;
  148.     }
  149. }
  150.  
  151. //-----------------------------------------------------------------------------
  152. //
  153. // fake implementations of Monitor APIs that work with the primary display
  154. // no special parameter validation is made since these run in client code
  155. //
  156. //-----------------------------------------------------------------------------
  157.  
  158. int WINAPI
  159. xGetSystemMetrics(int nIndex)
  160. {
  161.     if (InitMultipleMonitorStubs())
  162.         return g_pfnGetSystemMetrics(nIndex);
  163.  
  164.     switch (nIndex)
  165.     {
  166.     case SM_CMONITORS:
  167.     case SM_SAMEDISPLAYFORMAT:
  168.         return 1;
  169.  
  170.     case SM_XVIRTUALSCREEN:
  171.     case SM_YVIRTUALSCREEN:
  172.         return 0;
  173.  
  174.     case SM_CXVIRTUALSCREEN:
  175.         nIndex = SM_CXSCREEN;
  176.         break;
  177.  
  178.     case SM_CYVIRTUALSCREEN:
  179.         nIndex = SM_CYSCREEN;
  180.         break;
  181.     }
  182.  
  183.     return GetSystemMetrics(nIndex);
  184. }
  185.  
  186. #define xPRIMARY_MONITOR ((HMONITOR)0x12340042)
  187.  
  188. HMONITOR WINAPI
  189. xMonitorFromPoint(POINT ptScreenCoords, DWORD dwFlags)
  190. {
  191.     if (InitMultipleMonitorStubs())
  192.         return g_pfnMonitorFromPoint(ptScreenCoords, dwFlags);
  193.  
  194.     if ((dwFlags & (MONITOR_DEFAULTTOPRIMARY | MONITOR_DEFAULTTONEAREST)) ||
  195.         ((ptScreenCoords.x >= 0) &&
  196.         (ptScreenCoords.x < GetSystemMetrics(SM_CXSCREEN)) &&
  197.         (ptScreenCoords.y >= 0) &&
  198.         (ptScreenCoords.y < GetSystemMetrics(SM_CYSCREEN))))
  199.     {
  200.         return xPRIMARY_MONITOR;
  201.     }
  202.  
  203.     return NULL;
  204. }
  205.  
  206. HMONITOR WINAPI
  207. xMonitorFromRect(LPCRECT lprcScreenCoords, DWORD dwFlags)
  208. {
  209.     if (InitMultipleMonitorStubs())
  210.         return g_pfnMonitorFromRect(lprcScreenCoords, dwFlags);
  211.  
  212.     if ((dwFlags & (MONITOR_DEFAULTTOPRIMARY | MONITOR_DEFAULTTONEAREST)) ||
  213.         ((lprcScreenCoords->right > 0) &&
  214.         (lprcScreenCoords->bottom > 0) &&
  215.         (lprcScreenCoords->left < GetSystemMetrics(SM_CXSCREEN)) &&
  216.         (lprcScreenCoords->top < GetSystemMetrics(SM_CYSCREEN))))
  217.     {
  218.         return xPRIMARY_MONITOR;
  219.     }
  220.  
  221.     return NULL;
  222. }
  223.  
  224. HMONITOR WINAPI
  225. xMonitorFromWindow(HWND hWnd, DWORD dwFlags)
  226. {
  227.     WINDOWPLACEMENT wp;
  228.  
  229.     if (InitMultipleMonitorStubs())
  230.         return g_pfnMonitorFromWindow(hWnd, dwFlags);
  231.  
  232.     if (dwFlags & (MONITOR_DEFAULTTOPRIMARY | MONITOR_DEFAULTTONEAREST))
  233.         return xPRIMARY_MONITOR;
  234.  
  235.     if (IsIconic(hWnd) ? 
  236.             GetWindowPlacement(hWnd, &wp) : 
  237.             GetWindowRect(hWnd, &wp.rcNormalPosition)) {
  238.  
  239.         return xMonitorFromRect(&wp.rcNormalPosition, dwFlags);
  240.     }
  241.  
  242.     return NULL;
  243. }
  244.  
  245. BOOL WINAPI
  246. xGetMonitorInfo(HMONITOR hMonitor, LPMONITORINFO lpMonitorInfo)
  247. {
  248.     RECT rcWork;
  249.  
  250.     if (InitMultipleMonitorStubs())
  251.         return g_pfnGetMonitorInfo(hMonitor, lpMonitorInfo);
  252.  
  253.     if ((hMonitor == xPRIMARY_MONITOR) &&
  254.         lpMonitorInfo &&
  255.         (lpMonitorInfo->cbSize >= sizeof(MONITORINFO)) &&
  256.         SystemParametersInfo(SPI_GETWORKAREA, 0, &rcWork, 0))
  257.     {
  258.         lpMonitorInfo->rcMonitor.left = 0;
  259.         lpMonitorInfo->rcMonitor.top  = 0;
  260.         lpMonitorInfo->rcMonitor.right  = GetSystemMetrics(SM_CXSCREEN);
  261.         lpMonitorInfo->rcMonitor.bottom = GetSystemMetrics(SM_CYSCREEN);
  262.         lpMonitorInfo->rcWork = rcWork;
  263.         lpMonitorInfo->dwFlags = MONITORINFOF_PRIMARY;
  264.  
  265.         if (lpMonitorInfo->cbSize >= sizeof(MONITORINFOEX))
  266.             lstrcpy(((MONITORINFOEX*)lpMonitorInfo)->szDevice, TEXT("DISPLAY"));
  267.  
  268.         return TRUE;
  269.     }
  270.  
  271.     return FALSE;
  272. }
  273.  
  274. BOOL WINAPI
  275. xEnumDisplayMonitors(
  276.         HDC             hdcOptionalForPainting,
  277.         LPCRECT         lprcEnumMonitorsThatIntersect,
  278.         MONITORENUMPROC lpfnEnumProc,
  279.         LPARAM          dwData)
  280. {
  281.     RECT rcLimit;
  282.  
  283.     if (InitMultipleMonitorStubs()) {
  284.         return g_pfnEnumDisplayMonitors(
  285.                 hdcOptionalForPainting,
  286.                 lprcEnumMonitorsThatIntersect,
  287.                 lpfnEnumProc,
  288.                 dwData);
  289.     }
  290.  
  291.     if (!lpfnEnumProc)
  292.         return FALSE;
  293.  
  294.     rcLimit.left   = 0;
  295.     rcLimit.top    = 0;
  296.     rcLimit.right  = GetSystemMetrics(SM_CXSCREEN);
  297.     rcLimit.bottom = GetSystemMetrics(SM_CYSCREEN);
  298.  
  299.     if (hdcOptionalForPainting)
  300.     {
  301.         RECT    rcClip;
  302.         POINT   ptOrg;
  303.  
  304.         switch (GetClipBox(hdcOptionalForPainting, &rcClip))
  305.         {
  306.         default:
  307.             if (!GetDCOrgEx(hdcOptionalForPainting, &ptOrg))
  308.                 return FALSE;
  309.  
  310.             OffsetRect(&rcLimit, -ptOrg.x, -ptOrg.y);
  311.             if (IntersectRect(&rcLimit, &rcLimit, &rcClip) &&
  312.                 (!lprcEnumMonitorsThatIntersect ||
  313.                      IntersectRect(&rcLimit, &rcLimit, lprcEnumMonitorsThatIntersect))) {
  314.  
  315.                 break;
  316.             }
  317.             //fall thru
  318.         case NULLREGION:
  319.              return TRUE;
  320.         case ERROR:
  321.              return FALSE;
  322.         }
  323.     } else {
  324.         if (    lprcEnumMonitorsThatIntersect &&
  325.                 !IntersectRect(&rcLimit, &rcLimit, lprcEnumMonitorsThatIntersect)) {
  326.  
  327.             return TRUE;
  328.         }
  329.     }
  330.  
  331.     return lpfnEnumProc(
  332.             xPRIMARY_MONITOR,
  333.             hdcOptionalForPainting,
  334.             &rcLimit,
  335.             dwData);
  336. }
  337.  
  338. #undef xPRIMARY_MONITOR
  339. #undef COMPILE_MULTIMON_STUBS
  340.  
  341. #else   // COMPILE_MULTIMON_STUBS
  342.  
  343. extern int  WINAPI xGetSystemMetrics(int);
  344. extern HMONITOR WINAPI xMonitorFromWindow(HWND, DWORD);
  345. extern HMONITOR WINAPI xMonitorFromRect(LPCRECT, DWORD);
  346. extern HMONITOR WINAPI xMonitorFromPoint(POINT, DWORD);
  347. extern BOOL WINAPI xGetMonitorInfo(HMONITOR, LPMONITORINFO);
  348. extern BOOL WINAPI xEnumDisplayMonitors(HDC, LPCRECT, MONITORENUMPROC, LPARAM);
  349.  
  350. #endif  // COMPILE_MULTIMON_STUBS
  351.  
  352. //
  353. // build defines that replace the regular APIs with our versions
  354. //
  355. #define GetSystemMetrics    xGetSystemMetrics
  356. #define MonitorFromWindow   xMonitorFromWindow
  357. #define MonitorFromRect     xMonitorFromRect
  358. #define MonitorFromPoint    xMonitorFromPoint
  359. #define GetMonitorInfo      xGetMonitorInfo
  360. #define EnumDisplayMonitors xEnumDisplayMonitors
  361.  
  362. #ifdef __cplusplus
  363. }
  364. #endif  // __cplusplus
  365. #pragma option pop /*P_O_Pop*/
  366.