화면 이미지 저장 및 프린트 출력

300x250

 

 

[ 프로그램 UI ]

프린트 버튼을 누르면

 

위와 같이 프린트 다이얼로그가 뜨면서 인쇄할 수 있습니다.

(프린트 다이얼로그가 안뜨게 하는 코드도 첨부)

 

 

[ 주요 메인 코드 ]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
 
void CPrintTestDlg::OnBnClickedButton1()
{
    CTime t = CTime::GetCurrentTime();
    CString str_time;
 
    int temp_year = (int)(t.GetYear() / 100* 100;
    temp_year = t.GetYear() - temp_year;
 
    str_time.Format(_T("Capture_%02d%02d%02d_%02d%02d%02d.png"), temp_year, t.GetMonth(),
        t.GetDay(), t.GetHour(), t.GetMinute(), t.GetSecond());
    save_path_png_ = GetExecutedPath() + str_time;
 
    //1. 이미지 저장
    Capture(save_path_png_);
 
    //2. 이미지 프린트
    PrintOutImage(save_path_png_);
 
    //3. 파일 삭제
    DeleteFile(save_path_png_);
}
 
cs

버튼을 누르면 현재 시간으로 파일 경로를 만듭니다.  그 이 후

 

1. Capture() : 이미지 캡쳐 및 저장 함수 ( 듀얼 모니터 인식 )

2. PrintOutImage() : 이미지 출력 함수 ( 출력 다이얼로그 ON / OFF 가능)

3. DeleteFile() : 파일 삭제 함수

 

와 같은 순으로 함수가 호출 됩니다.

 

 

[ 나머지 함수 코드 ]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
 
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(00, 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,
                    00, 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);
    }
}
cs

GetExecutedPath(); // 실행 파일 경로 확인
GetPopupOffset(); // 듀얼 모니터 좌표인식
Capture(CString file_name); : 화며 캡쳐 및 이미지 저장
PrintOutImage(CString file_name); : 출력

 

 

[소스파일 다운로드] 아래

PrintTest.zip
3.48MB

300x250

댓글()