MFC classes for multiple monitors
来源:互联网 发布:布衣乐队知乎 编辑:程序博客网 时间:2024/09/21 09:04
- Download source files - 6 Kb
- Download demo project - 30 Kb
Introduction
Awhile back I decided to write a screensaver application as a way to pick up MFC. After I got my first implementation up and running I shipped it off to a friend. Well the first thing he noted was a a glaring bug: he uses multiple monitors and my screen saver only showed up on one.
After some scouring of the internet and research into MSDN I was able to to get the screensaver running on multiple monitors by using some API calls the were introduced with Windows 98/2000. This project is a couple of small MFC classes that wrap the multi-monitor API.
These classes can be safely used on Windows 95/NT4 as well. On those platforms, they just provide the properties of the one and only monitor.
Background
Eveyrything you ever wanted to know about the API for multiple monitors is described in a very well written article by David Campbell in the June 1997 issue of MSJ.
The API itself is simple and straightforward. It has some new constants to pass to GetSystemMetrics
and a couple methods to enumerate all of the monitors currently attached to the system and get the properties of a given monitor.
All of the types and functions are defined by the platform SDK in the file multimon.h
. If you #define WINVER
to be greater than 0x400
you don't need to link to multimon.h
, as the API gets defined in windef.h for Windows 98 and later targeted builds. Multimon.h
also provides some stub functions that allow calls to be safely made on Windows 95/NT4 machines. These wrappers interrogate the runtime OS and either make fall-through calls into the actual API or return the propeties of the one (and only) monitor on those older platforms.
The multi monitor classes take care of including multimon.h
correctly depending on the value of WINVER
.
Using the code
CMointor
is a basic MFC class that allows you to safely use the multi-monitor API on any Win32 platform.
There are three classes in this library:
CMonitors
represents the collection of monitors currently attached to the system and wraps the EnumDisplayMonitors
API function.
//CMonitors' interfaceCMonitor GetMonitor( const int index ) const;int GetCount() const; //returns the monitor closest to the specified itemstatic CMonitor GetNearestMonitor( const LPRECT lprc );static CMonitor GetNearestMonitor( const POINT pt );static CMonitor GetNearestMonitor( const CWnd* pWnd );//is the specificed item visible on any monitorstatic BOOL IsOnScreen( const POINT pt );static BOOL IsOnScreen( const CWnd* pWnd );static BOOL IsOnScreen( const LPRECT lprc );//returns the rectangle encompassing all monitorsstatic void GetVirtualDesktopRect( LPRECT lprc );//determines whether the given handle is a valid monitor handlestatic BOOL IsMonitor( const HMONITOR hMonitor );static CMonitor GetPrimaryMonitor();static BOOL AllMonitorsShareDisplayFormat();static int GetMonitorCount();
CMonitor
is a wrapper around an HMONITOR
handle (returned from EnumDisplayMonitors
) and the GetMonitorInfo
function. With CMonitor
you can get at the characteristics of a given monitor.
//The interface of CMonitor void Attach( const HMONITOR hMonitor );HMONITOR Detach();void ClipRectToMonitor( LPRECT lprc,
const BOOL UseWorkAreaRect = FALSE ) const;void CenterRectToMonitor( LPRECT lprc,
const BOOL UseWorkAreaRect = FALSE ) const;void CenterWindowToMonitor( CWnd* const pWnd,
const BOOL UseWorkAreaRect = FALSE ) const;//creates a device context for the monitor - the client is responsible for
// DeleteDCHDC CreateDC() const;void GetMonitorRect( LPRECT lprc ) const;//the work area is the monitor rect minus the start barvoid GetWorkAreaRect( LPRECT lprc ) const;void GetName( CString& string ) const;int GetBitsPerPixel() const;//determines if the specified item on the monitorBOOL IsOnMonitor( const POINT pt ) const;BOOL IsOnMonitor( const CWnd* pWnd ) const;BOOL IsOnMonitor( const LPRECT lprc ) const;BOOL IsPrimaryMonitor() const;BOOL IsMonitor() const;
CMonitorDC
is a CDC
derived class that represents a monitor specific device context. I haven't really gone to far with this class but it seemed like a logical part of the library.
Known Limitations
CMonitor
and CMonitors
rely on the assumption that a monitor handle does not change. This has proved to be a safe assumption empirically but isn't nessecarily a guarantee.
http://www.codeproject.com/KB/GDI/multimon.aspx
- MFC classes for multiple monitors
- directx Multiple Display Monitors
- 【Size Classes Design Help】- About Designing for Multiple Size Classes
- Move windows between multiple monitors
- c# 多显示器设置主屏幕(Set primary screen for multiple monitors)
- Link-Multiple classes
- WTL for MFC Programmers, Part I - ATL GUI Classes
- WTL for MFC Programmers, Part II - WTL GUI Base Classes
- WTL for MFC Programmers, Part I - ATL GUI Classes
- WTL for MFC Programmers, Part I - ATL GUI Classes
- WTL for MFC Programmers, Part I - ATL GUI Classes
- WTL for MFC Programmers, Part II - WTL GUI Base Classes
- WTL for MFC Programmers, Part V - Advanced Dialog UI Classes
- WTL for MFC Programmers, Part I - ATL GUI Classes
- WTL for MFC Programmers, Part II - WTL GUI Base Classes
- WTL for MFC Programmers, Part V - Advanced Dialog UI Classes
- WTL for MFC Programmers, Part I - ATL GUI Classes
- WTL for MFC Programmers, Part IX - GDI Classes, Common Dialogs, and Utility Classes
- 斯坦福大学开放课程——编程方法 作业2 - 6
- Tomcat配置任意目录下的Web应用程序[转]
- XML基础学习
- 向数据库添加连续日期(年和日期整体)
- 如何利用问答提高排名和权重
- MFC classes for multiple monitors
- *P 和(*P)的差别 用一维数组代替二维数组
- CentOS FTP服务(vsftpd)配置
- android 线程中的ui问题 Handler的基本使用 关于获取动态时间在ui的问题
- Windows Azure真实案例:微软IT-将拍卖工具搬移至云端,方便雇员捐赠
- 任务管理模块遇到的问题-上
- 别让性格影响你的职场生涯
- 来看看吧,有用的GDI+双缓冲写法
- 查找端口占用