截图10
HBITMAP CopyScreenToBitmap(int x1, int x2, int y1, int y2)
{
//屏幕和内存设备描述表
HDC hSrcDC, hMemDC;
//位图句柄
HBITMAP hBitmap, hOldBitmap;
//选定区域坐标
int nX, nY, nX2, nY2;
//位图宽度和高度
int nWidth, nHeight;
//屏幕分辨率
int xScrn, yScrn;
/*
//确保选定区域不为空矩形
if(IsRectEmpty(lpRect))
return NULL;*/
//为屏幕创建设备描述表
hSrcDC = CreateDC("DISPLAY", NULL, NULL, NULL);
//为屏幕设备描述表创建兼容的内存设备描述表
hMemDC = CreateCompatibleDC(hSrcDC);
//获得选定区域坐标
nX = x1;
nY = y1;
nX2 = x2;
nY2 = y2;
//获得屏幕分辨率
xScrn = GetDeviceCaps(hSrcDC, HORZRES);
yScrn = GetDeviceCaps(hSrcDC, VERTRES);
//确保选定区域是可见的
if(nX<0)
nX = 0;
if(nY<0)
nY = 0;
if(nX2>xScrn)
nX2 = xScrn;
if(nY2>yScrn)
nY2 = yScrn;
nWidth = nX2 - nX;
nHeight = nY2 - nY;
//创建一个与屏幕设备描述表兼容的位图
hBitmap = CreateCompatibleBitmap(hSrcDC, nWidth, nHeight);
//把新位图选到内存设备描述表中
hOldBitmap = (HBITMAP)SelectObject(hMemDC, hBitmap);
//把屏幕设备描述表拷贝到内存设备描述表中
BitBlt(hMemDC, 0, 0, nWidth, nHeight, hSrcDC, nX, nY, SRCCOPY);
//得到屏幕位图的句柄
hBitmap = (HBITMAP)SelectObject(hMemDC, hOldBitmap);
//清除
DeleteDC(hSrcDC);
DeleteDC(hMemDC);
//返回位置句柄
return hBitmap;
}
Void CaptureScreen()
{
int nScreenWidth = GetSystemMetrics(SM_CXSCREEN);
int nScreenHeight = GetSystemMetrics(SM_CYSCREEN);
HWND hDesktopWnd = GetDesktopWindow();
HDC hDesktopDC = GetDC(hDesktopWnd);
HDC hCaptureDC = CreateCompatibleDC(hDesktopDC);
HBITMAP hCaptureBitmap =CreateCompatibleBitmap(hDesktopDC,
nScreenWidth, nScreenHeight);
SelectObject(hCaptureDC,hCaptureBitmap);
BitBlt(hCaptureDC,0,0,nScreenWidth,nScreenHeight,
hDesktopDC,0,0,SRCCOPY|CAPTUREBLT);
SaveCapturedBitmap(hCaptureBitmap); //Place holder - Put your code
//here to save the captured image to disk
ReleaseDC(hDesktopWnd,hDesktopDC);
DeleteDC(hCaptureDC);
DeleteObject(hCaptureBitmap);
}
void CCaptureDlg::Screen(CString fileName)
{
CDC *pDC;//屏幕DC
pDC = this->GetDesktopWindow()->GetDC();
int BitPerPixel = pDC->GetDeviceCaps(BITSPIXEL);//获得颜色模式
int Width = pDC->GetDeviceCaps(HORZRES);
int Height = pDC->GetDeviceCaps(VERTRES);
CDC memDC;//内存DC
memDC.CreateCompatibleDC(pDC);
CBitmap memBitmap, *oldmemBitmap;//建立和屏幕兼容的bitmap
memBitmap.CreateCompatibleBitmap(pDC, Width, Height);
oldmemBitmap = memDC.SelectObject(&memBitmap);//将memBitmap选入内存DC
memDC.BitBlt(0, 0, Width, Height, pDC, 0, 0, SRCCOPY);//复制屏幕图像到内存DC
//以下代码保存memDC中的位图到文件
BITMAP bmp;
memBitmap.GetBitmap(&bmp);//获得位图信息
CFile file;
if(!file.Open(fileName,CFile::modeCreate|CFile::modeWrite))
{
#ifdef _DENUG
AfxMessageBox("打开文件错误");
#endif
return ;
}
BITMAPINFOHEADER bih = {0};//位图信息头
bih.biBitCount =24;// bmp.bmBitsPixel;//每个像素字节大小
bih.biCompression = BI_RGB;
bih.biHeight = bmp.bmHeight;//高度
bih.biPlanes = 1;
bih.biSize = sizeof(BITMAPINFOHEADER);
bih.biSizeImage = bmp.bmWidthBytes * bmp.bmHeight;//图像数据大小
bih.biWidth = bmp.bmWidth;//宽度
BITMAPFILEHEADER bfh = {0};//位图文件头
bfh.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);//到位图数据的偏移量
bfh.bfSize = bfh.bfOffBits + bmp.bmWidthBytes * bmp.bmHeight;//文件总的大小
bfh.bfType = (WORD)0x4d42;
file.Write(&bfh,sizeof(BITMAPFILEHEADER));//写入位图文件头
file.Write(&bih,sizeof(BITMAPINFOHEADER));//写入位图信息头
byte * p = new byte[bmp.bmWidthBytes * bmp.bmHeight];//申请内存保存位图数据
GetDIBits(memDC.m_hDC, (HBITMAP) memBitmap.m_hObject, 0, Height, p,
(LPBITMAPINFO) &bih, DIB_RGB_COLORS);//获取位图数据
file.WriteHuge(p,bmp.bmWidthBytes * bmp.bmHeight);
file.Close();
delete [] p;
memDC.SelectObject(oldmemBitmap);
}void CCaptureDlg::Screen(CString fileName)
{
CDC *pDC;//屏幕DC
pDC = this->GetDesktopWindow()->GetDC();
int BitPerPixel = pDC->GetDeviceCaps(BITSPIXEL);//获得颜色模式
int Width = pDC->GetDeviceCaps(HORZRES);
int Height = pDC->GetDeviceCaps(VERTRES);
CDC memDC;//内存DC
memDC.CreateCompatibleDC(pDC);
CBitmap memBitmap, *oldmemBitmap;//建立和屏幕兼容的bitmap
memBitmap.CreateCompatibleBitmap(pDC, Width, Height);
oldmemBitmap = memDC.SelectObject(&memBitmap);//将memBitmap选入内存DC
memDC.BitBlt(0, 0, Width, Height, pDC, 0, 0, SRCCOPY);//复制屏幕图像到内存DC
//以下代码保存memDC中的位图到文件
BITMAP bmp;
memBitmap.GetBitmap(&bmp);//获得位图信息
CFile file;
if(!file.Open(fileName,CFile::modeCreate|CFile::modeWrite))
{
#ifdef _DENUG
AfxMessageBox("打开文件错误");
#endif
return ;
}
BITMAPINFOHEADER bih = {0};//位图信息头
bih.biBitCount =24;// bmp.bmBitsPixel;//每个像素字节大小
bih.biCompression = BI_RGB;
bih.biHeight = bmp.bmHeight;//高度
bih.biPlanes = 1;
bih.biSize = sizeof(BITMAPINFOHEADER);
bih.biSizeImage = bmp.bmWidthBytes * bmp.bmHeight;//图像数据大小
bih.biWidth = bmp.bmWidth;//宽度
BITMAPFILEHEADER bfh = {0};//位图文件头
bfh.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);//到位图数据的偏移量
bfh.bfSize = bfh.bfOffBits + bmp.bmWidthBytes * bmp.bmHeight;//文件总的大小
bfh.bfType = (WORD)0x4d42;
file.Write(&bfh,sizeof(BITMAPFILEHEADER));//写入位图文件头
file.Write(&bih,sizeof(BITMAPINFOHEADER));//写入位图信息头
byte * p = new byte[bmp.bmWidthBytes * bmp.bmHeight];//申请内存保存位图数据
GetDIBits(memDC.m_hDC, (HBITMAP) memBitmap.m_hObject, 0, Height, p,
(LPBITMAPINFO) &bih, DIB_RGB_COLORS);//获取位图数据
file.WriteHuge(p,bmp.bmWidthBytes * bmp.bmHeight);
file.Close();
delete [] p;
memDC.SelectObject(oldmemBitmap);
}void CCaptureDlg::Screen(CString fileName)
{
CDC *pDC;//屏幕DC
pDC = this->GetDesktopWindow()->GetDC();
int BitPerPixel = pDC->GetDeviceCaps(BITSPIXEL);//获得颜色模式
int Width = pDC->GetDeviceCaps(HORZRES);
int Height = pDC->GetDeviceCaps(VERTRES);
CDC memDC;//内存DC
memDC.CreateCompatibleDC(pDC);
CBitmap memBitmap, *oldmemBitmap;//建立和屏幕兼容的bitmap
memBitmap.CreateCompatibleBitmap(pDC, Width, Height);
oldmemBitmap = memDC.SelectObject(&memBitmap);//将memBitmap选入内存DC
memDC.BitBlt(0, 0, Width, Height, pDC, 0, 0, SRCCOPY);//复制屏幕图像到内存DC
//以下代码保存memDC中的位图到文件
BITMAP bmp;
memBitmap.GetBitmap(&bmp);//获得位图信息
CFile file;
if(!file.Open(fileName,CFile::modeCreate|CFile::modeWrite))
{
#ifdef _DENUG
AfxMessageBox("打开文件错误");
#endif
return ;
}
BITMAPINFOHEADER bih = {0};//位图信息头
bih.biBitCount =24;// bmp.bmBitsPixel;//每个像素字节大小
bih.biCompression = BI_RGB;
bih.biHeight = bmp.bmHeight;//高度
bih.biPlanes = 1;
bih.biSize = sizeof(BITMAPINFOHEADER);
bih.biSizeImage = bmp.bmWidthBytes * bmp.bmHeight;//图像数据大小
bih.biWidth = bmp.bmWidth;//宽度
BITMAPFILEHEADER bfh = {0};//位图文件头
bfh.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);//到位图数据的偏移量
bfh.bfSize = bfh.bfOffBits + bmp.bmWidthBytes * bmp.bmHeight;//文件总的大小
bfh.bfType = (WORD)0x4d42;
file.Write(&bfh,sizeof(BITMAPFILEHEADER));//写入位图文件头
file.Write(&bih,sizeof(BITMAPINFOHEADER));//写入位图信息头
byte * p = new byte[bmp.bmWidthBytes * bmp.bmHeight];//申请内存保存位图数据
GetDIBits(memDC.m_hDC, (HBITMAP) memBitmap.m_hObject, 0, Height, p,
(LPBITMAPINFO) &bih, DIB_RGB_COLORS);//获取位图数据
file.WriteHuge(p,bmp.bmWidthBytes * bmp.bmHeight);
file.Close();
delete [] p;
memDC.SelectObject(oldmemBitmap);
}
CPaintDC dc(this);
CWnd* pWnd=GetDlgItem(IDC_WENBEN);
CDC* pDC=pWnd->GetDC();
pWnd->Invalidate();
pWnd->UpdateWindow();
pDC->Rectangle(0,0,100,100);
//创建画笔对象**************画X和Y轴
CPen* pPenRed=new CPen;
pPenRed->CreatePen(PS_SOLID,1,RGB(255,0,0));
CGdiObject* pOldPen=pDC->SelectObject(pPenRed);
pDC->MoveTo(10,10);
pDC->LineTo(10,280);
pDC->LineTo(280,280);
//创 画笔对象*****************画X轴和Y轴箭头
CPen* pPenBlue=new CPen;
pPenBlue->CreatePen(PS_SOLID,1,RGB(0,0,255));
pDC->SelectObject(pPenBlue); pDC->MoveTo(5,15);
pDC->LineTo(10,10);
pDC->LineTo(15,15);
pDC->MoveTo(275,275);
pDC->LineTo(280,280);
pDC->LineTo(275,285);
//写X轴刻值
CString str;
str.Format("0");
pDC->SetTextColor(RGB(0,255,0));
pDC->TextOut(10,283,str); str.Format("50");
pDC->TextOut(60,283,str); str.Format("100");
pDC->TextOut(110,283,str); str.Format("150");
pDC->TextOut(160,283,str); str.Format("200");
pDC->TextOut(210,283,str); str.Format("250");
pDC->TextOut(260,283,str); //写X轴刻度线
for(int i=0;i<256;i+=5)
{
if((i&1)==0)
{
//10的倍数
pDC->MoveTo(i+10,280);
pDC->LineTo(i+10,284); }
else
{
pDC->MoveTo(i+10,280);
pDC->LineTo(i+10,282);
}
} pDC->SelectObject(pOldPen);
// 除新的画笔
delete pPenRed;
delete pPenBlue;