Rabbit Remote Control 0.0.30
Loading...
Searching...
No Matches
DisplayWindows.cpp
1#include "DisplayWindows.h"
2#include <Windows.h>
3#include <QLoggingCategory>
4
5Q_DECLARE_LOGGING_CATEGORY(logDW)
6CDisplayWindows::CDisplayWindows(QObject *parent) : CDesktop(parent)
7{
8 m_DC = GetDesktopDC();
9 m_MemDc = NULL;
10 m_Bitmap = NULL;
11 m_Width = m_Height = 0;
12}
13
14CDisplayWindows::~CDisplayWindows()
15{
16 DeleteObject(m_Bitmap);
17 DeleteDC(m_MemDc);
18 ReleaseDC(NULL, m_DC);
19}
20
21CDesktop* CDesktop::Instance()
22{
23 static CDesktop* p = nullptr;
24 if(!p) p = new CDisplayWindows();
25 return p;
26}
27
28HDC CDisplayWindows::GetDesktopDC()
29{
30 //m_DC = CreateDC(_T("DISPLAY"),NULL,NULL,NULL ); // Primary screen
31 m_DC = GetDC(GetDesktopWindow()); // Multi-screen;
32 return m_DC;
33}
34
35int CDisplayWindows::Width()
36{
37 return GetDeviceCaps(m_DC, HORZRES); // pixel
38}
39
40int CDisplayWindows::Height()
41{
42 return GetDeviceCaps(m_DC, VERTRES); // pixel
43}
44
45QImage CDisplayWindows::GetDisplay(int width, int height, int x, int y)
46{
47 QImage img;
48 bool bChange = false;
49
50 do{
51 if(NULL == m_DC)
52 {
53 qCritical(logDW,
54 "GetDC fail: %d",
55 GetLastError());
56 return img;
57 }
58
59 if(width != m_Width || height != m_Height
60 || NULL == m_MemDc || NULL == m_Bitmap)
61 {
62 bChange = true;
63 m_Width = width;
64 m_Height = height;
65 if(-1 == width) m_Width = Width();
66 if(-1 == height) m_Height = Height();
67 }
68
69 if(bChange)
70 {
71 DeleteObject(m_Bitmap);
72 DeleteDC(m_MemDc);
73
74 m_MemDc = CreateCompatibleDC(m_DC);
75 if(NULL == m_MemDc)
76 {
77 qCritical(logDW,
78 "CreateCompatibleDC fail: %d",
79 GetLastError());
80 break;
81 }
82
83 m_Bitmap = CreateCompatibleBitmap(m_DC, m_Width, m_Height);
84 if(NULL == m_Bitmap)
85 {
86 qCritical(logDW,
87 "CreateCompatibleBitmap fail: %d",
88 GetLastError());
89 break;
90 }
91 HGDIOBJ oldBitmap = SelectObject(m_MemDc, m_Bitmap);
92 if(NULL == oldBitmap)
93 {
94 qCritical(logDW,
95 "SelectObject fail: %d",
96 GetLastError());
97 break;
98 }
99 }
100 if(!BitBlt(m_MemDc, 0, 0, m_Width, m_Height, m_DC, x, y, SRCCOPY))
101 {
102 qCritical(logDW,
103 "BitBlt fail: %d",
104 GetLastError());
105 break;
106 }
107
108 //SelectObject(memDc, oldBitmap);
109
110 // Get the bitmap format information
111 BITMAPINFO bi;
112 memset(&bi, 0, sizeof(bi));
113 bi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
114 bi.bmiHeader.biBitCount = 0;
115
116 bi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
117 bi.bmiHeader.biWidth = m_Width;
118 bi.bmiHeader.biHeight = m_Height;
119 bi.bmiHeader.biPlanes = 1;
120 bi.bmiHeader.biBitCount = 32;
121 bi.bmiHeader.biCompression = BI_RGB;
122 bi.bmiHeader.biSizeImage = 0;
123 bi.bmiHeader.biXPelsPerMeter = 0;
124 bi.bmiHeader.biYPelsPerMeter = 0;
125 bi.bmiHeader.biClrUsed = 0;
126 bi.bmiHeader.biClrImportant = 0;
127
128 img = QImage(m_Width, m_Height, QImage::Format_ARGB32);
129
130 // Get image
131 if (!::GetDIBits(m_MemDc, m_Bitmap, 0, m_Height,
132 img.bits(), (BITMAPINFO*)&bi, DIB_RGB_COLORS))
133 {
134 qCritical(logDW,
135 "Get image fail: %d",
136 GetLastError());
137 break;
138 }
139
140 img = img.mirrored();
141 } while(0);
142
143 DeleteObject(m_Bitmap);
144 DeleteDC(m_MemDc);
145 return img;
146}