Multimon.h
上传用户:zqemid3000
上传日期:2007-01-04
资源大小:158k
文件大小:12k
源码类别:

多显示器编程

开发平台:

Visual C++

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