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

📄 getscreentobitmap.cpp

📁 简单的远程控制工具,分为服务器与客户斋,让你了解socket编程的知识.
💻 CPP
字号:
// GetScreenToBitmap.cpp: implementation of the CGetScreenToBitmap class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "ctrl_srvr9.h"
#include "GetScreenToBitmap.h"
//#include "jpeglib.H"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CGetScreenToBitmap::CGetScreenToBitmap()
{
	m_lpBmpData = NULL;
	m_dwBmpSize = 0;
	m_dwBmpInfoSize = 0;
}

CGetScreenToBitmap::~CGetScreenToBitmap()
{
   ResetVariable();
}


void CGetScreenToBitmap::ResetVariable()
{
/*    if(m_lpBmpData)
	   GlobalUnlock(m_hBmpData);
    if(m_hBmpData)
       GlobalFree(m_hBmpData);
*/
//	if(m_lpBmpData) delete m_lpBmpData;
    if(m_lpBmpData) GlobalFree(m_lpBmpData);
	m_lpBmpData = NULL;
    m_dwBmpSize = 0;
    m_dwBmpInfoSize = 0;
}



LPSTR FAR CGetScreenToBitmap::GetImage()
{
	 return m_lpBmpData;
}

DWORD CGetScreenToBitmap::GetImageSize()
{
	 return m_dwBmpSize;
}



//DEL BOOL CGetScreenToBitmap::GetScreen2Bmp(CRect rcArea,int nBits, int nArea)
//DEL {
//DEL /*	CDC dc;
//DEL 	CDC *pdc;
//DEL 	dc.CreateDC("DISPLAY",NULL,NULL,NULL);
//DEL 	CBitmap bm;
//DEL 	int Width=GetSystemMetrics(SM_CXSCREEN);
//DEL 	int Height=GetSystemMetrics(SM_CYSCREEN);
//DEL 	bm.CreateCompatibleBitmap(&dc,Width,Height);
//DEL 
//DEL 
//DEL 	CDC tdc;
//DEL 	tdc.CreateCompatibleDC(&dc);
//DEL 	CBitmap*pOld=tdc.SelectObject(&bm);
//DEL 	tdc.BitBlt(0,0,Width,Height,&dc,0,0,SRCCOPY);
//DEL 	tdc.SelectObject(pOld);
//DEL 	BITMAP btm;
//DEL 	bm.GetBitmap(&btm);
//DEL 	DWORD size=btm.bmWidthBytes*btm.bmHeight;
//DEL 	LPSTR lpData=(LPSTR)GlobalAllocPtr(GPTR,size);
//DEL /////////////////////////////////////////////
//DEL 	BITMAPINFOHEADER bih;
//DEL 	bih.biBitCount=btm.bmBitsPixel;
//DEL 	bih.biClrImportant=0;
//DEL 	bih.biClrUsed=0;
//DEL 	bih.biCompression=0;
//DEL 	bih.biHeight=btm.bmHeight;
//DEL 	bih.biPlanes=1;
//DEL 	bih.biSize=sizeof(BITMAPINFOHEADER);
//DEL 	bih.biSizeImage=size;
//DEL 	bih.biWidth=btm.bmWidth;
//DEL 	bih.biXPelsPerMeter=0;
//DEL 	bih.biYPelsPerMeter=0;
//DEL ///////////////////////////////////
//DEL 	GetDIBits(dc,bm,0,bih.biHeight,lpData,(BITMAPINFO*)&bih,DIB_RGB_COLORS);
//DEL 	//bm.GetBitmapBits(size,lpData);	//此函数在处理5-5-5模式的16位色下会出现颜色混乱
//DEL //////////////////////////////
//DEL 	static int filecount=0;
//DEL 	CString name;
//DEL 	name.Format("pict%04d.bmp",filecount++);
//DEL 	name=m_Path+name;
//DEL 	BITMAPFILEHEADER bfh;
//DEL 	bfh.bfReserved1=bfh.bfReserved2=0;
//DEL 	bfh.bfType=((WORD)('M'<< 8)|'B');
//DEL 	bfh.bfSize=54+size;
//DEL 	bfh.bfOffBits=54;
//DEL 	CFile bf;
//DEL 	if(bf.Open(name,CFile::modeCreate|CFile::modeWrite)){
//DEL 		bf.WriteHuge(&bfh,sizeof(BITMAPFILEHEADER));
//DEL 		bf.WriteHuge(&bih,sizeof(BITMAPINFOHEADER));
//DEL 		bf.WriteHuge(lpData,size);
//DEL 		bf.Close();
//DEL 		nCount++;
//DEL 	}
//DEL 	GlobalFreePtr(lpData);
//DEL 	if(nCount==1)
//DEL 		m_Number.Format("%d picture captured.",nCount);
//DEL 	else
//DEL 		m_Number.Format("%d pictures captured.",nCount);
//DEL 	UpdateData(FALSE);
//DEL */
//DEL 	return 1;
//DEL }

BOOL CGetScreenToBitmap::GetScreen(CRect rcArea,int nBits, int nArea)
{
	HDC		hScrDC, hMemDC;			// 屏幕和内存设备描述表
	HBITMAP		hBitmap, hOldBitmap;		// 位图句柄
//	int		xScrn, yScrn;			// 屏幕分辨率 

	HDC		hDC;				//设备描述表
	int		iBits;				//当前显示分辨率下每个像素所占字节数
	WORD		wBitCount;			//位图中每个像素所占字节数

	//定义调色板大小, 位图中像素字节大小 ,位图文件大小 , 写入文件字节数
	DWORD		dwPaletteSize=0, dwBmBitsSize;
	BITMAP		Bitmap;			//位图属性结构
	BITMAPINFOHEADER	bi;		//位图信息头结构 
	LPBITMAPINFOHEADER	lpbi;		//指向位图信息头结构

	//定义文件,分配内存句柄,调色板句柄  
//	HANDLE			hDib;
	HPALETTE		hPal,hOldPal=NULL;
    //释放原有资源
	ResetVariable();
	// 获得屏幕分辨率
	SetArea(rcArea, nArea); //取图象区域
	//为屏幕创建设备描述表
	hScrDC = CreateDC("DISPLAY", NULL, NULL, NULL);
/*过时
	xScrn = GetDeviceCaps(hScrDC, HORZRES);
	yScrn = GetDeviceCaps(hScrDC, VERTRES);
	//确保选定区域是可见的
	if (rcArea.left < 0)
		rcArea.left = 0;
	if (rcArea.top < 0)
		rcArea.top = 0;
	if (rcArea.right > xScrn)
		rcArea.right = xScrn;
	if (rcArea.bottom > yScrn)
		rcArea.bottom = yScrn;
*/
	//为屏幕设备描述表创建兼容的内存设备描述表
	hMemDC = CreateCompatibleDC(hScrDC);
	// 创建一个与屏幕设备描述表兼容的位图
	hBitmap = CreateCompatibleBitmap (hScrDC, 
		      m_rcArea.Width(), m_rcArea.Height());
	// 把新位图选到内存设备描述表中
	hOldBitmap = (HBITMAP) SelectObject(hMemDC, 
		          hBitmap);
	// 把屏幕设备描述表拷贝到内存设备描述表中
	BitBlt(hMemDC, 0, 0, m_rcArea.Width(), 
		   m_rcArea.Height(), hScrDC, 
		   m_rcArea.left, m_rcArea.top, SRCCOPY);
	//得到屏幕位图的句柄
	hBitmap = (HBITMAP) SelectObject(hMemDC, hOldBitmap);


	//计算位图每个像素所占字节数
	iBits = GetDeviceCaps(hScrDC, BITSPIXEL);
	iBits = iBits * GetDeviceCaps(hScrDC, PLANES);
	if (iBits <= 1)
		wBitCount = 1;
	else if (iBits <= 4)
		wBitCount = 4;
	else if (iBits <= 8)
		wBitCount = 8;
	else// if (iBits <= 24)
		wBitCount = 24;

	if (wBitCount > nBits)
		wBitCount = nBits;

	//计算调色板大小
	if (wBitCount <= 8)
		dwPaletteSize = (1 << wBitCount) * sizeof(RGBQUAD);

	//设置位图信息头结构
	GetObject(hBitmap, sizeof(BITMAP), (LPSTR)&Bitmap);
	bi.biSize = sizeof(BITMAPINFOHEADER);
	bi.biWidth = Bitmap.bmWidth;
	bi.biHeight = Bitmap.bmHeight;
	bi.biPlanes = 1;
	bi.biBitCount = wBitCount;
	bi.biCompression = BI_RGB;
	bi.biSizeImage = 0;
	bi.biXPelsPerMeter = 0;
	bi.biYPelsPerMeter = 0;
	bi.biClrUsed = dwPaletteSize/sizeof(RGBQUAD);
	bi.biClrImportant = 0;

	dwBmBitsSize = ((Bitmap.bmWidth * wBitCount+31)/32)* 4 *Bitmap.bmHeight ;
	m_dwBmpInfoSize = dwPaletteSize + sizeof(BITMAPINFOHEADER);
	m_dwBmpSize = dwBmBitsSize + dwPaletteSize + sizeof(BITMAPINFOHEADER);
	
	//为位图内容分配内存
/*	m_hBmpData  = GlobalAlloc(GHND,m_dwBmpSize);
	if (m_hBmpData == NULL) goto Exit01;
	m_lpBmpData = (char *)GlobalLock(m_hBmpData);
	if (m_lpBmpData == NULL) goto Exit01;
*/
//	m_lpBmpData = (LPSTR)new BYTE[m_dwBmpSize];
	m_lpBmpData  = (LPSTR)GlobalAlloc(GMEM_FIXED,m_dwBmpSize);
	if (m_lpBmpData == NULL) goto Exit01;

    lpbi = (LPBITMAPINFOHEADER)m_lpBmpData;
	*lpbi = bi;
	// 处理调色板   
	hPal = (HPALETTE) GetStockObject(DEFAULT_PALETTE);
	if (hPal)
	{
		hDC = ::GetDC (NULL);
		hOldPal = ::SelectPalette(hDC, hPal, FALSE);
		RealizePalette(hDC);
	}
	// 获取该调色板下新的像素值
	::GetDIBits(
			hDC,
			hBitmap,
			0, 
			(UINT) Bitmap.bmHeight,
			(LPSTR)lpbi + sizeof(BITMAPINFOHEADER) + dwPaletteSize,
			//(BITMAPINFOHEADER *)lpbi,
			(BITMAPINFO *)lpbi,
			DIB_RGB_COLORS
		  );
	//恢复调色板
	if (hOldPal)
	{
		SelectPalette(hDC, hOldPal, TRUE);
		RealizePalette(hDC);
		::ReleaseDC(NULL, hDC);
	}
//DEL	m_lpBmpData = (char *)lpbi;
	//清除
	DeleteObject (hBitmap);
	DeleteObject (hOldBitmap);
	DeleteDC(hScrDC);
	DeleteDC(hMemDC);
	return TRUE;

Exit01:
	ResetVariable();
	DeleteObject (hBitmap);
	DeleteDC(hScrDC);
	DeleteDC(hMemDC);
	return FALSE;
}

void CGetScreenToBitmap::SetArea(CRect rcArea, int nArea)
{
	//为屏幕创建设备描述表
	HWND hFocus;
	int		xScrn, yScrn;			// 屏幕分辨率 
	HDC hScrDC = CreateDC("DISPLAY", NULL, NULL, NULL);
	// 获得屏幕分辨率

	xScrn = GetDeviceCaps(hScrDC, HORZRES);
	yScrn = GetDeviceCaps(hScrDC, VERTRES);
	DeleteDC(hScrDC);
    m_rcArea.left = 0;
    m_rcArea.top = 0;
    m_rcArea.right = xScrn;
    m_rcArea.bottom = yScrn;
    if (nArea == 1 &&  //1)当前窗口2)指定区域0)全屏
        (hFocus = ::GetForegroundWindow()) != NULL)
	{
	    GetWindowRect(hFocus, &m_rcArea);
	}
	else if(nArea == 2)
	{
    m_rcArea =  rcArea;
	}
	if (m_rcArea.left < 0)
		m_rcArea.left = 0;
	if (m_rcArea.top < 0)
		m_rcArea.top = 0;
	if (m_rcArea.right > xScrn)
		m_rcArea.right = xScrn;
	if (m_rcArea.bottom > yScrn)
		m_rcArea.bottom = yScrn;

	//确保选定区域是可见的
}

⌨️ 快捷键说明

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