1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
|
/*
* $Id$
*
* Author: Donald Kackman
* Email: don@itsEngineering.com
* Copyright 2002, Donald Kackman
*
* This file is part of mplayerc.
*
* Mplayerc is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* Mplayerc is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include "stdafx.h"
#include "Monitors.h"
// CMonitors
CMonitors::CMonitors()
{
// WARNING : GetSystemMetrics(SM_CMONITORS) return only visible display monitors, and EnumDisplayMonitors
// enumerate visible and pseudo invisible monitors !!!
// m_MonitorArray.SetSize( GetMonitorCount() );
ADDMONITOR addMonitor;
addMonitor.pMonitors = &m_MonitorArray;
addMonitor.currentIndex = 0;
::EnumDisplayMonitors( NULL, NULL, AddMonitorsCallBack, (LPARAM)&addMonitor );
}
CMonitors::~CMonitors()
{
for ( int i = 0; i < m_MonitorArray.GetSize(); i++ )
delete m_MonitorArray.GetAt( i );
}
// CMonitors member functions
BOOL CALLBACK CMonitors::AddMonitorsCallBack( HMONITOR hMonitor, HDC hdcMonitor, LPRECT lprcMonitor, LPARAM dwData )
{
LPADDMONITOR pAddMonitor = (LPADDMONITOR)dwData;
CMonitor* pMonitor = DNew CMonitor;
pMonitor->Attach( hMonitor );
pAddMonitor->pMonitors->Add( pMonitor );
pAddMonitor->currentIndex++;
return TRUE;
}
//
// returns the primary monitor
CMonitor CMonitors::GetPrimaryMonitor()
{
//the primary monitor always has its origin at 0,0
HMONITOR hMonitor = ::MonitorFromPoint( CPoint( 0,0 ), MONITOR_DEFAULTTOPRIMARY );
ASSERT( IsMonitor( hMonitor ) );
CMonitor monitor;
monitor.Attach( hMonitor );
ASSERT( monitor.IsPrimaryMonitor() );
return monitor;
}
//
// is the given handle a valid monitor handle
BOOL CMonitors::IsMonitor( const HMONITOR hMonitor )
{
if ( hMonitor == NULL )
return FALSE;
MATCHMONITOR match;
match.target = hMonitor;
match.foundMatch = FALSE;
::EnumDisplayMonitors( NULL, NULL, FindMatchingMonitorHandle, (LPARAM)&match );
return match.foundMatch;
}
//this is the callback method that gets called via IsMontior
BOOL CALLBACK CMonitors::FindMatchingMonitorHandle( HMONITOR hMonitor, HDC hdcMonitor, LPRECT lprcMonitor, LPARAM dwData )
{
LPMATCHMONITOR pMatch = (LPMATCHMONITOR)dwData;
if ( hMonitor == pMatch->target )
{
//found a monitor with the same handle we are looking for
pMatch->foundMatch = TRUE;
return FALSE; //stop enumerating
}
//haven't found a match yet
pMatch->foundMatch = FALSE;
return TRUE; //keep enumerating
}
BOOL CMonitors::AllMonitorsShareDisplayFormat()
{
return ::GetSystemMetrics( SM_SAMEDISPLAYFORMAT );
}
//
// the number of monitors on the system
int CMonitors::GetMonitorCount()
{
return ::GetSystemMetrics(SM_CMONITORS);
}
CMonitor CMonitors::GetMonitor( const int index ) const
{
#if _MFC_VER >= 0x0700
ASSERT( index >= 0 && index < m_MonitorArray.GetCount() );
#else
ASSERT( index >= 0 && index < m_MonitorArray.GetSize() );
#endif
CMonitor* pMonitor = static_cast<CMonitor*>(m_MonitorArray.GetAt( index ));
return *pMonitor;
}
//
// returns the rectangle that is the union of all active monitors
void CMonitors::GetVirtualDesktopRect( LPRECT lprc )
{
::SetRect( lprc,
::GetSystemMetrics( SM_XVIRTUALSCREEN ),
::GetSystemMetrics( SM_YVIRTUALSCREEN ),
::GetSystemMetrics( SM_CXVIRTUALSCREEN ),
::GetSystemMetrics( SM_CYVIRTUALSCREEN ) );
}
//
// these methods determine wheter the given item is
// visible on any monitor
BOOL CMonitors::IsOnScreen( const LPRECT lprc )
{
return ::MonitorFromRect( lprc, MONITOR_DEFAULTTONULL ) != NULL;
}
BOOL CMonitors::IsOnScreen( const POINT pt )
{
return ::MonitorFromPoint( pt, MONITOR_DEFAULTTONULL ) != NULL;
}
BOOL CMonitors::IsOnScreen( const CWnd* pWnd )
{
return ::MonitorFromWindow( pWnd->GetSafeHwnd(), MONITOR_DEFAULTTONULL ) != NULL;
}
CMonitor CMonitors::GetNearestMonitor( const LPRECT lprc )
{
CMonitor monitor;
monitor.Attach( ::MonitorFromRect( lprc, MONITOR_DEFAULTTONEAREST ) );
return monitor;
}
CMonitor CMonitors::GetNearestMonitor( const POINT pt )
{
CMonitor monitor;
monitor.Attach( ::MonitorFromPoint( pt, MONITOR_DEFAULTTONEAREST ) );
return monitor;
}
CMonitor CMonitors::GetNearestMonitor( const CWnd* pWnd )
{
ASSERT( pWnd );
ASSERT( ::IsWindow( pWnd->m_hWnd ) );
CMonitor monitor;
monitor.Attach( ::MonitorFromWindow( pWnd->GetSafeHwnd(), MONITOR_DEFAULTTONEAREST ) );
return monitor;
}
|