Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/visual-studio-code/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在c+中打开进程并捕获其区域的屏幕+;在Windows上使用Qt 如何在Windows上C++中打开进程并将其区域的屏幕捕获成图像文件? 我正在使用Qt,但Qt似乎没有用于此的API。 所以我想知道是否有任何API或库可以做到这一点。 谢谢_Qt_Qt5_Screen Capture_Openprocess - Fatal编程技术网

在c+中打开进程并捕获其区域的屏幕+;在Windows上使用Qt 如何在Windows上C++中打开进程并将其区域的屏幕捕获成图像文件? 我正在使用Qt,但Qt似乎没有用于此的API。 所以我想知道是否有任何API或库可以做到这一点。 谢谢

在c+中打开进程并捕获其区域的屏幕+;在Windows上使用Qt 如何在Windows上C++中打开进程并将其区域的屏幕捕获成图像文件? 我正在使用Qt,但Qt似乎没有用于此的API。 所以我想知道是否有任何API或库可以做到这一点。 谢谢,qt,qt5,screen-capture,openprocess,Qt,Qt5,Screen Capture,Openprocess,我的第一次尝试是这样的。它只捕获窗口的内容,而不是窗口本身。是否还要捕获整个窗口或屏幕 #include <QApplication> #include <QTreeView> #include <QScreen> #include <QPixmap> #include <QTimer> int main(int argc, char** args) { QApplication app(argc, args); au

我的第一次尝试是这样的。它只捕获窗口的内容,而不是窗口本身。是否还要捕获整个窗口或屏幕

#include <QApplication>
#include <QTreeView>
#include <QScreen>
#include <QPixmap>
#include <QTimer>

int main(int argc, char** args) {
    QApplication app(argc, args);
    auto view = new QTreeView;
    view->show();
    QTimer::singleShot(10, [&]() {
        auto screen = app.primaryScreen();
        auto pixmap = screen->grabWindow(view->winId());
        pixmap.save("Screenshot.png");
    });
    app.exec();
}
#包括
#包括
#包括
#包括
#包括
int main(int argc,char**args){
QApplication应用程序(argc、args);
自动查看=新建QTreeView;
查看->显示();
QTimer::单发(10,[&](){
自动屏幕=app.primaryScreen();
自动pixmap=屏幕->抓取窗口(视图->winId());
保存(“Screenshot.png”);
});
app.exec();
}

您可以使用Windows API执行此操作

1.获取进程id

qint64 pid = 0;
QProcess::startDetached("calc.exe", QStringList(), QString(), &pid);
HWND g_HWND=NULL;
BOOL CALLBACK EnumWindowsProcMy(HWND hwnd,LPARAM lParam)
{
    DWORD lpdwProcessId;
    GetWindowThreadProcessId(hwnd,&lpdwProcessId);
    if(lpdwProcessId==lParam)
    {
        g_HWND = hwnd;
        return FALSE;
    }
    return TRUE;
}

g_HWND = NULL;
EnumWindows(EnumWindowsProcMy,pid);
2.从进程id获取窗口处理程序

qint64 pid = 0;
QProcess::startDetached("calc.exe", QStringList(), QString(), &pid);
HWND g_HWND=NULL;
BOOL CALLBACK EnumWindowsProcMy(HWND hwnd,LPARAM lParam)
{
    DWORD lpdwProcessId;
    GetWindowThreadProcessId(hwnd,&lpdwProcessId);
    if(lpdwProcessId==lParam)
    {
        g_HWND = hwnd;
        return FALSE;
    }
    return TRUE;
}

g_HWND = NULL;
EnumWindows(EnumWindowsProcMy,pid);
3.使用此功能从您的
HWND
中获取屏幕截图并保存到文件中

int WinSysUtils::CaptureAnImage(HWND hWnd, const wchar_t *fileName)
{
    HDC dcScreen = GetDC(hWnd);
    HDC dcTarget = CreateCompatibleDC(dcScreen);

    RECT rect;
    GetClientRect(hWnd, &rect);

    int width = rect.right - rect.left;
    int height = rect.bottom - rect.top;

    HBITMAP bmpTarget = CreateCompatibleBitmap(dcScreen, width, height);
    HGDIOBJ oldBmp = SelectObject(dcTarget, bmpTarget);

    BitBlt(dcTarget, 0, 0, width, height, dcScreen, 0, 0, SRCCOPY | CAPTUREBLT);
    SelectObject(dcTarget, oldBmp);

    // Get the BITMAP from the HBITMAP
    BITMAP bmpScreen;
    GetObject(bmpTarget, sizeof(BITMAP), &bmpScreen);

    BITMAPFILEHEADER   bmfHeader;
    BITMAPINFOHEADER   bi;

    bi.biSize = sizeof(BITMAPINFOHEADER);
    bi.biWidth = bmpScreen.bmWidth;
    bi.biHeight = bmpScreen.bmHeight;
    bi.biPlanes = 1;
    bi.biBitCount = 32;
    bi.biCompression = BI_RGB;
    bi.biSizeImage = 0;
    bi.biXPelsPerMeter = 0;
    bi.biYPelsPerMeter = 0;
    bi.biClrUsed = 0;
    bi.biClrImportant = 0;

    DWORD dwBmpSize = ((bmpScreen.bmWidth * bi.biBitCount + 31) / 32) * 4 * bmpScreen.bmHeight;

    // Starting with 32-bit Windows, GlobalAlloc and LocalAlloc are implemented as wrapper functions that
    // call HeapAlloc using a handle to the process's default heap. Therefore, GlobalAlloc and LocalAlloc
    // have greater overhead than HeapAlloc.
    HANDLE hDIB = GlobalAlloc(GHND, dwBmpSize);
    char *lpbitmap = (char *)GlobalLock(hDIB);

    // Gets the "bits" from the bitmap and copies them into a buffer
    // which is pointed to by lpbitmap.
    GetDIBits(dcScreen, bmpTarget, 0,
              (UINT)bmpScreen.bmHeight,
              lpbitmap,
              (BITMAPINFO *)&bi, DIB_RGB_COLORS);

    // A file is created, this is where we will save the screen capture.
    HANDLE hFile = CreateFile(fileName,
            GENERIC_WRITE,
            0,
            NULL,
            CREATE_ALWAYS,
            FILE_ATTRIBUTE_NORMAL, NULL);

    // Add the size of the headers to the size of the bitmap to get the total file size
    DWORD dwSizeofDIB = dwBmpSize + sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);

    //Offset to where the actual bitmap bits start.
    bmfHeader.bfOffBits = (DWORD)sizeof(BITMAPFILEHEADER) + (DWORD)sizeof(BITMAPINFOHEADER);

    //Size of the file
    bmfHeader.bfSize = dwSizeofDIB;

    //bfType must always be BM for Bitmaps
    bmfHeader.bfType = 0x4D42; //BM

    DWORD dwBytesWritten = 0;
    WriteFile(hFile, (LPSTR)&bmfHeader, sizeof(BITMAPFILEHEADER), &dwBytesWritten, NULL);
    WriteFile(hFile, (LPSTR)&bi, sizeof(BITMAPINFOHEADER), &dwBytesWritten, NULL);
    WriteFile(hFile, (LPSTR)lpbitmap, dwBmpSize, &dwBytesWritten, NULL);

    //Unlock and Free the DIB from the heap
    GlobalUnlock(hDIB);
    GlobalFree(hDIB);

    //Close the handle for the file that was created
    CloseHandle(hFile);

    DeleteDC(dcTarget);
    DeleteDC(dcScreen);

    return 0;
}

您想捕获整个屏幕还是只捕获其中的一部分?处理是什么意思?您还可以尝试更改
grabWindow
的选项,以获取完整的窗口。不幸的是,我没有找到一个简单的方法来获得所需的大小增量,这取决于您当前的操作系统和操作系统设置。