vc++的屏幕截图

来源:互联网 发布:赚钱的网络兼职 编辑:程序博客网 时间:2024/05/29 03:16

截图功能想起来真的不难,可是,我实际操作的时候,却发现真的不容易,网上讲了很多方法,但是,要么实现起来很复杂,要么就是库类在vc++不支持(其实网上资料很多,可能是我没学会)。花了好长时间,终于用gdi+搞定了。

vc本身不带有gdi+库,需要自己下载,我上传了一份。地址:
http://download.csdn.net/download/lsjweiyi/9727096

vc中还需要进行配置,具体配置方法百度吧。

#define ULONG_PTR ULONG#include  <iostream>   #include  <windows.h>#include  <Gdiplus.h>using   namespace   Gdiplus;using   namespace   std   ;//获取Encoder的CLSID的方法int GetEncoderClsid(const WCHAR* format, CLSID* pCLSID){ UINT num = 0; UINT size = 0; ImageCodecInfo* pImageCodecInfo = NULL;//把指针变为0 GetImageEncodersSize(&num, &size);//获取图片编码器大小 if(size == 0){    return -1;    } pImageCodecInfo = (ImageCodecInfo *)(malloc(size)); if(pImageCodecInfo == NULL){    return -1;    } GetImageEncoders(num, size, pImageCodecInfo); // Find for the support of format for image in the windows for(UINT i = 0; i < num; ++i) {  //MimeType: Depiction for the program image   if( wcscmp(pImageCodecInfo[i].MimeType, format) == 0)  {    *pCLSID = pImageCodecInfo[i].Clsid;   free(pImageCodecInfo);   return i;   }  }  free(pImageCodecInfo);  return -1;}HRESULT GetScreenSnap(){  ///////////////////////////////////////////  LPCWSTR     pszFileName = L"test.bmp";  /*  int     xs = 200;  int     ys = 200;  int     quality = 80;  */  int     quality = 80;  //得到屏幕设备HDC以及长宽        HDC hdc = GetWindowDC(NULL);  int x  = GetDeviceCaps(hdc,     HORZRES);        int y  = GetDeviceCaps(hdc,     VERTRES);  //创建与屏幕设备兼容的内存区域HDC  HDC hmemdc = ::CreateCompatibleDC(hdc);  //创建相关位图,把位图与内存区域HDC绑定  HBITMAP hbmp = ::CreateCompatibleBitmap(hdc,x,y),hold;        hold  = (HBITMAP)::SelectObject(hmemdc,hbmp);  //把屏幕设备传到内存区域(位图)中  BitBlt(hmemdc, 0, 0, x, y,hdc,0,0,SRCCOPY);    //添加鼠标到图片里   //POINT l_pt;   //::GetCursorPos(&l_pt);   //HCURSOR l_hCursor = ::GetCursor();   //::DrawIcon(hmemdc,l_pt.x,l_pt.y,l_hCursor);   //还原内存区域  SelectObject(hmemdc,hold); /*  //::ShowWindow(NULL,SW_SHOWMAXIMIZED);//SW_SHOWMAXIMIZED  //得到显示对话框的HDC  HDC hdc1 = GetDC(NULL);  //创建与对话框兼容的内存区域的DC  HDC hmemdc1 = ::CreateCompatibleDC(hdc1);   //把位图与对话框兼容内存绑定  hold  = (HBITMAP)::SelectObject(hmemdc1,hbmp);  //把对话框兼容内存的内容(位图)传至对话框  BitBlt(hdc1, 0, 0, x, y,hmemdc1,0,0,SRCCOPY);  //还原内存区域  SelectObject(hmemdc1,hold);  */  ::UpdateWindow(NULL);  {        //使用GDI+保存位图    Bitmap bit(hbmp,NULL);    printf ("%d",sizeof(bit));    printf ("\n");    CLSID encoderClsid;    //encoderClsid = (void*)(new char[10]);    EncoderParameters encoderParameters;          encoderParameters.Count = 1;          encoderParameters.Parameter[0].Guid = EncoderQuality;          encoderParameters.Parameter[0].Type = EncoderParameterValueTypeLong;          encoderParameters.Parameter[0].NumberOfValues = 1;          encoderParameters.Parameter[0].Value = &quality;          GetEncoderClsid(L"image/bmp",&encoderClsid);    bit.Save(pszFileName,&encoderClsid,&encoderParameters);   }         ::DeleteObject(hbmp);         ::DeleteObject(hmemdc);   //::DeleteObject(hmemdc1);   //::DeleteObject(hdc1);   ::DeleteObject(hdc);   return 0;   } int main(){    //初始化gdi+    GdiplusStartupInput gdiplusStartupInput;    ULONG_PTR gdiplusToken;    GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);    GetScreenSnap();    GdiplusShutdown(gdiplusToken); //关闭gdi+    return 0;}

放张效果图:
这里写图片描述

其实我还没完全弄懂整个代码,只知道是获取屏幕句柄,然后用位图保存。比如如何不保存直接把位图显示出来,不知道可不可以做到。

0 0