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

來源:互聯網
上載者:User

標籤:status   cto   mha   ted   sel   gdi   ice   printf   rssi   


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 dialogSetIcon(m_hIcon, TRUE);    // Set big iconSetIcon(m_hIcon, FALSE);    // Set small icon// TODO: Add extra initialization hereGdiplusStartup(&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 desktopCBitmap memBitmap;memBitmap.CreateCompatibleBitmap(pDestDC, screenWidth, screenHeight);//create memory DC for desktopCDC memDC;memDC.CreateCompatibleDC(pDestDC);memDC.SelectObject(&memBitmap);//copy desktop DC to the memery DCmemDC.BitBlt(0, 0, screenWidth, screenHeight, pDestDC, 0, 0, SRCCOPY);//obtain the bitmap informationBITMAP bmpInfo;memBitmap.GetBitmap(&bmpInfo);//generate BITMAPINFOBITMAPINFO 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 bitmapBYTE* 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 bitmapBitmap *pSrcBmp = Bitmap::FromBITMAPINFO(&m_BITMAPINFO, (void*)pBuffer);//save to jpgCLSID 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/bmpimage/jpegimage/gifimage/x-emfimage/x-wmfimage/tiffimage/pngimage/x-icon*/UINT num = 0; // number of image decodersUINT size = 0; // size, in bytes, of the image decoder arrayImageCodecInfo* 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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.