最近用到windows平台下的双屏显示,查阅一些资料,最终实现该功能。
多显示器编程的一般步骤为:
网上一般用类似如下的程序,获得显示屏的个数 int i;//显示器总数 BOOL flag; DISPLAY_DEVICE dd; i = 0; flag = true; ZeroMemory(&dd, sizeof(dd)); dd.cb = sizeof(dd); do { flag = EnumDisplayDevices(NULL, i, &dd, 0); if (flag) i ++; } while (flag); MSDN上的解释: BOOL EnumDisplayDevices( __in LPCTSTR lpDevice, __in DWORD iDevNum, __out PDISPLAY_DEVICE lpDisplayDevice, __in DWORD dwFlags)To query all display devices in the current session, call this function in a loop, starting with iDevNum set to 0, and incrementing iDevNum until the function fails. To select all display devices in the desktop, use only the display devices that have the DISPLAY_DEVICE_ATTACHED_TO_DESKTOPflag in the DISPLAY_DEVICE structure. (http://msdn.microsoft.com/en-us/library/dd162609(v=vs.85).aspx) EnumDisplayDevices反复的检测显示器的编号,其返回值判断是否存在该显示器。 我试验了这段代码,虽然我的机器上只接了一个显示器,这段代码却能检测到4个! 各个显示器的信息如下: deviceNum:0DeviceName:\\.\DISPLAY1DeviceString: Intel(R) Graphics Media Accelerator HDdeviceNum:1DeviceName:\\.\DISPLAY2DeviceString: Intel(R) Graphics Media Accelerator HDdeviceNum:2DeviceName:\\.\DISPLAYV1DeviceString:NetMeeting driverdeviceNum:3DeviceName:\\.\DISPLAYV2DeviceString:RDPDD Chained DD
MSDN对该函数的解释:The EnumDisplayDevices function lets you obtain information about the display devices in the current session.推测EnumDisplayDevices 能获得主机上所有可用的显示器接口,可用不代表正在用的。代表什么意思? 猜测是虚拟的显示接口或者是镜像 StateFlags的值为DISPLAY_DEVICE_MIRRORING_DRIVER 意思为Represents a pseudo device used to mirror application drawing for remoting or other purposes. An invisible pseudo monitor is associated with this device. For example, NetMeeting uses it. Note that GetSystemMetrics (SM_MONITORS) only accounts for visible display monitors。 尝试dd.StateFlags==DISPLAY_DEVICE_ACTIVE 未果,这种方法应该是可以的。测试不同的devicenum得到的dd.StateFlags不同,外接显示器的StateFlags的值很大。 int i;//显示器总数 BOOL flag; DISPLAY_DEVICE dd; i = 0; flag = true; ZeroMemory(&dd, sizeof(dd)); dd.cb = sizeof(dd);
DEVMODE devMode; memset( &devMode, 0, sizeof(devMode) ); devMode.dmSize = sizeof(devMode); do { flag = EnumDisplayDevices(NULL, i, &dd, 0); flag=flag&&EnumDisplaySettings( (const char*)dd.DeviceName, ENUM_CURRENT_SETTINGS, &devMode ); if (flag) i ++; } while (flag); 通过EnumDisplaySettings获得对应显示器的信息,如果没有外接显示器,则函数放回Null。运行这段代码显示器数目显示为1。 参考资料: Windows系统下的多显示器模式开发日记 一到七 http://blog.sina.com.cn/s/blog_4078ccd60100049a.html MSDN http://msdn.microsoft.com/en-us/library/dd162609(v=vs.85).aspx Multiple Display Monitors http://msdn.microsoft.com/en-us/library/dd162617(v=vs.85).aspx 获取显示屏个数 http://blog.csdn.net/jszj/article/details/4620103
|
|