[英]Saving screenshot of a specific window as bmp in window
I need to get a screen view of a specific window as .bmp
format. 我需要以.bmp
格式获取特定窗口的屏幕视图。 In other words I need to have the functionality of alt+printscreen
and it needs to work even the window is at back or minimized. 换句话说,我需要具有alt+printscreen
的功能,并且即使窗口处于后退或最小化也需要它起作用。 So I wrote a function below which returns a HBITMAP
type in order to save the view as .bmp
file with another function later. 因此,我在下面编写了一个函数,该函数返回HBITMAP
类型,以便稍后将视图另存为.bmp
文件。
HBITMAP CaptureWindowBitmap(HWND MyHWND)
{
HDC hWindowDC = GetWindowDC(MyHWND);
HDC hMemoryDC = CreateCompatibleDC(hWindowDC);
int x = GetDeviceCaps(hWindowDC, HORZRES);
int y = GetDeviceCaps(hWindowDC, VERTRES);
HBITMAP hBitmap = CreateCompatibleBitmap(hWindowDC, x, y);
HBITMAP hBitmapOld = (HBITMAP)SelectObject(hMemoryDC, hBitmap);
BitBlt(hMemoryDC, 0, 0, x, y, hWindowDC, 0, 0, SRCCOPY);
hBitmap = (HBITMAP)SelectObject(hMemoryDC, hBitmapOld);
DeleteDC(hMemoryDC);
DeleteDC(hWindowDC);
return hBitmap;
}
However my function gets the image of the whole screen. 但是,我的功能可以获取整个屏幕的图像。 How can I fix this? 我怎样才能解决这个问题?
Here is the solution i found from elsewhere: 这是我从其他地方找到的解决方案:
HBITMAP CaptureWindowBitmap(HWND MyHWND)
{
RECT rc;
GetClientRect(MyHWND, &rc);
HDC hdcScreen = GetDC(NULL);
HDC hdc = CreateCompatibleDC(hdcScreen);
HBITMAP hbmp = CreateCompatibleBitmap(hdcScreen, rc.right - rc.left, rc.bottom - rc.top);
SelectObject(hdc, hbmp);
PrintWindow(MyHWND, hdc, PW_CLIENTONLY);
DeleteDC(hdc);
ReleaseDC(NULL, hdcScreen);
return hbmp;
}
Window is captured by the function below: 窗口由以下功能捕获:
BOOL CALLBACK EnumWindowsProc(HWND hwnd, LPARAM lParam)
{
char buffer[128];
int written = GetWindowTextA(hwnd, buffer, 128);
if (written && strstr(buffer,WindowName) ) {
*(HWND*)lParam = hwnd;
return FALSE;
}
return TRUE;
}
HWND GetHwnd()
{
HWND hWnd = NULL;
EnumWindows(EnumWindowsProc, (LPARAM)&hWnd);
return hWnd;
}
where LPCTSTR WindowName
is a global variable containing the name or partial name of the window. 其中LPCTSTR WindowName
是一个全局变量,包含窗口的名称或部分名称。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.