mfc 프린트에 해당하는 글 1

MFC 화면 프린트 출력

300x250

 

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
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

PrintTest.zip
0.24MB

 

 

 

 

 

 

 

 

300x250

댓글()