///////////////////////////////////////////////////////
// CaptureControl.h
// freefalcon 2004.12.10
#include <afxwin.h>
#ifndef __CAPTURE_CONTROL_H__
#define __CAPTURE_CONTROL_H__
#define CAPTURE_DEST_CLIPBOARD 0
#define CAPTURE_DEST_FILE 1
class CCaptureControl
{
public:
CCaptureControl()
{
m_hWnd = NULL;
m_nDest = CAPTURE_DEST_CLIPBOARD;
m_strFilePath = ".";
m_strFileName = "NoName";
m_nWidth = 0;
m_nHeight = 0;
}
virtual ~CCaptureControl()
{
}
HWND SetWindow(HWND hWnd = NULL)
{
HWND oldhWnd = m_hWnd;
m_hWnd = hWnd;
return oldhWnd;
}
void SetDestination(UINT nDest)
{
m_nDest = nDest;
}
void SetFilePath(const CString& strFilePath)
{
m_strFilePath = strFilePath;
}
void SetFileName(const CString& strFileName)
{
m_strFileName = strFileName;
}
int GetWidth() const
{
return m_nWidth;
}
int GetHeight() const
{
return m_nHeight;
}
BOOL Capture()
{
BOOL ret = TRUE;
CDC dc;
int nWidth;
int nHeight;
if(m_hWnd == NULL)
{
dc.CreateDC("DISPLAY",NULL,NULL,NULL);
nWidth = GetSystemMetrics(SM_CXSCREEN);
nHeight = GetSystemMetrics(SM_CYSCREEN);
}
else
{
CWnd* pWnd = CWnd::FromHandle(m_hWnd);
CDC* pDC = pWnd->GetDC();
dc.Attach(pDC->GetSafeHdc());
CRect rect;
pWnd->GetClientRect(&rect);
nWidth = rect.Width();
nHeight = rect.Height();
}
m_nWidth = nWidth;
m_nHeight = nHeight;
CDC dcMem;
dcMem.CreateCompatibleDC(&dc);
CBitmap bitmap;
bitmap.CreateCompatibleBitmap(&dc, nWidth, nHeight);
CBitmap *pOldBitmap = dcMem.SelectObject(&bitmap);
dcMem.BitBlt(0,0,nWidth,nHeight,&dc,0,0,SRCCOPY);
dcMem.SelectObject(pOldBitmap);
if(m_nDest == CAPTURE_DEST_CLIPBOARD)
{
ret = CopyBitmapToClipboard(bitmap);
}
else if(m_nDest == CAPTURE_DEST_FILE)
{
CString strFileFullPath = m_strFilePath+"\\"+m_strFileName + ".bmp";
ret = SaveBitmapToFile(dc.GetSafeHdc(), bitmap, strFileFullPath);
}
if(m_hWnd == NULL)
{
dc.DeleteDC();
}
else
{
dc.Detach();
}
return ret;
}
protected:
BOOL CopyBitmapToClipboard(const CBitmap& bitmap)
{
BOOL ret = FALSE;
if((::OpenClipboard(NULL) != 0) && (::EmptyClipboard() != 0))
{
if(::SetClipboardData(CF_BITMAP,bitmap.GetSafeHandle()) != NULL)
{
ret = TRUE;
}
::CloseClipboard();
}
return ret;
}
BOOL SaveBitmapToFile(HDC hDC, CBitmap& bitmap, LPCTSTR lpszFileName)
{
BOOL ret = TRUE;
BITMAP btm;
bitmap.GetBitmap(&btm);
DWORD size = btm.bmWidthBytes * btm.bmHeight;
HGLOBAL hMem = GlobalAlloc(GMEM_FIXED | GMEM_ZEROINIT,size);
if(hMem == NULL)
{
return FALSE;
}
LPSTR lpData = (LPSTR)GlobalLock(hMem);
/////////////////////////////////////////////
BITMAPINFOHEADER bih;
bih.biSize = sizeof(BITMAPINFOHEADER);
bih.biWidth = btm.bmWidth;
bih.biHeight = btm.bmHeight;
bih.biPlanes = 1;
bih.biBitCount = btm.bmBitsPixel;
bih.biCompression = 0;
bih.biSizeImage = size;
bih.biXPelsPerMeter = 0;
bih.biYPelsPerMeter = 0;
bih.biClrUsed = 0;
bih.biClrImportant = 0;
if(GetDIBits(hDC,bitmap,0,bih.biHeight,lpData,(BITMAPINFO*)&bih,DIB_RGB_COLORS) == 0)
{
GlobalFree(hMem);
return FALSE;
}
//bitmap.GetBitmapBits(size,lpData);//此函数在处理5-5-5模式的16位色下会出现颜色混乱
BITMAPFILEHEADER bfh;
bfh.bfType = ((WORD)('M'<< 8)|'B');
bfh.bfReserved1 = 0;
bfh.bfReserved2 = 0;
bfh.bfSize = 54+size;
bfh.bfOffBits = 54;
CFile bf;
if(bf.Open(lpszFileName, CFile::modeCreate|CFile::modeWrite))
{
bf.WriteHuge(&bfh,sizeof(BITMAPFILEHEADER));
bf.WriteHuge(&bih,sizeof(BITMAPINFOHEADER));
bf.WriteHuge(lpData,size);
bf.Close();
}
else
{
ret = FALSE;
}
GlobalFree(hMem);
return ret;
}
protected:
UINT m_nDest;
HWND m_hWnd;
CString m_strFilePath;
CString m_strFileName;
int m_nWidth;
int m_nHeight;
};
#endif//__CAPTURE_CONTROL_H__
int main()
{
CBitmap const bitmap;
CString const path="C:\\";
CString const fileName="ok.bmp";
CCaptureControl c;
c.SetFilePath(path);
c.SetFileName(fileName);
c.SetDestination(1);
c.Capture();
c.~CCaptureControl();
return 0;
}
说明:该类由csdn朋友freefalcon(心宇—小小菜鸟想高飞) 设计,我只是收藏
本文地址:http://com.8s8s.com/it/it25897.htm