根据MSDN
lpWindowName [输入,可选]
Type: LPCTSTR
The window name (the window's title). If this parameter is NULL, all window names match.
因此,您的 WindowName 不能是“Mozilla Firefox”,因为 Firefox 窗口的标题永远不会是“Mozilla Firefox”,但它可能是“Mozilla Firefox Start Page - Mozilla Firefox”或其他取决于网页名称的东西。这是示例图片
因此,您的代码应该是这样的,(下面的代码仅适用 -仅当您具有确切窗口的标题名称时才有效:“Mozilla Firefox Start Page - Mozilla Firefox”,如上图所示。我已经在 Windows 8.1 上进行了测试并且它有效)
void CaptureWindow()
{
RECT rc;
HWND hwnd = ::FindWindow(0, _T("Mozilla Firefox Start Page - Mozilla Firefox"));//::FindWindow(0,_T("ScreenCapture (Running) - Microsoft Visual Studio"));//::FindWindow(0, _T("Calculator"));//= FindWindow("Notepad", NULL); //You get the ideal?
if (hwnd == NULL)
{
return;
}
GetClientRect(hwnd, &rc);
//create
HDC hdcScreen = GetDC(NULL);
HDC hdc = CreateCompatibleDC(hdcScreen);
HBITMAP hbmp = CreateCompatibleBitmap(hdcScreen,
rc.right - rc.left, rc.bottom - rc.top);
SelectObject(hdc, hbmp);
//Print to memory hdc
PrintWindow(hwnd, hdc, PW_CLIENTONLY);
//copy to clipboard
OpenClipboard(NULL);
EmptyClipboard();
SetClipboardData(CF_BITMAP, hbmp);
CloseClipboard();
//release
DeleteDC(hdc);
DeleteObject(hbmp);
ReleaseDC(NULL, hdcScreen);
//Play(TEXT("photoclick.wav"));//This is just a function to play a sound, you can write it yourself, but it doesn't matter in this example so I comment it out.
}