如何在Windows应用程序中截屏?

如何使用Win32截屏当前屏幕?



森栏
浏览 532回答 3
3回答

墨色风雨

// get the device context of the screenHDC hScreenDC = CreateDC("DISPLAY", NULL, NULL, NULL);     // and a device context to put it inHDC hMemoryDC = CreateCompatibleDC(hScreenDC);int width = GetDeviceCaps(hScreenDC, HORZRES);int height = GetDeviceCaps(hScreenDC, VERTRES);// maybe worth checking these are positive valuesHBITMAP hBitmap = CreateCompatibleBitmap(hScreenDC, width, height);// get a new bitmapHBITMAP hOldBitmap = (HBITMAP) SelectObject(hMemoryDC, hBitmap);BitBlt(hMemoryDC, 0, 0, width, height, hScreenDC, 0, 0, SRCCOPY);hBitmap = (HBITMAP) SelectObject(hMemoryDC, hOldBitmap);// clean upDeleteDC(hMemoryDC);DeleteDC(hScreenDC);// now your image is held in hBitmap. You can save it or do whatever with it

万千封印

void GetScreenShot(void){    int x1, y1, x2, y2, w, h;    // get screen dimensions    x1  = GetSystemMetrics(SM_XVIRTUALSCREEN);    y1  = GetSystemMetrics(SM_YVIRTUALSCREEN);    x2  = GetSystemMetrics(SM_CXVIRTUALSCREEN);    y2  = GetSystemMetrics(SM_CYVIRTUALSCREEN);    w   = x2 - x1;    h   = y2 - y1;    // copy screen to bitmap    HDC     hScreen = GetDC(NULL);    HDC     hDC     = CreateCompatibleDC(hScreen);    HBITMAP hBitmap = CreateCompatibleBitmap(hScreen, w, h);    HGDIOBJ old_obj = SelectObject(hDC, hBitmap);    BOOL    bRet    = BitBlt(hDC, 0, 0, w, h, hScreen, x1, y1, SRCCOPY);    // save bitmap to clipboard    OpenClipboard(NULL);    EmptyClipboard();    SetClipboardData(CF_BITMAP, hBitmap);    CloseClipboard();       // clean up    SelectObject(hDC, old_obj);    DeleteDC(hDC);    ReleaseDC(NULL, hScreen);    DeleteObject(hBitmap);}
打开App,查看更多内容
随时随地看视频慕课网APP