36#include "DesktopWindows.h"
38#include <QLoggingCategory>
40Q_LOGGING_CATEGORY(logDW,
"DesktopWindows")
44 m_DC = GetDesktopDC();
46 m_Bitmap = m_DesktopBitmap = NULL;
47 m_Width = m_Height = 0;
49 m_MemDC = CreateCompatibleDC(m_DC);
52 m_DesktopBitmap = CreateCompatibleBitmap(m_DC, Width(), Height());
53 if(NULL == m_DesktopBitmap)
55 "CreateCompatibleBitmap fail: %d",
59 "CreateCompatibleDC fail: %d",
64 HGDIOBJ oldBitmap = SelectObject(m_MemDC, m_DesktopBitmap);
66 qCritical(logDW,
"SelectObject fail: %d", GetLastError());
68 m_Desktop = QImage(Width(), Height(), QImage::Format_ARGB32);
72CDesktopWindows::~CDesktopWindows()
75 DeleteObject(m_Bitmap);
77 DeleteObject(m_DesktopBitmap);
80 ReleaseDC(NULL, m_DC);
90HDC CDesktopWindows::GetDesktopDC()
95 m_DC = GetDC(GetDesktopWindow());
100int CDesktopWindows::Width()
102 return GetSystemMetrics(SM_CXVIRTUALSCREEN);
106int CDesktopWindows::Height()
108 return GetSystemMetrics(SM_CYVIRTUALSCREEN);
112QImage CDesktopWindows::GetDesktop()
115 if(!BitBlt(m_MemDC, 0, 0, Width(), Height(), m_DC, 0, 0, SRCCOPY))
127 memset(&bi, 0,
sizeof(bi));
128 bi.bmiHeader.biSize =
sizeof(BITMAPINFOHEADER);
129 bi.bmiHeader.biBitCount = 0;
131 bi.bmiHeader.biSize =
sizeof(BITMAPINFOHEADER);
132 bi.bmiHeader.biWidth = Width();
133 bi.bmiHeader.biHeight = Height();
134 bi.bmiHeader.biPlanes = 1;
135 bi.bmiHeader.biBitCount = 32;
136 bi.bmiHeader.biCompression = BI_RGB;
137 bi.bmiHeader.biSizeImage = 0;
138 bi.bmiHeader.biXPelsPerMeter = 0;
139 bi.bmiHeader.biYPelsPerMeter = 0;
140 bi.bmiHeader.biClrUsed = 0;
141 bi.bmiHeader.biClrImportant = 0;
144 if (!::GetDIBits(m_MemDC, m_DesktopBitmap, 0, Height(),
145 m_Desktop.bits(), (BITMAPINFO*)&bi, DIB_RGB_COLORS))
148 "Get image fail: %d",
153 m_Desktop = m_Desktop.mirrored();
159QImage CDesktopWindows::GetDesktop(
int x,
int y,
int width,
int height)
162 bool bChange =
false;
173 if(width != m_Width || height != m_Height
174 || NULL == m_MemDC || NULL == m_Bitmap)
179 if(-1 == width) m_Width = Width();
180 if(-1 == height) m_Height = Height();
185 DeleteObject(m_Bitmap);
187 m_Bitmap = CreateCompatibleBitmap(m_DC, m_Width, m_Height);
191 "CreateCompatibleBitmap fail: %d",
195 HGDIOBJ oldBitmap = SelectObject(m_MemDC, m_Bitmap);
196 if(NULL == oldBitmap)
199 "SelectObject fail: %d",
205 if(!BitBlt(m_MemDC, 0, 0, m_Width, m_Height, m_DC, x, y, SRCCOPY))
217 memset(&bi, 0,
sizeof(bi));
218 bi.bmiHeader.biSize =
sizeof(BITMAPINFOHEADER);
219 bi.bmiHeader.biBitCount = 0;
221 bi.bmiHeader.biSize =
sizeof(BITMAPINFOHEADER);
222 bi.bmiHeader.biWidth = m_Width;
223 bi.bmiHeader.biHeight = m_Height;
224 bi.bmiHeader.biPlanes = 1;
225 bi.bmiHeader.biBitCount = 32;
226 bi.bmiHeader.biCompression = BI_RGB;
227 bi.bmiHeader.biSizeImage = 0;
228 bi.bmiHeader.biXPelsPerMeter = 0;
229 bi.bmiHeader.biYPelsPerMeter = 0;
230 bi.bmiHeader.biClrUsed = 0;
231 bi.bmiHeader.biClrImportant = 0;
233 img = QImage(m_Width, m_Height, QImage::Format_ARGB32);
236 if (!::GetDIBits(m_MemDC, m_Bitmap, 0, m_Height,
237 img.bits(), (BITMAPINFO*)&bi, DIB_RGB_COLORS))
240 "Get image fail: %d",
245 img = img.mirrored();
248 DeleteObject(m_Bitmap);
253QImage CDesktopWindows::GetCursor(QPoint &pos, QPoint &posHot)
259QPoint CDesktopWindows::GetCursorPosition()