CString CPrintTestDlg::GetExecutedPath()
{
//실행파일 경로 구하는 함수
CString strResult;
CString strPath;
if (GetModuleFileName(nullptr, strPath.GetBuffer(_MAX_PATH + 1), MAX_PATH) != FALSE)
{
strPath.ReleaseBuffer();
strResult = strPath.Left(strPath.ReverseFind('\\') + 1);
}
return strResult;
}
CPoint CPrintTestDlg::GetPopupOffset()
{
CPoint point;
CRect rect;
this->GetWindowRect(rect); // 메인 다이얼로그 Rect를 받아야 함
point.x = rect.left;
point.y = rect.top;
return point;
}
void CPrintTestDlg::Capture(CString file_name)
{
CRect rect;
GetClientRect(rect);
CWnd* pWndDesktop = GetDesktopWindow();
CWindowDC ScrDC(pWndDesktop);
int sx = rect.left;
int sy = rect.top;
const CPoint offset = GetPopupOffset();
sx += offset.x;
sy += offset.y;
int cx = rect.Width();
int cy = rect.Height();
CImage Image;
(void)Image.Create(cx, cy, ScrDC.GetDeviceCaps(BITSPIXEL));
CDC* pDC = CDC::FromHandle(Image.GetDC());
(void)pDC->BitBlt(0, 0, cx, cy, &ScrDC, sx, sy, SRCCOPY);
Image.ReleaseDC();
CString str_save;
str_save = file_name;
long result = Image.Save(str_save, Gdiplus::ImageFormatPNG);
if (result < 0)
{
(void)AfxMessageBox(_T("캡쳐 오류"));
}
}
void CPrintTestDlg::PrintOutImage(CString file_name)
{
int nWidth, nHeight;
CClientDC dc(this);//this->pImgWnd
CDC MemDC;
MemDC.CreateCompatibleDC(&dc);
CRect rect;
GetClientRect(rect);
nWidth = rect.Width();
nHeight = rect.Height();
CBitmap BMP;
CImage image;
image.Load(file_name);
if (image.IsNull() == false)
{
int si = image.GetWidth();
int sih = image.GetHeight();
BMP.Attach(image.Detach());
HANDLE hDib;
LPSTR pDib;
LPBITMAPINFO lpBitInfo;
HANDLE hlpBitInfo;
hDib = GlobalAlloc(GHND, nWidth*nHeight * 3);
pDib = (LPSTR)GlobalLock(hDib);
hlpBitInfo = GlobalAlloc(GHND, sizeof(BITMAPINFOHEADER) + sizeof(BITMAPINFO));
lpBitInfo = (LPBITMAPINFO)GlobalLock(hlpBitInfo);
lpBitInfo->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
lpBitInfo->bmiHeader.biWidth = nWidth;
lpBitInfo->bmiHeader.biHeight = nHeight;
lpBitInfo->bmiHeader.biPlanes = 1;
lpBitInfo->bmiHeader.biBitCount = 24;
lpBitInfo->bmiHeader.biCompression = BI_RGB;
lpBitInfo->bmiHeader.biSizeImage = nWidth * nHeight * 3;
lpBitInfo->bmiHeader.biXPelsPerMeter = 0;
lpBitInfo->bmiHeader.biYPelsPerMeter = 0;
lpBitInfo->bmiHeader.biClrUsed = 0;
lpBitInfo->bmiHeader.biClrImportant = 0;
HDC hdc = ::GetDC(this->m_hWnd);
GetDIBits(hdc, (HBITMAP)BMP, 0, nHeight, pDib, lpBitInfo, DIB_RGB_COLORS);
::ReleaseDC(this->m_hWnd, hdc);
static DOCINFO docinfo = { sizeof(DOCINFO), _T("프린트"), NULL };
//팝업창 안띠우고 기본 설정 프린터로 인쇄
//CPrintDialog dlg(TRUE, PD_RETURNDEFAULT);
//dlg.DoModal();
//팝업창 뛰우고 프린터 선택해서 인쇄
CPrintDialog dlg(FALSE);
if (dlg.DoModal() == IDCANCEL)
{
GlobalUnlock(hDib);
GlobalFree(hDib);
GlobalUnlock(hlpBitInfo);
GlobalFree(hDib);
DeleteObject(BMP.m_hObject);
DeleteDC(MemDC.m_hDC);
DeleteDC(dc.m_hDC);
return;
}
DEVMODE *pDevmode = (DEVMODE*)dlg.GetDevMode();
pDevmode->dmOrientation = 2;
HDC hpdc = dlg.CreatePrinterDC();
int cy = GetDeviceCaps(hpdc, VERTRES);
int cx = GetDeviceCaps(hpdc, HORZRES);
if (StartDoc(hpdc, &docinfo))
{
if (StartPage(hpdc))
{
StretchDIBits(hpdc,
0, 0, cx, cy, 0, nHeight - sih, si, sih, pDib, lpBitInfo, DIB_RGB_COLORS, SRCCOPY);
//image.StretchBlt(image.GetDC(), 0, 0, nWidth, nHeight, SRCCOPY);
EndPage(hpdc);
}
EndDoc(hpdc);
}
GlobalUnlock(hDib);
GlobalFree(hDib);
GlobalUnlock(hlpBitInfo);
GlobalFree(hlpBitInfo);
DeleteObject(BMP.m_hObject);
DeleteDC(dc.m_hDC);
DeleteDC(MemDC.m_hDC);
::RestoreDC(hpdc, -1);
}
}