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

时间: 2024-10-23 20:41:20

Capture all the GUI windows' Image that Running on the Desktop的相关文章

(二) Windows 进行 Docker CE 安装(Docker Desktop)

参考并感谢 官方文档: https://docs.docker.com/docker-for-windows/install/ 下载地址 https://download.docker.com/win/stable/Docker%20for%20Windows%20Installer.exe 操作系统要求 Windows 10 64位:Pro,Enterprise或Education(Build 15063或更高版本) 必须启用Hyper-V和容器Windows功能 在Windows 10上成功

Running a Remote Desktop on a Windows Azure Linux VM (远程桌面到Windows Azure Linux )-摘自网络

A complete click-by-click, step-by-step video of this article is available here. OR… You can read the article, line-by-line. It all starts with a Linux Server running in the Windows Azure cloud… Onto this you’ll install a remote desktop protocol (RDP

Connect to a Windows PC from Ubuntu via Remote Desktop Connection

http://www.7tutorials.com/connecting-windows-remote-desktop-ubuntu A useful feature of Windows is being able to connect to your Desktop from another location to remotely manage your computer. While this functionality is native in Windows, it is not s

Mac 10.12安装Windows远程桌面工具Microsoft Remote Desktop

说明:之前Office自带的Windows远程桌面工具虽然简便,但是保存的服务器列表有限.而这个微软推出的自家工具可以完美解决这些问题. 下载: (链接:https://pan.baidu.com/s/1c2feWxU 密码: aq1r)

Windows Server 2016 Software-Defined-Datacenter Features

Windows Server 2016 is the cloud-ready operating system (OS) that delivers new layers of security and Microsoft Azure-inspired innovation for the applications and infrastructure that power your business. One of the important feature is Softwzre-defin

C# windows服务启动winform程序不显示UI问题解决

由于工作需要写一个解决winform程序自动更新下载重启的自动更新程序,之前用控制台全部实现,然而换成windows  service出现了两个问题,一个是路径问题(http://baidu.com),一个是服务启动其他winform程序不显示UI问题. 本篇解决UI显示问题. 以下为引用尤尼博文(原文地址:http://www.cnblogs.com/luxilin/p/3347212.html): 我开发的系统中有一接口程序(这里就称Task,是一个C#的Console Applicatio

Windows软件在Linux上的等价/替代/模仿软件列表 (抄一个)

Last update: 16.07.2003, 31.01.2005, 27.05.2005 您可在以下网站发现本列表最新版:http://www.linuxrsp.ru/win-lin-soft/. This page on other languages: Russian, Italian, Spanish, French, German. 从Windows转向Linux的一个最大困难就是缺乏对相关软件的认识.新手总是在寻找 Windows软件的替代品,但老鸟却难以回答,因为他们对Wind

linux和windows下安装python拓展包及requirement.txt安装类库

http://blog.csdn.net/pipisorry/article/details/39902327 python拓展包安装 直接安装拓展包默认路径: Unix(Linux)默认路径:/usr/local/lib/pythonX.Y/site-packagesWindows默认路径:C:\PythonXY\Lib\site-packages 測试和升级python拓展安装包pip 查看pip安装时相应的python版本号 which pip /d/python3.4.2/Scripts

Upgrade Windows Server 2016 to Windows Server 2019

Pre-Upgrade Upgrade path: Windows Server 2016 can be upgraded to Windows 2019 in a single upgrade process. Support: In-place Upgrade is supported for Windows Server 2016 on physicaL hardware, and in Virtual Machines. Public and private cloud companie