我正在使用bitblt捕获一个窗口.如果启用了航空主题,则捕获图像的背景为黑色.如果我禁用DWM并捕获窗口,则捕获的图像非常好.
这是我的代码的一部分.
HDC hdcMemDC = GDI32.INSTANCE.CreateCompatibleDC(desktopDC); HDC windowDC = User32.INSTANCE.GetDC(window); HWND window= User32Extra.INSTANCE.FindWindow(null,"Start menu"); GDI32Extra.INSTANCE.BitBlt(hdcMemDC,width,height,desktopDC,WinGDIExtra.SRCCOPY ); GDI32Extra.INSTANCE.BitBlt(hdcMemDC,windowBounds.left,windowBounds.top,windowWidth,windowHeight,windowDC,windowBounds.left+windowBounds1.right-windowBounds.right+(windowExtraGap/2),windowBounds.top+windowBounds1.bottom-windowBounds.bottom+(windowExtraGap/2),WinGDIExtra.SRCCOPY);
如何捕获具有适当背景的开始菜单?
有没有其他方法可以获得适当的航空窗口图像?
解决方法
使用桌面DC和切割到窗口
RECT rc,rc2; GetClientRect(hWnd,&rc); GetWindowRect(hWnd,&rc2); int width = rc2.right - rc2.left; int height = rc2.bottom - rc2.top; HDC hdcScreen = GetDC(NULL); //!!!! Get desktop DC HDC hBmpFileDC = CreateCompatibleDC(hdcScreen); HBITMAP hBmpFileBitmap = CreateCompatibleBitmap(hdcScreen,height); HBITMAP hOldBitmap = (HBITMAP)SelectObject(hBmpFileDC,hBmpFileBitmap); BitBlt(hBmpFileDC,hdcScreen,rc2.left,rc2.top,SRCCOPY | CAPTUREBLT); HGdioBJ prev = SelectObject(hBmpFileDC,hOldBitmap); SaveBitmap(szLogFilename,hBmpFileBitmap); DeleteDC(hBmpFileDC); DeleteObject(hBmpFileBitmap);
另一种变体
RECT rc; GetClientRect(hWnd,&rc); int width = rc.right - rc.left; int height = rc.bottom - rc.top; HDC hdcScreen = GetDC(hWnd); //////////////////////////// PrintWindow(hWnd,0); PrintWindow(hWnd,PW_CLIENTONLY); //////////////////////////// HDC hBmpFileDC = CreateCompatibleDC(hdcScreen); HBITMAP hBmpFileBitmap = CreateCompatibleBitmap(hdcScreen,hBmpFileBitmap); DeleteDC(hBmpFileDC); DeleteObject(hBmpFileBitmap);
在调用任何捕获方法之前我调用PrintWindow.它用于重绘自身的窗口.因此,屏幕截图将具有正确的图片.我通过PrintWindow的双重调用获得了最稳定的结果.