MFC截图功能(全屏)
2016-04-15 15:11
393 查看
按钮响应函数如下:
CTime tCurTime = CTime::GetCurrentTime();//当前日期时间;
CString strTime;
strTime = tCurTime.Format("%Y%m%d_%H%M%S");//将CTime转成CString;
CFileDialog Dlg(FALSE,NULL,strTime,OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,"excel文件(*.jpg)");
if(Dlg.DoModal() == IDOK)
{
m_str_save_path = Dlg.GetPathName();
m_str_save_path=m_str_save_path+".jpg";
}
CDC *pDC=GetDesktopWindow()->GetDC();//屏幕DC
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);//获得位图信息
//m_str_save_path="c:\\1.jpg";
FILE *fp = fopen(m_str_save_path, "w+b");
BITMAPINFOHEADER bih = {0};//位图信息头
bih.biBitCount = 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;
fwrite(&bfh, 1, sizeof(BITMAPFILEHEADER), fp);//写入位图文件头
fwrite(&bih, 1, sizeof(BITMAPINFOHEADER), fp);//写入位图信息头
byte * p = new byte[bmp.bmWidthBytes * bmp.bmHeight];//申请内存保存位图数据
GetDIBits(memDC.m_hDC, (HBITMAP) memBitmap.m_hObject, 0, Height, p,
(LPBITMAPINFO) &bih, DIB_RGB_COLORS);//获取位图数据
fwrite(p, 1, bmp.bmWidthBytes * bmp.bmHeight, fp);//写入位图数据
delete [] p;
fclose(fp);
memDC.SelectObject(oldmemBitmap);
memDC.DeleteDC();
ReleaseDC(pDC);
// if(this->m_bOpenMspaint)
::ShellExecute(NULL,"open","mspaint.exe",/*调用画图程序*/
m_str_save_path,
NULL,
SW_SHOWMAXIMIZED);
CTime tCurTime = CTime::GetCurrentTime();//当前日期时间;
CString strTime;
strTime = tCurTime.Format("%Y%m%d_%H%M%S");//将CTime转成CString;
CFileDialog Dlg(FALSE,NULL,strTime,OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,"excel文件(*.jpg)");
if(Dlg.DoModal() == IDOK)
{
m_str_save_path = Dlg.GetPathName();
m_str_save_path=m_str_save_path+".jpg";
}
CDC *pDC=GetDesktopWindow()->GetDC();//屏幕DC
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);//获得位图信息
//m_str_save_path="c:\\1.jpg";
FILE *fp = fopen(m_str_save_path, "w+b");
BITMAPINFOHEADER bih = {0};//位图信息头
bih.biBitCount = 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;
fwrite(&bfh, 1, sizeof(BITMAPFILEHEADER), fp);//写入位图文件头
fwrite(&bih, 1, sizeof(BITMAPINFOHEADER), fp);//写入位图信息头
byte * p = new byte[bmp.bmWidthBytes * bmp.bmHeight];//申请内存保存位图数据
GetDIBits(memDC.m_hDC, (HBITMAP) memBitmap.m_hObject, 0, Height, p,
(LPBITMAPINFO) &bih, DIB_RGB_COLORS);//获取位图数据
fwrite(p, 1, bmp.bmWidthBytes * bmp.bmHeight, fp);//写入位图数据
delete [] p;
fclose(fp);
memDC.SelectObject(oldmemBitmap);
memDC.DeleteDC();
ReleaseDC(pDC);
// if(this->m_bOpenMspaint)
::ShellExecute(NULL,"open","mspaint.exe",/*调用画图程序*/
m_str_save_path,
NULL,
SW_SHOWMAXIMIZED);
相关文章推荐
- zookeeper集群环境搭建(centos)
- FFMPEG录制视频
- NoClassDefFoundError解决
- js立即调用函数表达式
- 【原创】记一次HttpWebRequest中国移动查账单爬虫的攻克历程
- iOS中偏好设置的创建,数据写入与读取
- db2 卸载和安装
- Linux内核学习笔记三——进程调度
- el 表达式的基本用法
- 新手教程!Lynda的读书笔记之视觉设计师该怎样入门?
- share
- 递归与尾递归
- IOS开发之深拷贝与浅拷贝(mutableCopy与Copy)详解
- 程序开发中版本管理之命名规则及格式
- 在AndroidStudio中设置某项目支持使用Java 8 新特性
- 短信的广播接收者
- c#弱事件(weak event)
- UIStoryboard类介绍(如何从Storyboard中加载View Controller)
- MySQL Replication的Reset slave重置命令
- 图像的轮廓检测cvFindContours