Améliorez votre expérience CodeS-SourceS avec ce plugin:
http://codes-sources.commentcamarche.net/forum/affich-10000111-plugin-better-cs-2#cptpingu-signature
Améliorez votre expérience CodeS-SourceS avec ce plugin:
http://codes-sources.commentcamarche.net/forum/affich-10000111-plugin-better-cs-2#cptpingu-signature
Vous n’avez pas trouvé la réponse que vous recherchez ?
Posez votre questionVOID CopyFromScreen(HWND hWnd, LPTSTR strFileDest, int bmpWidth, int bmpHeight) { HDC hdcScreen = ::CreateDC("DISPLAY", NULL, NULL, NULL); HDC hdcCompatible = ::CreateCompatibleDC(hdcScreen); HBITMAP hbmScreen = ::CreateCompatibleBitmap(hdcScreen, ::GetDeviceCaps(hdcScreen, HORZRES), ::GetDeviceCaps(hdcScreen, VERTRES)); ::SelectObject(hdcCompatible, hbmScreen); // masque appli en cours ou non //::ShowWindow(hwnd, SW_HIDE); if(bmpWidth<=0) bmpWidth=::GetDeviceCaps(hdcScreen, HORZRES); if(bmpHeight<=0) bmpHeight = ::GetDeviceCaps(hdcScreen, VERTRES); ::BitBlt(hdcCompatible, 0,0, bmpWidth, bmpHeight, hdcScreen, 0,0, SRCCOPY); // reaffiche appli en cours ou non //::ShowWindow(hwnd, SW_SHOW); // Code pour créer le fichier BMP (trop long pour mettre ici) /* CreateBMPFile( strFileDest, CreateBitmapInfoStruct(hbmScreen), hbmScreen, hdcCompatible); */ ::ReleaseDC(hWnd,hdcCompatible); ::DeleteDC(hdcScreen); }
void CreateBMPFile(LPTSTR pszFile, PBITMAPINFO pbi, HBITMAP hBMP, HDC hDC) { HANDLE hf; BITMAPFILEHEADER hdr; PBITMAPINFOHEADER pbih; LPBYTE lpBits; DWORD dwTotal; DWORD cb; BYTE *hp; DWORD dwTmp; pbih = (PBITMAPINFOHEADER) pbi; lpBits = (LPBYTE) GlobalAlloc(GMEM_FIXED, pbih->biSizeImage); if (!lpBits) return; if (!GetDIBits(hDC, hBMP, 0, (WORD) pbih->biHeight, lpBits, pbi, DIB_RGB_COLORS)) { return; } hf = CreateFile(pszFile, GENERIC_READ | GENERIC_WRITE, (DWORD) 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, (HANDLE) NULL); if (hf == INVALID_HANDLE_VALUE) return; hdr.bfType = 0x4d42; // 0x42 = "B" 0x4d = "M" hdr.bfSize = (DWORD) (sizeof(BITMAPFILEHEADER) + pbih->biSize + pbih->biClrUsed * sizeof(RGBQUAD) + pbih->biSizeImage); hdr.bfReserved1 = 0; hdr.bfReserved2 = 0; hdr.bfOffBits = (DWORD) sizeof(BITMAPFILEHEADER) + pbih->biSize + pbih->biClrUsed * sizeof (RGBQUAD); if (!WriteFile(hf, (LPVOID) &hdr, sizeof(BITMAPFILEHEADER), (LPDWORD) &dwTmp, NULL)) { return; } if (!WriteFile(hf, (LPVOID) pbih, sizeof(BITMAPINFOHEADER) + pbih->biClrUsed * sizeof (RGBQUAD), (LPDWORD) &dwTmp, ( NULL))) return; dwTotal = cb = pbih->biSizeImage; hp = lpBits; if (!WriteFile(hf, (LPSTR) hp, (int) cb, (LPDWORD) &dwTmp,NULL)) return; if (!CloseHandle(hf)) return; GlobalFree((HGLOBAL)lpBits); } PBITMAPINFO CreateBitmapInfoStruct(HBITMAP hBmp) { BITMAP bmp; PBITMAPINFO pbmi; WORD cClrBits; if (!GetObject(hBmp, sizeof(BITMAP), &bmp)) return NULL; cClrBits = (WORD)(bmp.bmPlanes * bmp.bmBitsPixel); if (cClrBits == 1) cClrBits = 1; else if (cClrBits <= 4) cClrBits = 4; else if (cClrBits <= 8) cClrBits = 8; else if (cClrBits <= 16) cClrBits = 16; else if (cClrBits <= 24) cClrBits = 24; else cClrBits = 32; if (cClrBits != 24) pbmi = (PBITMAPINFO) LocalAlloc(LPTR, sizeof(BITMAPINFOHEADER) + sizeof(RGBQUAD) * (1<< cClrBits)); else pbmi = (PBITMAPINFO) LocalAlloc(LPTR, sizeof(BITMAPINFOHEADER)); pbmi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER); pbmi->bmiHeader.biWidth = bmp.bmWidth; pbmi->bmiHeader.biHeight = bmp.bmHeight; pbmi->bmiHeader.biPlanes = bmp.bmPlanes; pbmi->bmiHeader.biBitCount = bmp.bmBitsPixel; if (cClrBits < 24) pbmi->bmiHeader.biClrUsed = (1<<cClrBits); pbmi->bmiHeader.biCompression = BI_RGB; pbmi->bmiHeader.biSizeImage = ((pbmi->bmiHeader.biWidth * cClrBits +31) & ~31) /8 * pbmi->bmiHeader.biHeight; pbmi->bmiHeader.biClrImportant = 0; return pbmi; }
// init GDI+ #include <gdiplus.h> using namespace Gdiplus; #pragma comment(lib, "gdiplus.lib") // Dans main GdiplusStartupInput gdiplusStartupInput; ULONG_PTR gdiplusToken; GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL); // crée le screenShot Status status = CopyFromScreen(L"image/jpeg", L"monScreenShot.jpg", hWnd); // Fin du main, libère gdi+ GdiplusShutdown(gdiplusToken); Gdiplus::Status CopyFromScreen(const WCHAR* lpStrMimeType, const WCHAR* lpStrImageFileDest, HWND hwndHide) { if(hwndHide != NULL) ::ShowWindow(hwndHide, SW_HIDE); Gdiplus::Status status = Gdiplus::Status::GenericError; HDC hdcScreen = ::GetDC( 0 ); int wdScreen = ::GetDeviceCaps(hdcScreen, HORZRES); int hgscreen = ::GetDeviceCaps(hdcScreen, VERTRES); HDC memdc = ::CreateCompatibleDC(hdcScreen); HBITMAP membit = ::CreateCompatibleBitmap(hdcScreen, wdScreen, hgscreen); HBITMAP hOldBitmap =(HBITMAP)::SelectObject(memdc, membit); ::BitBlt(memdc, 0, 0, wdScreen, hgscreen, hdcScreen, 0, 0, SRCCOPY); Bitmap bm(membit,(HPALETTE)NULL); CLSID clsid; if( GetEncoderClsid(lpStrMimeType, &clsid) >= 0 ) status = bm.Save(lpStrImageFileDest, &clsid); ::SelectObject(memdc, hOldBitmap); ::DeleteObject(memdc); ::DeleteObject(membit); ::ReleaseDC(NULL,hdcScreen); if(hwndHide != NULL) ::ShowWindow(hwndHide, SW_SHOW); return status; } /* Récupère un Clsid */ int GetEncoderClsid(const WCHAR* format, CLSID* pClsid) { if( wcslen(format)==0 ) { *pClsid = GUID_NULL; return -1; } UINT num = 0; UINT size = 0; ImageCodecInfo* pImageCodecInfo = NULL; GetImageEncodersSize(&num, &size); if(size > 0 && num > 0) { pImageCodecInfo = (ImageCodecInfo*)(::GlobalAlloc(0, size)); if(pImageCodecInfo) { GetImageEncoders(num, size, pImageCodecInfo); for(UINT j = 0; j < num; ++j) { if( wcscmp(pImageCodecInfo[j].MimeType, format) == 0 ) { *pClsid = pImageCodecInfo[j].Clsid; ::GlobalFree(pImageCodecInfo); return j; } } if(pImageCodecInfo) ::GlobalFree(pImageCodecInfo); } } return -1; }