MultiMon.h
上传用户:dzyhzl
上传日期:2019-04-29
资源大小:56270k
文件大小:15k
源码类别:

模拟服务器

开发平台:

C/C++

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