#define ENABLE_ALL_SHOT (TRUE) // 一時的に最大化した後キャプチャ...(TRUE:する/FALSE:しない) void CMainDialogFx::SaveImage() { BOOL bDwmEnabled = FALSE; static HMODULE hModule = LoadLibraryEx(L"dwmapi.dll", NULL, LOAD_LIBRARY_SEARCH_SYSTEM32); typedef HRESULT(WINAPI* FuncDwmGetWindowAttribute) (HWND hwnd, DWORD dwAttribute, PVOID pvAttribute, DWORD cbAttribute); typedef HRESULT(WINAPI* FuncDwmIsCompositionEnabled)(BOOL* pfEnabled); static FuncDwmGetWindowAttribute pDwmGetWindowAttribute = (FuncDwmGetWindowAttribute)GetProcAddress(hModule, "DwmGetWindowAttribute"); static FuncDwmIsCompositionEnabled pDwmIsCompositionEnabled = (FuncDwmIsCompositionEnabled)GetProcAddress(hModule, "DwmIsCompositionEnabled"); if (hModule && pDwmGetWindowAttribute && pDwmIsCompositionEnabled) { pDwmIsCompositionEnabled(&bDwmEnabled); } CRect rc1; CRect rc2; CRect rc3; CImage* image1 = new CImage(); CImage* image2 = new CImage(); CImage* image3 = new CImage(); #if(ENABLE_ALL_SHOT) CRect rcOrigin; GetWindowRect(&rcOrigin); SetWindowPos(NULL, 0, 0, m_MaxSizeX, m_MaxSizeY, SWP_NOZORDER | SWP_NOMOVE); #endif if (bDwmEnabled) { GetWindowRect(&rc1); if (image1->Create(rc1.Width(), rc1.Height(), 32)) { HDC hImage1DC = image1->GetDC(); if (IsWin81orLater()) { ::PrintWindow(m_hWnd, hImage1DC, 2); // PW_RENDERFULLCONTENT, Windows 8.1 or later GetClientRect(&rc3); if (image3->Create(rc3.Width(), rc3.Height(), 32)) { HDC hImage3DC = image3->GetDC(); ::PrintWindow(m_hWnd, hImage3DC, 1); // PW_CLIENTONLY int targetY = 0; int offsetX = (rc1.Width() - rc3.Width()) / 2; int offsetY = (rc1.Height() - rc3.Height()) / 2; // Compare Screenshot for (int y = offsetY; y < rc1.Height() - rc3.Height(); y++) { for (int x = 0; x < rc3.Width(); x++) { if (image1->GetPixel(offsetX + x, y) == image3->GetPixel(x, 0)) { if (x == rc3.Width() / 2) { for (int yy = 0; yy < rc3.Height(); yy++) { if (image1->GetPixel(offsetX, y + yy) == image3->GetPixel(0, yy)) { if (yy == rc3.Height() / 2) { targetY = y; goto loopEnd; } } else { break; } } } } else { break; } } } loopEnd: if (targetY > 0) { ::BitBlt(hImage1DC, (rc1.Width() - rc3.Width()) / 2, targetY, rc3.Width(), rc3.Height(), hImage3DC, 0, 0, SRCCOPY); } image3->ReleaseDC(); } } else { ::PrintWindow(m_hWnd, hImage1DC, 0); } pDwmGetWindowAttribute(m_hWnd, DWMWA_EXTENDED_FRAME_BOUNDS, &rc2, sizeof(rc2)); // cstr.Format(L"Width=%d/%d, Height=%d/%d", rc1.Width(), rc2.Width(), rc1.Height(), rc2.Height()); // AfxMessageBox(cstr); if (rc1.Width() > rc2.Width()) { if (image2->Create(rc2.Width(), rc2.Height(), 32)) { HDC hImage2DC = image2->GetDC(); ::BitBlt(hImage2DC, 0, 0, rc2.Width(), rc2.Height(), hImage1DC, (rc1.Width() - rc2.Width()) / 2, 0, SRCCOPY); image2->ReleaseDC(); SaveImageDlg(image2); } else { SaveImageDlg(image1); } } else { SaveImageDlg(image1); } image1->ReleaseDC(); } } else { GetWindowRect(&rc1); if (image1->Create(rc1.Width(), rc1.Height(), 32)) { HDC hImage1DC = image1->GetDC(); ::PrintWindow(m_hWnd, hImage1DC, 0); SaveImageDlg(image1); image1->ReleaseDC(); } } #if(ENABLE_ALL_SHOT) SetWindowPos(NULL, 0, 0, rcOrigin.Width(), rcOrigin.Height(), SWP_NOZORDER | SWP_NOMOVE); #endif SAFE_DELETE(image1); SAFE_DELETE(image2); SAFE_DELETE(image3); }