怎样做网站的背景图片/天津seo外包
实现功能: 点击按钮 当前窗口图片将被保存下来
第一部:点击按钮的触发函数
void CDialogDlg::OnBnClickedButtonScreenshot() //截图
{// TODO: 在此添加控件通知处理程序代码bmpScreen Bmp;CRect rect;GetClientRect(&rect);ClientToScreen(&rect);//P:得到客户区转化为相对屏幕的坐标系确定一个文件保存位置CString sPath;GetModuleFileName(NULL,sPath.GetBufferSetLength(MAX_PATH+1),MAX_PATH);//得到程序的路径sPath.ReleaseBuffer();int nPos=sPath.ReverseFind('\\');m_StrExePath=sPath.Left(nPos);sPath = m_StrExePath;m_StrDBPath = sPath + _T("\\SavePicture\\ScreenPic.bmp"); //得到程序目录下的数据库的完整路径//Bmp.screenShot(rect,0,0,"ScreenPic.bmp");USES_CONVERSION;char* p =T2A(m_StrDBPath); 主要实现函数Bmp.screenShot(rect,0,0,p); ///第二第三个参数没用
}
第二部:截图函数screenShot的实现:
主要文件:bmpScreen.h bmpScreen.cpp
//***********************************************
//*******************截图操作汇总***************/
//************************************************/
/*1. CDC dc;dc.CreateDC(_T("DISPLAY"), NULL, NULL, NULL); //创建dc2. memDC.CreateCompatibleDC(&dc); //创建与当前DC兼容的内存DC 3. memBitmap.CreateCompatibleBitmap(&dc, rect.Width(),rect.Height()); //创建一块指定大小的位图 4. oldmemBitmap = memDC.SelectObject(&memBitmap); // 将该位图选入到内存DC中 memDC5. memDC.BitBlt(0, 0, rect.Width(),rect.Height(), &dc,left, top, SRCCOPY); // 将图形dc拷贝到一个目标memDC中最后进行 BMP文件存取(BMP图像文件被分成4个部分:位图文件头(Bitmap File Header)、位图信息头(Bitmap Info Header)、颜色表(Color Map)和位图数据(即图像数据,Data Bits或Data Body)。)
*/void bmpScreen::screenShot(CRect rect,int left,int top,char *name){CBitmap* m_pBitmap; // 加入类成员//CFrameWnd* pMainFrame = (CFrameWnd*)AfxGetMainWnd(); // 获得截图窗口的指针,默认为主窗口,可以更改为其他的窗口。// CPaintDC dc(pMainFrame); //为屏幕创建设备描述表CDC dc;dc.CreateDC(_T("DISPLAY"), NULL, NULL, NULL);m_pBitmap=new CBitmap; m_pBitmap->CreateCompatibleBitmap(&dc,rect.Width(),rect.Height()); CDC memDC; memDC.CreateCompatibleDC(&dc); CBitmap memBitmap, *oldmemBitmap; // 建立和屏幕兼容的bitmapmemBitmap.CreateCompatibleBitmap(&dc, rect.Width(),rect.Height());oldmemBitmap = memDC.SelectObject(&memBitmap);//将memBitmap选入内存DCmemDC.BitBlt(0, 0, rect.Width(),rect.Height(), &dc,rect.left, rect.top, SRCCOPY); //P:rect.left, rect.top原位图相对屏幕的坐标//memDC.BitBlt(0, 0, rect.Width(),rect.Height(), &dc,0, 0, SRCCOPY); BITMAP bmp;memBitmap.GetBitmap(&bmp); // 获得位图信息 ///P:得到创建的位图memBitmap的信息到bmp中FILE *fp = fopen(name, "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, rect.Height(), p, (LPBITMAPINFO) &bih, DIB_RGB_COLORS); //获取位图数据fwrite(p, 1, bmp.bmWidthBytes * bmp.bmHeight, fp); //写入位图数据delete [] p; fclose(fp);memDC.SelectObject(oldmemBitmap);memDC.DeleteDC();}
下载地址:http://download.csdn.net/detail/qq_17242957/9109785