keybd_event(VK_SNAPSHOT,1,0,0);//拷贝屏幕到剪贴板
                           
CPrintDialog dlg(false);
if(!dlg.GetDefaults())
{
return;
} dlg.GetDevMode()->dmOrientation = 1; dlg.DoModal();
HDC hdc = dlg.GetPrinterDC();
CDC *pdc; pdc = CDC::FromHandle(hdc);
pdc->StartDoc("abc");
pdc->StartPage();OpenClipboard();
HBITMAP hbmp = (HBITMAP)GetClipboardData(CF_BITMAP);
CBitmap* bm = CBitmap::FromHandle(handle);CDC dc;
dc.CreateCompatibleDC(pdc);
dc.SelectObject(bm);
pdc->BitBlt(0,0,1600,1600,&dc,0,0,SRCCOPY);
pdc->MoveTo(100,100);
pdc->LineTo(200,200);
pdc->EndPage();
pdc->EndDoc();
CloseClipboard();为什么这样不能把位图打印出来呢?是哪里出了问题?

解决方案 »

  1.   

    CPrintDialog dlg(false);
    if(!dlg.GetDefaults())
    {
    return;
    } dlg.GetDevMode()->dmOrientation = 1; dlg.DoModal();
    HDC hdc = dlg.GetPrinterDC();
    CDC *pdc; pdc = CDC::FromHandle(hdc);
    pdc->StartDoc("abc");
    pdc->StartPage(); pdc->BitBlt(0,0,1600,1600,this->GetDC(),0,0,SRCCOPY);
    pdc->MoveTo(100,100);
    pdc->LineTo(200,200);
    pdc->EndPage();
    pdc->EndDoc();
    这样能把屏幕打印出来,但不是我想要的.
      

  2.   

    CDC * pDC; // デバイスコンテキスト
    CDC DCMem; // メモリデバイスコンテキスト
    CDC DCPrn; // 印刷用デバイスコンテキスト
    HBITMAP hBitmap; // ビットマップハンドル
    HBITMAP Soh; // ビットマップハンドル
    DOCINFO DocInfo; // 印刷ジョブ構造体static
    CRect rect; // ダイアログ領域 // ハードコピーするウィンドウを決定する
    CWnd *pWnd = AfxGetApp()->m_pMainWnd; // 「例」 // 指令されたウィンドウの領域を取得する
    pWnd->GetWindowRect(&rect); // ディスプレイ デバイスコンテキスト作成
    pDC = pWnd->GetWindowDC(); // 注)GetDC()は、クライアント領域になる
    LPBITMAPINFO lpbi;
    LPTSTR lpBits = NULL; #define WIDTHBYTES(bits) ((((bits) + 31) / 32) * 4) // Fill in the BITMAPINFOHEADER
    lpbi = (LPBITMAPINFO) new BYTE[sizeof(BITMAPINFOHEADER)];
    lpbi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
    lpbi->bmiHeader.biWidth = rect.Width(); 
    lpbi->bmiHeader.biHeight = rect.Height(); 
    lpbi->bmiHeader.biPlanes = 1;
    lpbi->bmiHeader.biBitCount = 24;
    lpbi->bmiHeader.biCompression = BI_RGB;
    lpbi->bmiHeader.biSizeImage = WIDTHBYTES(rect.Width()) * rect.Height();
    lpbi->bmiHeader.biXPelsPerMeter = 0;
    lpbi->bmiHeader.biYPelsPerMeter = 0;
    lpbi->bmiHeader.biClrUsed = 0;
    lpbi->bmiHeader.biClrImportant = 0; hBitmap = CreateDIBSection(pDC->m_hDC, lpbi, DIB_RGB_COLORS, (void **)&lpBits, NULL, 0); // DSPメモリ デバイスコンテキスト作成
    DCMem.CreateCompatibleDC(pDC);
    Soh = (HBITMAP)SelectObject(DCMem.m_hDC, hBitmap); // ビットマップ複写
    DCMem.BitBlt(0, 0, rect.Width(), rect.Height(), pDC, 0, 0, SRCCOPY);#define CLIPBOARD 1 // クリップボードに転送する場合#ifdef CLIPBOARD
    VERIFY(OpenClipboard());
    VERIFY(EmptyClipboard());
    VERIFY(::SetClipboardData(CF_BITMAP, hBitmap) != NULL);
    CloseClipboard();
    #endif // CLIPBOARD // プリンタ デバイスコンテキスト作成
    CPrintDialog dlg(TRUE, PD_ALLPAGES, this); if (dlg.DoModal() == IDOK)
    {
    LPDEVMODE pDM = dlg.GetDevMode();
    pDM->dmCopies = 1;
    // 例 "Canon MEDIO-B1 LIPS4", "Canon BJC-700J"
    DCPrn.CreateDC("WINSPOOL", dlg.GetDeviceName(), NULL, NULL);
    DCPrn.ResetDC(pDM); DocInfo.cbSize = sizeof(DOCINFO);
    DocInfo.lpszDocName = "ハードコピー";
    DocInfo.lpszOutput = (LPSTR)NULL; int dmOrientation = pDM->dmOrientation;
    int Width = DCPrn.GetDeviceCaps(HORZRES) - 100;
    int Height = DCPrn.GetDeviceCaps(VERTRES) - 100;
    double Magnification;
    double MagnificationW = (double)Width / (double)rect.Width();
    double MagnificationH = (double)Height / (double)rect.Height();
    Magnification = MagnificationW >= MagnificationH ? MagnificationH : MagnificationW; if (DCPrn.StartDoc(&DocInfo) != -1)
    {
    DCPrn.StartPage(); // ビットマップ拡大複写
    int iBitMap_X = (int)((double)rect.Width() * Magnification);
    int iBitMap_Y = (int)((double)rect.Height() * Magnification);
    int iOffset_X = (DCPrn.GetDeviceCaps(HORZRES) - iBitMap_X - 50) / 2;
    int iOffset_Y; if (dmOrientation == 1)
    // 縦の場合
    iOffset_Y = 50;
    else
    // 横の場合
    iOffset_Y = (DCPrn.GetDeviceCaps(VERTRES) - iBitMap_Y) / 2; DCPrn.StretchBlt(iOffset_X, iOffset_Y, iBitMap_X, iBitMap_Y, &DCMem, 0, 0, rect.Width(), rect.Height(), SRCCOPY); DCPrn.EndPage();
    DCPrn.EndDoc();
    } DCPrn.DeleteDC();
    } delete [] lpbi;
    SelectObject(DCMem.m_hDC, Soh);
    DCMem.DeleteDC();#ifndef CLIPBOARD // クリップボードの場合、ビットマップを削除するとなくなってしまうため。
    DeleteObject(hBitmap);
    #endif // !CLIPBOARD