35#include "ScreenWindows.h"
37#include <QLoggingCategory>
39Q_DECLARE_LOGGING_CATEGORY(logDW)
48CScreenWindows::CScreenWindows(QObject *parent) :
CScreen(parent)
50 m_Format = QImage::Format_ARGB32;
53CScreenWindows::~CScreenWindows()
55 qDebug(logDW,
"CScreenWindows::~CScreenWindows");
58int CScreenWindows::Width()
63 return GetSystemMetrics(SM_CXSCREEN);
64 return m_Screen.width();
67int CScreenWindows::Height()
72 return GetSystemMetrics(SM_CYSCREEN);
73 return m_Screen.height();
76int CScreenWindows::VirtualTop()
78 return GetSystemMetrics(SM_XVIRTUALSCREEN);
81int CScreenWindows::VirtualLeft()
83 return GetSystemMetrics(SM_YVIRTUALSCREEN);
86int CScreenWindows::VirtualWidth()
88 return GetSystemMetrics(SM_CXVIRTUALSCREEN);
91int CScreenWindows::VirtualHeight()
93 return GetSystemMetrics(SM_CYVIRTUALSCREEN);
96int CScreenWindows::VisibleMonitorCount()
98 return GetSystemMetrics(SM_CMONITORS);
101QImage::Format CScreenWindows::GetFormat(
int index)
106int CScreenWindows::SetFormat(QImage::Format f)
112QImage CScreenWindows::GetScreen(
int index)
114 int nRet = GetImage(
true);
115 if(nRet)
return QImage();
117 if(QImage::Format_Invalid == m_Format)
118 m_Format = m_Screen.format();
119 if(m_Screen.format() != m_Format && QImage::Format_Invalid != m_Format)
120 m_Screen = m_Screen.convertToFormat(m_Format);
124int CScreenWindows::GetImage(
bool bBuffer)
128 if(!m_Screen.isNull() && !bBuffer)
133 HBITMAP bitmap = NULL;
137 dc = GetDC(GetDesktopWindow());
146 memDc = CreateCompatibleDC(dc);
150 "CreateCompatibleDC fail: %d",
155 int ScreenWidth = GetDeviceCaps(dc, HORZRES);
156 int ScreenHeight = GetDeviceCaps(dc, VERTRES);
157 bitmap = CreateCompatibleBitmap(dc, ScreenWidth, ScreenHeight);
161 "CreateCompatibleBitmap fail: %d",
165 HGDIOBJ oldBitmap = SelectObject(memDc, bitmap);
166 if(NULL == oldBitmap)
169 "SelectObject fail: %d",
173 if(!BitBlt(memDc, 0, 0, ScreenWidth, ScreenHeight, dc, 0, 0, SRCCOPY))
185 ::GetCursorPos(&point);
186 HCURSOR hCursor = ::GetCursor();
187 ::DrawIcon(memDc, point.x, point.y, hCursor);
194 memset(&bi, 0,
sizeof(bi));
195 bi.bmiHeader.biSize =
sizeof(BITMAPINFOHEADER);
196 bi.bmiHeader.biBitCount = 0;
200 bi.bmiHeader.biSize =
sizeof(BITMAPINFOHEADER);
201 bi.bmiHeader.biWidth = ScreenWidth;
202 bi.bmiHeader.biHeight = ScreenHeight;
203 bi.bmiHeader.biPlanes = 1;
204 bi.bmiHeader.biBitCount = 32;
205 bi.bmiHeader.biCompression = BI_RGB;
206 bi.bmiHeader.biSizeImage = 0;
207 bi.bmiHeader.biXPelsPerMeter = 0;
208 bi.bmiHeader.biYPelsPerMeter = 0;
209 bi.bmiHeader.biClrUsed = 0;
210 bi.bmiHeader.biClrImportant = 0;
212 if(m_Screen.isNull() || m_Screen.format() != QImage::Format_ARGB32)
213 m_Screen = QImage(ScreenWidth, ScreenHeight, QImage::Format_ARGB32);
216 if (!::GetDIBits(memDc, bitmap, 0, ScreenHeight,
217 m_Screen.bits(), (BITMAPINFO*)&bi, DIB_RGB_COLORS))
220 "Get image fail: %d",
225 m_Screen = m_Screen.mirrored();
229 if (!::GetDIBits(memDc, bitmap, 0, ScreenHeight, NULL, (BITMAPINFO*)&bi, DIB_RGB_COLORS))
232 "unable to determine device pixel format: %d",
244 if(bi.bmiHeader.biBitCount > 8)
246 switch(bi.bmiHeader.biCompression)
250 switch (bi.bmiHeader.biBitCount) {
253 qInfo(logDW,
"16-bit High Colour");
254 m_Screen = QImage(ScreenWidth, ScreenHeight, QImage::Format_RGB555);
257 qInfo(logDW,
"24-bit High Colour");
258 m_Screen = QImage(ScreenWidth, ScreenHeight, QImage::Format_RGB888);
262 qInfo(logDW,
"32-bit High Colour");
263 m_Screen = QImage(ScreenWidth, ScreenHeight, QImage::Format_ARGB32);
266 qCritical(logDW,
"bits per pixel %u not supported", bi.bmiHeader.biBitCount);
272 switch (bi.bmiHeader.biBitCount) {
275 qInfo(logDW,
"16-bit High Colour");
276 m_Screen = QImage(ScreenWidth, ScreenHeight, QImage::Format_RGB555);
280 qInfo(logDW,
"32-bit High Colour");
281 m_Screen = QImage(ScreenWidth, ScreenHeight, QImage::Format_ARGB32);
284 qCritical(logDW,
"bits per pixel %u not supported", bi.bmiHeader.biBitCount);
292 qCritical(logDW,
"bi.bmiHeader.biBitCount < 8");
298 DeleteObject(bitmap);