BOOL CaptureWnd(int pid, CString filename) { HWND hHwnd = NULL; if (pid == 0) hHwnd = GetDesktopWindow(); else { // explorer.exe 인 경우 전체화면 HANDLE hProcess = OpenProcess(PROCESS_VM_OPERATION | PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, pid); WCHAR buf[MAX_PATH] = {0,}; if (GetProcessImageFileName(hProcess, buf, MAX_PATH)) { CString strFinder(buf); strFinder.MakeUpper(); if (strFinder.Find(_T("EXPLORER.EXE")) > 0) { hHwnd = GetDesktopWindow(); } } else { WCHAR buf[MAX_PATH] = {0,}; GetProcessName(pid, buf, MAX_PATH); AfxMessageBox(buf); RECT rect = {0,}; HWND hBackWnd = GetDesktopWindow(); GetHWNDFromPID(pid, NULL, &hBackWnd, rect); hHwnd = hBackWnd; } } if (hHwnd == NULL) hHwnd = GetDesktopWindow(); CWnd* pWnd = CWnd::FromHandle(hHwnd); CClientDC dc(pWnd); CRect rect; ::GetClientRect(hHwnd, rect); CDC dcMem; dcMem.CreateCompatibleDC(&dc); CBitmap bitmap; bitmap.CreateCompatibleBitmap(&dc, rect.Width(), rect.Height()); CBitmap* bitmapOld = dcMem.SelectObject(&bitmap); dcMem.BitBlt(0, 0, rect.Width(), rect.Height(), &dc, 0, 0, SRCCOPY); // Watermarking CFont font; font.CreateFont(12, 0, 0, 0, FW_BOLD, FALSE, FALSE, 0, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH | FF_SWISS, _T("굴림체")); CFont* fontOld = dcMem.SelectObject(&font); dcMem.SetBkMode(TRANSPARENT); CString str = filename; dcMem.SetTextColor(RGB(33, 255, 33)); dcMem.TextOut(20, rect.Height() - 70, str.GetBuffer(0), str.GetLength()); TCHAR buf[MAX_PATH] = {0,}; if (CUtilGlobal::GetProcessPathByPID(pid, buf)) str.Format(_T("%s (Pid=%d)"), buf, pid); else str.Format(_T("Get Process Path Failed (Pid=%d)"), pid); dcMem.TextOut(20, rect.Height() - 55, str.GetBuffer(0), str.GetLength()); //str.Format(_T("(Dll:C:\\Program Files (x86)\\DreamSecurity\\NTSMagicLineMBX\\PKICertCls.dll)")); //dcMem.TextOut(20, rect.Height() - 40, str.GetBuffer(0), str.GetLength()); // Save CImage captureImage; if(!captureImage.Create(rect.Width(), rect.Height(), 32)) return FALSE; HDC hDC = captureImage.GetDC(); BitBlt(hDC, 0, 0, rect.Width(), rect.Height(), dcMem.GetSafeHdc(), 0, 0, SRCCOPY); captureImage.Save(filename, Gdiplus::ImageFormatJPEG); captureImage.ReleaseDC(); dcMem.SelectObject(&bitmapOld); dcMem.SelectObject(&fontOld); dcMem.DeleteDC(); return TRUE; }
//CaptureWnd(process id, file path); CaptureWnd(0, _T("C:\\capture.jpg")); //전체화면
Retrieved from http://hyacinth.byus.net/moniwiki/wiki.php/C++/(Windows)캡처 윈도우
last modified 2014-12-16 17:43:32