728x90

아래의 코드를 그냥 인쇄하고 싶은 버튼에 넣으면 된다...


    // 인쇄 코드.

    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;

    BMP.CreateCompatibleBitmap(&dc, rect.Width(), rect.Height());

    CBitmap* pOldBitmap = MemDC.SelectObject(&BMP);

    MemDC.BitBlt(0, 0, nWidth, nHeight, &dc, 0, 0, SRCCOPY);


    /*

    SECJpeg* jpg = new SECJpeg();

    jpg->CreateFromBitmap(&MemDC,&BMP);

    jpg->SaveImage("Test.jpg");

    */


    HANDLE hDib;

    LPSTR pDib;

    LPBITMAPINFO lpBitInfo;

    HANDLE hlpBitInfo;

    //CBitmap BMP;


    //BMP.LoadBitmap(IDB_BITMAP1);


    hDib = GlobalAlloc(GHND, nWidth*nHeight * 3);

    pDib = (LPSTR)GlobalLock(hDib);

    hlpBitInfo = GlobalAlloc(GHND, sizeof(BITMAPINFOHEADER) + sizeof(BITMAPINFO));

    lpBitInfo = (LPBITMAPINFO)GlobalLock(hlpBitInfo);


    //BITMAPINFO

    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;

    ////BITMAPINFO  


    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("IMAGE"), NULL };


    CPrintDialog dlg(FALSE);

    if (dlg.DoModal() == IDCANCEL)

        return;


    HDC hpdc = dlg.GetPrinterDC();

    int cx, cy;


    cy = GetDeviceCaps(hpdc, VERTRES);

    cx = GetDeviceCaps(hpdc, HORZRES);


    if (StartDoc(hpdc, &docinfo))

    {

        if (StartPage(hpdc))

        {

            StretchDIBits(hpdc,

                0, 0, cx, cy, 0, 0, nWidth, nHeight, pDib, lpBitInfo, DIB_RGB_COLORS, SRCCOPY);

            EndPage(hpdc);

        }

        EndDoc(hpdc);

    }

    ::RestoreDC(hpdc, -1);


출처 : http://frostguy.tistory.com/42

728x90

+ Recent posts