⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cescreencapture.cpp

📁 通过同步软件来获取移动智能设备屏幕画面
💻 CPP
字号:
// CeScreenCapture.cpp : Defines the entry point for the DLL application.
//

#include "stdafx.h"
#include "CeScreenCapture.h"
#include <windows.h>
#include <commctrl.h>
#if _WIN32_WCE >= 0x500
#include <ddraw.h>
#else
#include "gc.h"
#endif
#include "dibsection.h"


BOOL APIENTRY DllMain( HANDLE hModule, 
                       DWORD  ul_reason_for_call, 
                       LPVOID lpReserved
					 )
{
	switch (ul_reason_for_call)
	{
	case DLL_PROCESS_ATTACH:
	case DLL_THREAD_ATTACH:
	case DLL_THREAD_DETACH:
	case DLL_PROCESS_DETACH:
		break;
	}
    return TRUE;
}


#if _WIN32_WCE >= 0x500


// CeScreenCapture
//
//		Captures the device screen into a bitmap and send the data back to the desktop.
//
CESCREENCAPTURE_API int CeScreenCapture(DWORD cbInput, BYTE* pInput, DWORD* pcbOutput, BYTE** ppOutput, LPVOID pReserved)
{
	int						nScreenX	= GetSystemMetrics(SM_CXSCREEN),
							nScreenY	= GetSystemMetrics(SM_CYSCREEN);
	CComPtr<IDirectDraw>	spDD;
	HRESULT					hr;

	LocalFree(pInput);
	*pcbOutput = 0;
	*ppOutput = NULL;

	// Get a pointer to the Direct Draw object
	hr = DirectDrawCreate(NULL, &spDD, NULL);
	if(SUCCEEDED(hr))
	{
		CComPtr<IDirectDrawSurface>	spSurface;
		
		hr = spDD->SetCooperativeLevel(NULL, DDSCL_NORMAL);
		if(FAILED(hr))
			return 1;

		DDSURFACEDESC ddsd; 
		ddsd.dwSize = sizeof(ddsd); 
		 
		// Tell DirectDraw which members are valid. 
		ddsd.dwFlags = DDSD_CAPS; 
		 
		// Request a primary surface. 
		ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;

		// Get a pointer to the primary surface.
		hr = spDD->CreateSurface(&ddsd, &spSurface, NULL);
		if(SUCCEEDED(hr))
		{
			HDC	hDC;

			// Retrieve the primary surface's HDC
			hr = spSurface->GetDC(&hDC);
			if(SUCCEEDED(hr))
			{
				CDIBSection	dib;
				HDC			hCaptureDC	= CreateCompatibleDC(hDC);

				dib.CreateBitmap(nScreenX, nScreenY, 16);

				if(dib.GetSafeHandle() != NULL)
				{
					SelectObject(hCaptureDC, dib.GetSafeHandle());

					if(BitBlt(hCaptureDC, 0, 0, nScreenX, nScreenY, hDC, 0, 0, SRCCOPY))
					{
						DWORD		dwSize;
						BYTE*		pBuffer;

						dwSize	= sizeof(DIBINFO) + dib.GetImageSize();
						pBuffer	= (BYTE*)LocalAlloc(LPTR, dwSize);
						
						if(pBuffer)
						{
							memcpy(pBuffer, dib.GetBitmapInfo(), sizeof(DIBINFO));
							memcpy(pBuffer + sizeof(DIBINFO), dib.GetDIBits(), dib.GetImageSize());

							*pcbOutput	= dwSize;
							*ppOutput	= pBuffer;
						}
					}
				}

				DeleteDC(hCaptureDC);

				spSurface->ReleaseDC(hDC);
			}
		}
	}

	return 0;
}


#else	// _WIN32_WCE < 0x500


// CeScreenCapture
//
//		Captures the device screen into a bitmap and send the data back to the desktop.
//
CESCREENCAPTURE_API int CeScreenCapture(DWORD cbInput, BYTE* pInput, DWORD* pcbOutput, BYTE** ppOutput, LPVOID pReserved)
{
	CGC		gx;
	int		nScreenX	= GetSystemMetrics(SM_CXSCREEN),
			nScreenY	= GetSystemMetrics(SM_CYSCREEN);

	LocalFree(pInput);
	*pcbOutput = 0;
	*ppOutput = NULL;

	if(gx.Open(NULL))
	{
		if(gx.BeginDraw())
		{
			CDIBSection	dib;

			if(dib.CreateBitmap(nScreenX, nScreenY, (int)gx.GetBitsPerPixel()))
			{
				int		x, y;
				WORD*	pBits = (WORD*)dib.GetDIBits();

				for(y = nScreenY - 1; y >= 0; --y)
				{
					for(x = 0; x < nScreenX; ++x)
					{
						*pBits++ = gx.GetPixel(x, y);
					}
				}

				DWORD		dwSize;
				BYTE*		pBuffer;
				BITMAPINFO*	pInfo	= dib.GetBitmapInfo();

				pInfo->bmiHeader.biCompression	= BI_BITFIELDS;

				DWORD dw[3];
				if(gx.GetDisplayFormat() & kfDirect555)
				{
					dw[0] = 31744;		// RED bitmask   Bits: 0 11111 00000 00000
					dw[1] = 992;		// GREEN bitmask Bits: 0 00000 11111 00000
					dw[2] = 31;			// BLUE bitmask  Bits: 0 00000 00000 11111
				}
				else if(gx.GetDisplayFormat() & kfDirect565)
				{
					dw[0] = 0xF800;		// RED bitmask   Bits: 11111 000000 00000
					dw[1] = 0x7E0;		// GREEN bitmask Bits: 00000 111111 00000
					dw[2] = 0x1F;		// BLUE bitmask  Bits: 00000 000000 11111
				}

				CopyMemory(dib.GetColorTable(), dw, sizeof(DWORD) * 3);

				dwSize	= sizeof(DIBINFO) + dib.GetImageSize();
				pBuffer	= (BYTE*)LocalAlloc(LPTR, dwSize);
				
				if(pBuffer)
				{
					memcpy(pBuffer, dib.GetBitmapInfo(), sizeof(DIBINFO));
					memcpy(pBuffer + sizeof(DIBINFO), dib.GetDIBits(), dib.GetImageSize());

					*pcbOutput	= dwSize;
					*ppOutput	= pBuffer;
				}
			}
			
			gx.EndDraw();
		}

		gx.Close();
	}

	return 0;
}


#endif	// #if _WIN32_WCE >= 0x500

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -