WTLVisual C++ 9.0Windows 2008Visual C++ 8.0Windows VistaWindows 2003Windows 2000设计/图形架构师Windows XP初学者开发WindowsC++
Don Kackman 多显示器类移植到 WTL






4.78/5 (7投票s)
Win32 多显示器 API 的类包装器

介绍
我在研究 Windows 中的多显示器支持时,发现了 Don Kackman 的一篇优秀文章(见下文链接),其中描述了他围绕 Win32 多显示器 API 构建的 MFC 封装类。由于我需要在 WTL 项目中使用它,因此我将这些类移植到 WTL,保留了大部分原始代码。
Using the Code
有 3 个类
CMonitor
- 围绕HMONITOR
句柄(由EnumDisplayMonitors
返回)和GetMonitorInfo
函数的封装。使用CMonitor
,您可以获取给定显示器的特性。CMonitors
- 表示当前连接到系统的显示器集合,并封装EnumDisplayMonitors
API 函数。CMonitorDC
- 基于 CDC 的类,表示特定显示器的设备上下文。它没有在任何地方使用,但我为了完整性保留了它。
借助这些类,为您的应用程序添加多显示器支持变得非常容易。请查看以下几个示例。
- 将 "Monitor.h" 和 "Monitors.h" 头文件添加到项目中。
- 如果您正在保存窗口位置以稍后恢复它,则需要确保在使用它之前该位置仍然有效。在创建或显示窗口之前添加以下代码
CMonitors::MakeSureRectIsVisible(pRect);
以下是此函数的实现。无需解释它做什么,对吧?// Makes sure a rectangle is visible void CMonitors::MakeSureRectIsVisible(const LPRECT lprc) { // Check if a rectangle will be visible on any monitor if (!CMonitors::IsOnScreen(lprc)) { // If not a rectangle will be positioned // in the center of the primary monitor CMonitor monitor = CMonitors::GetPrimaryMonitor(); monitor.CenterRectToMonitor(lprc); } }
- 如果您需要将窗口居中显示在主显示器上,请添加以下代码
CMonitor monitor = CMonitors::GetPrimaryMonitor(); monitor.CenterWindowToMonitor(this);
容易混淆的地方
请不要忘记,负坐标或大于 SM_CXSCREEN
、SM_CYSCREEN
的坐标现在是有效的。
致谢
有关更多详细信息,请参阅 Don Kackman 的原始文章:用于多显示器的 MFC 类 [^]
历史
- 2009 年 4 月 26 日:初始发布