首页 > 代码库 > Capture all the GUI windows' Image that Running on the Desktop

Capture all the GUI windows' Image that Running on the Desktop


Here I captured all the GUI images that running on the desktop, including the GUI that running on the virtual desktop that is created via "task view" under WIN 10.

Please note that the GUI that is in minimize status cannot be captured in this method. Also, the Internet Explorer cannot be captured with this method.

#include <gdiplus.h>
using namespace Gdiplus;
GdiplusStartupInput gdiplusStartupInput;
ULONG_PTR gdiplusToken;
BOOL CvirtualDesktopDlg::OnInitDialog()
{
CDialogEx::OnInitDialog();

// Set the icon for this dialog. The framework does this automatically
// when the application‘s main window is not a dialog
SetIcon(m_hIcon, TRUE);    // Set big icon
SetIcon(m_hIcon, FALSE);    // Set small icon

// TODO: Add extra initialization here
GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
std::ofstream mylog("mylog.log");
CWnd* pDesktopWnd = CWnd::GetDesktopWindow();
CWnd* pWnd = pDesktopWnd->GetWindow(GW_CHILD);
int jpgnum = 0;
while (pWnd != NULL)
{
mylog << "number:" << jpgnum << endl;
CString strClassName = _T(" ");
::GetClassName(pWnd->GetSafeHwnd(), strClassName.GetBuffer(256), 256);
mylog << "class name\t:" << (CT2A)(LPCTSTR) strClassName << endl;
CString strWindowText = _T(" ");
::GetWindowText(pWnd->GetSafeHwnd(), strWindowText.GetBuffer(256), 256);
mylog << "window text\t:" << (CT2A)(LPCTSTR)strWindowText << endl;

char jpgfilename[1024];
sprintf_s(jpgfilename, "jpg_%d.jpg", jpgnum++);
//jpgfilename.Format(_T("jpg_%d"), jpgnum++);
USES_CONVERSION;
saveDesktoptoJPG(A2T(jpgfilename), pWnd->m_hWnd);

pWnd = pWnd->GetWindow(GW_HWNDNEXT);
}
mylog.close();
return TRUE; // return TRUE unless you set the focus to a control
}

void CvirtualDesktopDlg::saveDesktoptoJPG(TCHAR *jpgFilePathName, HWND hwnd)
{
//Get desktop DC
/*
HWND hDeskWnd = ::GetDesktopWindow();
CDC *pDestDC = CDC::FromHandle(::GetDC(hDeskWnd));
*/
CDC *pDestDC = CDC::FromHandle(::GetDC(hwnd) );
int screenWidth = pDestDC->GetDeviceCaps(HORZRES);
int screenHeight = pDestDC->GetDeviceCaps(VERTRES);
//Create Bitmap that is comatible with desktop
CBitmap memBitmap;
memBitmap.CreateCompatibleBitmap(pDestDC, screenWidth, screenHeight);
//create memory DC for desktop
CDC memDC;
memDC.CreateCompatibleDC(pDestDC);
memDC.SelectObject(&memBitmap);
//copy desktop DC to the memery DC
memDC.BitBlt(0, 0, screenWidth, screenHeight, pDestDC, 0, 0, SRCCOPY);
//obtain the bitmap information
BITMAP bmpInfo;
memBitmap.GetBitmap(&bmpInfo);
//generate BITMAPINFO
BITMAPINFO m_BITMAPINFO;
memset(&m_BITMAPINFO, 0, sizeof(BITMAPINFO));
m_BITMAPINFO.bmiHeader.biSize = sizeof(BITMAPINFO);
m_BITMAPINFO.bmiHeader.biPlanes = 1;
m_BITMAPINFO.bmiHeader.biBitCount = bmpInfo.bmBitsPixel;
m_BITMAPINFO.bmiHeader.biCompression = BI_RGB;
m_BITMAPINFO.bmiHeader.biWidth = bmpInfo.bmWidth;
m_BITMAPINFO.bmiHeader.biHeight = bmpInfo.bmHeight;
//get the data of bitmap
BYTE* pBuffer = new BYTE[bmpInfo.bmWidthBytes*bmpInfo.bmHeight];
GetDIBits(memDC.m_hDC, (HBITMAP)memBitmap.m_hObject, 0, screenHeight, pBuffer, (LPBITMAPINFO)&m_BITMAPINFO, DIB_RGB_COLORS);
//generate bitmap
Bitmap *pSrcBmp = Bitmap::FromBITMAPINFO(&m_BITMAPINFO, (void*)pBuffer);
//save to jpg
CLSID encoderClsid;
GetEncoderClsid(L"image/jpeg", &encoderClsid);
// pSrcBmp->Save(L"D:\\desktop.jpg", &encoderClsid);
pSrcBmp->Save(jpgFilePathName, &encoderClsid);

delete pSrcBmp;
delete pBuffer;
ReleaseDC(pDestDC);
}
int CvirtualDesktopDlg::GetEncoderClsid(const WCHAR* format, CLSID* pClsid)
{
/*
The preceding code produces the following output:
image/bmp
image/jpeg
image/gif
image/x-emf
image/x-wmf
image/tiff
image/png
image/x-icon
*/
UINT num = 0; // number of image decoders
UINT size = 0; // size, in bytes, of the image decoder array

ImageCodecInfo* pImageCodecInfo = NULL;
// How many decoders are there?
// How big (in bytes) is the array of all ImageCodecInfo objects?
GetImageEncodersSize(&num, &size);
if (size == 0)
{
return -1;
}
// Create a buffer large enough to hold the array of ImageCodecInfo
// objects that will be returned by GetImageDecoders.
pImageCodecInfo = (ImageCodecInfo*)(malloc(size));
if (pImageCodecInfo == NULL)
{
return -1;
}
// GetImageDecoders creates an array of ImageCodecInfo objects
// and copies that array into a previously allocated buffer.
// The third argument, imageCodecInfos, is a pointer to that buffer.
GetImageEncoders(num, size, pImageCodecInfo);

for (UINT j = 0; j< num; ++j)
{
if (wcscmp(pImageCodecInfo[j].MimeType, format) == 0)
{
*pClsid = pImageCodecInfo[j].Clsid;
free(pImageCodecInfo);
return j; //success
}
}
free(pImageCodecInfo);
return -1; //failure
}

Rusult:

file: <mylog.log>

number:0
class name :MSCTFIME UI
window text :MSCTFIME UI
number:1
class name :IME
window text :Default IME
number:2
class name :MSCTFIME UI
window text :MSCTFIME UI
number:3
class name :IME
window text :Default IME
number:4
class name :ForegroundStaging
window text :
number:5
class name :ForegroundStaging
window text :
number:6
class name :TaskListThumbnailWnd
window text :
number:7
class name :tooltips_class32
window text :
number:8
class name :tooltips_class32
window text :

 

Capture all the GUI windows' Image that Running on the Desktop