multimon.h
上传用户:wqn_0827
上传日期:2007-01-04
资源大小:10k
文件大小:10k
源码类别:

多显示器编程

开发平台:

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, MONITORENUMPROC, LPARAM);
  94. BOOL  (WINAPI *g_pfnEnumDisplayDevices)(LPVOID, int, DISPLAY_DEVICE *,DWORD);
  95. BOOL InitMultipleMonitorStubs(void)
  96. {
  97. HMODULE hUser32;
  98. static BOOL fInitDone;
  99. if (fInitDone)
  100. {
  101. return g_pfnGetMonitorInfo != NULL;
  102. }
  103. if ((hUser32 = GetModuleHandle(TEXT("USER32"))) &&
  104. (*(FARPROC*)&g_pfnGetSystemMetrics    = GetProcAddress(hUser32,"GetSystemMetrics")) &&
  105. (*(FARPROC*)&g_pfnMonitorFromWindow   = GetProcAddress(hUser32,"MonitorFromWindow")) &&
  106. (*(FARPROC*)&g_pfnMonitorFromRect     = GetProcAddress(hUser32,"MonitorFromRect")) &&
  107. (*(FARPROC*)&g_pfnMonitorFromPoint    = GetProcAddress(hUser32,"MonitorFromPoint")) &&
  108. (*(FARPROC*)&g_pfnEnumDisplayMonitors = GetProcAddress(hUser32,"EnumDisplayMonitors")) &&
  109. #ifdef UNICODE
  110. (*(FARPROC*)&g_pfnGetMonitorInfo      = GetProcAddress(hUser32,"GetMonitorInfoW")) &&
  111. (*(FARPROC*)&g_pfnEnumDisplayDevices  = GetProcAddress(hUser32,"EnumDisplayDevicesW")) &&
  112. #else
  113. (*(FARPROC*)&g_pfnGetMonitorInfo      = GetProcAddress(hUser32,"GetMonitorInfoA")) &&
  114. (*(FARPROC*)&g_pfnEnumDisplayDevices  = GetProcAddress(hUser32,"EnumDisplayDevicesA")) &&
  115. #endif
  116. (GetSystemMetrics(SM_CXVIRTUALSCREEN) >= GetSystemMetrics(SM_CXSCREEN)) &&
  117. (GetSystemMetrics(SM_CYVIRTUALSCREEN) >= GetSystemMetrics(SM_CYSCREEN)) )
  118. {
  119. fInitDone = TRUE;
  120. return TRUE;
  121. }
  122. else
  123. {
  124. g_pfnGetSystemMetrics    = NULL;
  125. g_pfnMonitorFromWindow   = NULL;
  126. g_pfnMonitorFromRect     = NULL;
  127. g_pfnMonitorFromPoint    = NULL;
  128. g_pfnGetMonitorInfo      = NULL;
  129. g_pfnEnumDisplayMonitors = NULL;
  130. g_pfnEnumDisplayDevices  = NULL;
  131. fInitDone = TRUE;
  132. return FALSE;
  133. }
  134. }
  135. //-----------------------------------------------------------------------------
  136. //
  137. // "stubbed" implementations of Monitor APIs that work with the primary display
  138. //
  139. //-----------------------------------------------------------------------------
  140. int WINAPI
  141. xGetSystemMetrics(int nIndex)
  142. {
  143. if (InitMultipleMonitorStubs())
  144. return g_pfnGetSystemMetrics(nIndex);
  145. switch (nIndex)
  146. {
  147. case SM_CMONITORS:
  148. case SM_SAMEDISPLAYFORMAT:
  149. return 1;
  150. case SM_XVIRTUALSCREEN:
  151. case SM_YVIRTUALSCREEN:
  152. return 0;
  153. case SM_CXVIRTUALSCREEN:
  154. nIndex = SM_CXSCREEN;
  155. break;
  156. case SM_CYVIRTUALSCREEN:
  157. nIndex = SM_CYSCREEN;
  158. break;
  159. }
  160. return GetSystemMetrics(nIndex);
  161. }
  162. #define xPRIMARY_MONITOR ((HMONITOR)0x42)
  163. HMONITOR WINAPI
  164. xMonitorFromRect(LPCRECT lprcScreenCoords, UINT uFlags)
  165. {
  166. if (InitMultipleMonitorStubs())
  167. return g_pfnMonitorFromRect(lprcScreenCoords, uFlags);
  168. if ((uFlags & (MONITOR_DEFAULTTOPRIMARY | MONITOR_DEFAULTTONEAREST)) ||
  169. ((lprcScreenCoords->right > 0) &&
  170. (lprcScreenCoords->bottom > 0) &&
  171. (lprcScreenCoords->left < GetSystemMetrics(SM_CXSCREEN)) &&
  172. (lprcScreenCoords->top < GetSystemMetrics(SM_CYSCREEN))))
  173. {
  174. return xPRIMARY_MONITOR;
  175. }
  176. return NULL;
  177. }
  178. HMONITOR WINAPI
  179. xMonitorFromWindow(HWND hWnd, UINT uFlags)
  180. {
  181. RECT rc;
  182. if (InitMultipleMonitorStubs())
  183. return g_pfnMonitorFromWindow(hWnd, uFlags);
  184. if (uFlags & (MONITOR_DEFAULTTOPRIMARY | MONITOR_DEFAULTTONEAREST))
  185. return xPRIMARY_MONITOR;
  186. if (GetWindowRect(hWnd, &rc))
  187. return xMonitorFromRect(&rc, uFlags);
  188. return NULL;
  189. }
  190. HMONITOR WINAPI
  191. xMonitorFromPoint(POINT ptScreenCoords, UINT uFlags)
  192. {
  193. if (InitMultipleMonitorStubs())
  194. return g_pfnMonitorFromPoint(ptScreenCoords, uFlags);
  195. if ((uFlags & (MONITOR_DEFAULTTOPRIMARY | MONITOR_DEFAULTTONEAREST)) ||
  196. ((ptScreenCoords.x >= 0) &&
  197. (ptScreenCoords.x < GetSystemMetrics(SM_CXSCREEN)) &&
  198. (ptScreenCoords.y >= 0) &&
  199. (ptScreenCoords.y < GetSystemMetrics(SM_CYSCREEN))))
  200. {
  201. return xPRIMARY_MONITOR;
  202. }
  203. return NULL;
  204. }
  205. BOOL WINAPI
  206. xGetMonitorInfo(HMONITOR hMonitor, LPMONITORINFO lpMonitorInfo)
  207. {
  208. RECT rcWork;
  209. if (InitMultipleMonitorStubs())
  210. return g_pfnGetMonitorInfo(hMonitor, lpMonitorInfo);
  211. if ((hMonitor == xPRIMARY_MONITOR) && lpMonitorInfo &&
  212. (lpMonitorInfo->cbSize >= sizeof(MONITORINFO)) &&
  213. SystemParametersInfo(SPI_GETWORKAREA, 0, &rcWork, 0))
  214. {
  215. lpMonitorInfo->rcMonitor.left = 0;
  216. lpMonitorInfo->rcMonitor.top  = 0;
  217. lpMonitorInfo->rcMonitor.right  = GetSystemMetrics(SM_CXSCREEN);
  218. lpMonitorInfo->rcMonitor.bottom = GetSystemMetrics(SM_CYSCREEN);
  219. lpMonitorInfo->rcWork = rcWork;
  220. lpMonitorInfo->dwFlags = MONITORINFOF_PRIMARY;
  221. if (lpMonitorInfo->cbSize >= sizeof(MONITORINFOEX))
  222. lstrcpy(((MONITORINFOEX*)lpMonitorInfo)->szDevice, TEXT("DISPLAY"));
  223. return TRUE;
  224. }
  225. return FALSE;
  226. }
  227. BOOL WINAPI
  228. xEnumDisplayMonitors(
  229. HDC hdc,
  230. LPCRECT lprcIntersect, 
  231. MONITORENUMPROC lpfnEnumProc,
  232. LPARAM lData)
  233. {
  234. RECT rcCallback, rcLimit;
  235. if (InitMultipleMonitorStubs())
  236. return g_pfnEnumDisplayMonitors(hdc,
  237. lprcIntersect, lpfnEnumProc, lData);
  238.     
  239. if (!lpfnEnumProc)
  240. return FALSE;
  241. rcLimit.left   = 0;
  242. rcLimit.top    = 0;
  243. rcLimit.right  = GetSystemMetrics(SM_CXSCREEN);
  244. rcLimit.bottom = GetSystemMetrics(SM_CYSCREEN);
  245. if (hdc)
  246. {
  247. RECT rcClip;
  248. HWND hWnd;
  249. if ((hWnd = WindowFromDC(hdc)) == NULL)
  250. return FALSE;
  251. switch (GetClipBox(hdc, &rcClip))
  252. {
  253. default:
  254. MapWindowPoints(NULL, hWnd, (LPPOINT)&rcLimit, 2);
  255. if (IntersectRect(&rcCallback, &rcClip, &rcLimit))
  256. break;
  257. //fall thru
  258. case NULLREGION:
  259.  return TRUE;
  260. case ERROR:
  261.  return FALSE;
  262. }
  263. rcLimit = rcCallback;
  264. }
  265. if (!lprcIntersect ||
  266. IntersectRect(&rcCallback, lprcIntersect, &rcLimit))
  267. {
  268. lpfnEnumProc(xPRIMARY_MONITOR, hdc, &rcCallback, lData);
  269. }
  270. return TRUE;
  271. }
  272. BOOL WINAPI
  273. xEnumDisplayDevices(LPVOID lpReserved, int iDeviceNum, DISPLAY_DEVICE * pDisplayDevice, DWORD dwFlags)
  274. {
  275. if (InitMultipleMonitorStubs())
  276. return g_pfnEnumDisplayDevices(lpReserved, iDeviceNum, pDisplayDevice, dwFlags);
  277.     
  278. return FALSE;
  279. }
  280. #undef xPRIMARY_MONITOR
  281. #undef COMPILE_MULTIMON_STUBS
  282. #else // COMPILE_MULTIMON_STUBS
  283. extern int WINAPI xGetSystemMetrics(int);
  284. extern HMONITOR WINAPI xMonitorFromWindow(HWND, UINT);
  285. extern HMONITOR WINAPI xMonitorFromRect(LPCRECT, UINT);
  286. extern HMONITOR WINAPI xMonitorFromPoint(POINT, UINT);
  287. extern BOOL WINAPI xGetMonitorInfo(HMONITOR, LPMONITORINFO);
  288. extern BOOL WINAPI xEnumDisplayMonitors(HDC, LPCRECT, MONITORENUMPROC, LPARAM);
  289. extern BOOL WINAPI xEnumDisplayDevices(LPVOID, int, DISPLAY_DEVICE *,DWORD);
  290. #endif // COMPILE_MULTIMON_STUBS
  291. //
  292. // build defines that replace the regular APIs with our versions
  293. //
  294. #define GetSystemMetrics    xGetSystemMetrics
  295. #define MonitorFromWindow   xMonitorFromWindow
  296. #define MonitorFromRect     xMonitorFromRect
  297. #define MonitorFromPoint    xMonitorFromPoint
  298. #define GetMonitorInfo      xGetMonitorInfo
  299. #define EnumDisplayMonitors xEnumDisplayMonitors
  300. #define EnumDisplayDevices xEnumDisplayDevices
  301. #ifdef __cplusplus
  302. }
  303. #endif /* __cplusplus */
  304. #endif  /* !defined(_WIN32_WINNT) || (_WIN32_WINNT < 0x0500) */