multimon.h
上传用户:jyxwjx
上传日期:2020-03-13
资源大小:221k
文件大小:12k
源码类别:

多显示器编程

开发平台:

Visual C++

  1. #if !defined(_WIN32_WINNT) || (_WIN32_WINNT < 0x0500)
  2. //=============================================================================
  3. //
  4. // MULTIMON
  5. // stub module that "stubs" multiple monitor APIs on pre-Memphis Win32 OSes
  6. //
  7. // By using this header your code will work unchanged on Win95,
  8. // you will get back correct values from GetSystemMetrics() for new metrics
  9. // and the new APIs will act like only one display is present.
  10. //
  11. // exactly one source must include this with COMPILE_MULTIMON_STUBS defined
  12. //
  13. //=============================================================================
  14. #ifdef __cplusplus
  15. extern "C" {            /* Assume C declarations for C++ */
  16. #endif  /* __cplusplus */
  17. //
  18. // if we are building on Win95/NT4 headers we need to declare this stuff ourselves
  19. //
  20. #ifndef SM_CMONITORS
  21. #define SM_XVIRTUALSCREEN       76
  22. #define SM_YVIRTUALSCREEN       77
  23. #define SM_CXVIRTUALSCREEN      78
  24. #define SM_CYVIRTUALSCREEN      79
  25. #define SM_CMONITORS            80
  26. #define SM_SAMEDISPLAYFORMAT    81
  27. DECLARE_HANDLE(HMONITOR);
  28. #define MONITOR_DEFAULTTONULL       0x00000000
  29. #define MONITOR_DEFAULTTOPRIMARY    0x00000001
  30. #define MONITOR_DEFAULTTONEAREST    0x00000002
  31. #define MONITORINFOF_PRIMARY        0x00000001
  32. typedef struct tagMONITORINFO
  33. {
  34.     DWORD   cbSize;
  35.     RECT    rcMonitor;
  36.     RECT    rcWork;
  37.     DWORD   dwFlags;
  38. } MONITORINFO, *LPMONITORINFO;
  39. #define CCHDEVICENAME 32
  40. #ifdef __cplusplus
  41. typedef struct tagMONITORINFOEX : public tagMONITORINFO
  42. {
  43.   TCHAR       szDevice[CCHDEVICENAME];
  44. } MONITORINFOEX, *LPMONITORINFOEX;
  45. #else
  46. typedef struct
  47. {
  48.   MONITORINFO;
  49.   TCHAR       szDevice[CCHDEVICENAME];
  50. } MONITORINFOEX, *LPMONITORINFOEX;
  51. #endif
  52. typedef BOOL (CALLBACK* MONITORENUMPROC)(HMONITOR, HDC, LPRECT, LPARAM);
  53. #endif  // SM_CMONITORS
  54. #ifndef DISPLAY_DEVICE_ATTACHED_TO_DESKTOP
  55. typedef struct {
  56.     DWORD  cb;
  57.     CHAR   DeviceName[32];
  58.     CHAR   DeviceString[128];
  59.     DWORD  StateFlags;
  60. } DISPLAY_DEVICE;
  61. #define DISPLAY_DEVICE_ATTACHED_TO_DESKTOP 0x00000001
  62. #define DISPLAY_DEVICE_MULTI_DRIVER        0x00000002
  63. #define DISPLAY_DEVICE_PRIMARY_DEVICE      0x00000004
  64. #define DISPLAY_DEVICE_MIRRORING_DRIVER    0x00000008
  65. #endif
  66. #define DISPLAY_DEVICE_VGA                 0x00000010
  67. #ifndef ENUM_CURRENT_SETTINGS
  68. #define ENUM_CURRENT_SETTINGS       ((DWORD)-1)
  69. #define ENUM_REGISTRY_SETTINGS      ((DWORD)-2)
  70. #endif
  71. #undef GetMonitorInfo
  72. #undef GetSystemMetrics
  73. #undef MonitorFromWindow
  74. #undef MonitorFromRect
  75. #undef MonitorFromPoint
  76. #undef EnumDisplayMonitors
  77. #undef EnumDisplayDevices
  78. //
  79. // define this to compile the stubs
  80. // otherwise you get the declarations
  81. //
  82. #ifdef COMPILE_MULTIMON_STUBS
  83.     //---------------------------------------------------------------------------
  84.     //
  85.     // Implement the API stubs.
  86.     //
  87.     //---------------------------------------------------------------------------
  88.     int      (WINAPI* g_pfnGetSystemMetrics)(int);
  89.     HMONITOR (WINAPI* g_pfnMonitorFromWindow)(HWND, BOOL);
  90.     HMONITOR (WINAPI* g_pfnMonitorFromRect)(LPCRECT, BOOL);
  91.     HMONITOR (WINAPI* g_pfnMonitorFromPoint)(POINT, BOOL);
  92.     BOOL     (WINAPI* g_pfnGetMonitorInfo)(HMONITOR, LPMONITORINFO);
  93.     BOOL     (WINAPI* g_pfnEnumDisplayMonitors)(HDC, LPCRECT,
  94.               MONITORENUMPROC, LPARAM);
  95.     BOOL     (WINAPI *g_pfnEnumDisplayDevices)(LPVOID, int, 
  96.               DISPLAY_DEVICE *,DWORD);
  97.     BOOL InitMultipleMonitorStubs(void)
  98.     {
  99.         HMODULE hUser32;
  100.         static BOOL fInitDone;
  101.         if (fInitDone)
  102.         {
  103.             return g_pfnGetMonitorInfo != NULL;
  104.         }
  105.         if ((hUser32 = GetModuleHandle(TEXT("USER32"))) &&
  106.             (*(FARPROC*)&g_pfnGetSystemMetrics    = GetProcAddress(hUser32,"GetSystemMetrics")) &&
  107.             (*(FARPROC*)&g_pfnMonitorFromWindow   = GetProcAddress(hUser32,"MonitorFromWindow")) &&
  108.             (*(FARPROC*)&g_pfnMonitorFromRect     = GetProcAddress(hUser32,"MonitorFromRect")) &&
  109.             (*(FARPROC*)&g_pfnMonitorFromPoint    = GetProcAddress(hUser32,"MonitorFromPoint")) &&
  110.             (*(FARPROC*)&g_pfnEnumDisplayMonitors = GetProcAddress(hUser32,"EnumDisplayMonitors")) &&
  111.     #ifdef UNICODE
  112.             (*(FARPROC*)&g_pfnGetMonitorInfo      = GetProcAddress(hUser32,"GetMonitorInfoW")) &&
  113.             (*(FARPROC*)&g_pfnEnumDisplayDevices  = GetProcAddress(hUser32,"EnumDisplayDevicesW")) &&
  114.     #else
  115.             (*(FARPROC*)&g_pfnGetMonitorInfo      = GetProcAddress(hUser32,"GetMonitorInfoA")) &&
  116.             (*(FARPROC*)&g_pfnEnumDisplayDevices  = GetProcAddress(hUser32,"EnumDisplayDevicesA")) &&
  117.     #endif
  118.             (GetSystemMetrics(SM_CXVIRTUALSCREEN) >= GetSystemMetrics(SM_CXSCREEN)) &&
  119.             (GetSystemMetrics(SM_CYVIRTUALSCREEN) >= GetSystemMetrics(SM_CYSCREEN)) )
  120.         {
  121.             fInitDone = TRUE;
  122.             return TRUE;
  123.         }
  124.         else
  125.         {
  126.             g_pfnGetSystemMetrics    = NULL;
  127.             g_pfnMonitorFromWindow   = NULL;
  128.             g_pfnMonitorFromRect     = NULL;
  129.             g_pfnMonitorFromPoint    = NULL;
  130.             g_pfnGetMonitorInfo      = NULL;
  131.             g_pfnEnumDisplayMonitors = NULL;
  132.             g_pfnEnumDisplayDevices  = NULL;
  133.             fInitDone = TRUE;
  134.             return FALSE;
  135.         }
  136.     }
  137.     //---------------------------------------------------------------------------
  138.     //
  139.     // "stubbed" implementations of Monitor APIs that work with the primary         //  display
  140.     //
  141.     //---------------------------------------------------------------------------
  142.     int WINAPI
  143.     xGetSystemMetrics(int nIndex)
  144.     {
  145.         if (InitMultipleMonitorStubs())
  146.             return g_pfnGetSystemMetrics(nIndex);
  147.         switch (nIndex)
  148.         {
  149.         case SM_CMONITORS:
  150.         case SM_SAMEDISPLAYFORMAT:
  151.             return 1;
  152.         case SM_XVIRTUALSCREEN:
  153.         case SM_YVIRTUALSCREEN:
  154.             return 0;
  155.         case SM_CXVIRTUALSCREEN:
  156.             nIndex = SM_CXSCREEN;
  157.             break;
  158.         case SM_CYVIRTUALSCREEN:
  159.             nIndex = SM_CYSCREEN;
  160.             break;
  161.         }
  162.         return GetSystemMetrics(nIndex);
  163.     }
  164.     #define xPRIMARY_MONITOR ((HMONITOR)0x42)
  165.     HMONITOR WINAPI
  166.     xMonitorFromRect(LPCRECT lprcScreenCoords, UINT uFlags)
  167.     {
  168.         if (InitMultipleMonitorStubs())
  169.             return g_pfnMonitorFromRect(lprcScreenCoords, uFlags);
  170.         if ((uFlags & (MONITOR_DEFAULTTOPRIMARY | MONITOR_DEFAULTTONEAREST)) ||
  171.             ((lprcScreenCoords->right > 0) &&
  172.             (lprcScreenCoords->bottom > 0) &&
  173.             (lprcScreenCoords->left < GetSystemMetrics(SM_CXSCREEN)) &&
  174.             (lprcScreenCoords->top < GetSystemMetrics(SM_CYSCREEN))))
  175.         {
  176.             return xPRIMARY_MONITOR;
  177.         }
  178.         return NULL;
  179.     }
  180.     HMONITOR WINAPI
  181.     xMonitorFromWindow(HWND hWnd, UINT uFlags)
  182.     {
  183.         RECT rc;
  184.         if (InitMultipleMonitorStubs())
  185.             return g_pfnMonitorFromWindow(hWnd, uFlags);
  186.         if (uFlags & (MONITOR_DEFAULTTOPRIMARY | MONITOR_DEFAULTTONEAREST))
  187.             return xPRIMARY_MONITOR;
  188.         if (GetWindowRect(hWnd, &rc))
  189.             return xMonitorFromRect(&rc, uFlags);
  190.         return NULL;
  191.     }
  192.     HMONITOR WINAPI
  193.     xMonitorFromPoint(POINT ptScreenCoords, UINT uFlags)
  194.     {
  195.         if (InitMultipleMonitorStubs())
  196.             return g_pfnMonitorFromPoint(ptScreenCoords, uFlags);
  197.         if ((uFlags & (MONITOR_DEFAULTTOPRIMARY | MONITOR_DEFAULTTONEAREST)) ||
  198.             ((ptScreenCoords.x >= 0) &&
  199.             (ptScreenCoords.x < GetSystemMetrics(SM_CXSCREEN)) &&
  200.             (ptScreenCoords.y >= 0) &&
  201.             (ptScreenCoords.y < GetSystemMetrics(SM_CYSCREEN))))
  202.         {
  203.             return xPRIMARY_MONITOR;
  204.         }
  205.         return NULL;
  206.     }
  207.     BOOL WINAPI
  208.     xGetMonitorInfo(HMONITOR hMonitor, LPMONITORINFO lpMonitorInfo)
  209.     {
  210.         RECT rcWork;
  211.         if (InitMultipleMonitorStubs())
  212.             return g_pfnGetMonitorInfo(hMonitor, lpMonitorInfo);
  213.         if ((hMonitor == xPRIMARY_MONITOR) && lpMonitorInfo &&
  214.             (lpMonitorInfo->cbSize >= sizeof(MONITORINFO)) &&
  215.             SystemParametersInfo(SPI_GETWORKAREA, 0, &rcWork, 0))
  216.         {
  217.             lpMonitorInfo->rcMonitor.left = 0;
  218.             lpMonitorInfo->rcMonitor.top  = 0;
  219.             lpMonitorInfo->rcMonitor.right  = GetSystemMetrics(SM_CXSCREEN);
  220.             lpMonitorInfo->rcMonitor.bottom = GetSystemMetrics(SM_CYSCREEN);
  221.             lpMonitorInfo->rcWork = rcWork;
  222.             lpMonitorInfo->dwFlags = MONITORINFOF_PRIMARY;
  223.             if (lpMonitorInfo->cbSize >= sizeof(MONITORINFOEX))
  224.                 lstrcpy(((MONITORINFOEX*)lpMonitorInfo)->szDevice,
  225.                 TEXT("DISPLAY"));
  226.             return TRUE;
  227.         }
  228.         return FALSE;
  229.     }
  230.     BOOL WINAPI
  231.     xEnumDisplayMonitors(
  232.         HDC hdc,
  233.         LPCRECT lprcIntersect, 
  234.         MONITORENUMPROC lpfnEnumProc,
  235.         LPARAM lData)
  236.     {
  237.         RECT rcCallback, rcLimit;
  238.         if (InitMultipleMonitorStubs())
  239.             return g_pfnEnumDisplayMonitors(hdc, lprcIntersect, lpfnEnumProc,
  240.                                            lData);
  241.     
  242.         if (!lpfnEnumProc)
  243.             return FALSE;
  244.         rcLimit.left   = 0;
  245.         rcLimit.top    = 0;
  246.         rcLimit.right  = GetSystemMetrics(SM_CXSCREEN);
  247.         rcLimit.bottom = GetSystemMetrics(SM_CYSCREEN);
  248.         if (hdc)
  249.         {
  250.             RECT rcClip;
  251.             HWND hWnd;
  252.             if ((hWnd = WindowFromDC(hdc)) == NULL)
  253.                 return FALSE;
  254.             switch (GetClipBox(hdc, &rcClip))
  255.             {
  256.             default:
  257.                 MapWindowPoints(NULL, hWnd, (LPPOINT)&rcLimit, 2);
  258.                 if (IntersectRect(&rcCallback, &rcClip, &rcLimit))
  259.                     break;
  260.                 //fall thru
  261.             case NULLREGION:
  262.                  return TRUE;
  263.             case ERROR:
  264.                  return FALSE;
  265.             }
  266.             rcLimit = rcCallback;
  267.         }
  268.         if (!lprcIntersect ||
  269.             IntersectRect(&rcCallback, lprcIntersect, &rcLimit))
  270.         {
  271.             lpfnEnumProc(xPRIMARY_MONITOR, hdc, &rcCallback, lData);
  272.         }
  273.         return TRUE;
  274.     }
  275.     BOOL WINAPI
  276.     xEnumDisplayDevices(LPVOID lpReserved, int iDeviceNum, 
  277. DISPLAY_DEVICE * pDisplayDevice, DWORD dwFlags)
  278.     {
  279.         if (InitMultipleMonitorStubs())
  280.             return g_pfnEnumDisplayDevices(lpReserved, iDeviceNum,
  281.                                            pDisplayDevice, dwFlags);
  282.     
  283.         return FALSE;
  284.     }
  285.     #undef xPRIMARY_MONITOR
  286.     #undef COMPILE_MULTIMON_STUBS
  287. #else    // COMPILE_MULTIMON_STUBS
  288.     extern int    WINAPI xGetSystemMetrics(int);
  289.     extern HMONITOR WINAPI xMonitorFromWindow(HWND, UINT);
  290.     extern HMONITOR WINAPI xMonitorFromRect(LPCRECT, UINT);
  291.     extern HMONITOR WINAPI xMonitorFromPoint(POINT, UINT);
  292.     extern BOOL    WINAPI xGetMonitorInfo(HMONITOR, LPMONITORINFO);
  293.     extern BOOL    WINAPI xEnumDisplayMonitors(HDC, LPCRECT, MONITORENUMPROC,
  294.                                                LPARAM);
  295.     extern BOOL WINAPI xEnumDisplayDevices(LPVOID, int, DISPLAY_DEVICE *,DWORD);
  296. #endif    // COMPILE_MULTIMON_STUBS
  297. //
  298. // build defines that replace the regular APIs with our versions
  299. //
  300. #define GetSystemMetrics    xGetSystemMetrics
  301. #define MonitorFromWindow   xMonitorFromWindow
  302. #define MonitorFromRect     xMonitorFromRect
  303. #define MonitorFromPoint    xMonitorFromPoint
  304. #define GetMonitorInfo      xGetMonitorInfo
  305. #define EnumDisplayMonitors xEnumDisplayMonitors
  306. #define EnumDisplayDevices    xEnumDisplayDevices
  307. #ifdef __cplusplus
  308. }
  309. #endif    /* __cplusplus */
  310. #endif  /* !defined(_WIN32_WINNT) || (_WIN32_WINNT < 0x0500) */