Title is No Title

not very good here!

导航

print a dialog is simple.

1:add friend in view class;2:add CDC and CBitmap and onPrint() and ..some func,3:
and then at dlg's OnOk or other func call OnPriviewFunction is ok!
1:
public://add for print form
 CRect m_rect;
 CBitmap * m_pBm;
 CDC * m_pMemDC;
  
 friend class CAboutDlg;
 friend class TestDrawDlg;
 CPrintInfo *pInfo;
protected: // create from serialization only
 CTestPint2View();
 DECLARE_DYNCREATE(CTestPint2View)
2:
CTestPint2View::CTestPint2View()
{ //add
 m_pMemDC = new CDC ;
 m_pBm = new CBitmap;

}

CTestPint2View::~CTestPint2View()
{

 //add
 delete m_pMemDC; //CLEAN UP OUR VARIABLES
 delete m_pBm;
}
/////////////////////////////////////////////////////////////////////////////
// CTestPint2View printing

BOOL CTestPint2View::OnPreparePrinting(CPrintInfo* pInfo)
{
 // default preparation
 return DoPreparePrinting(pInfo);
}

void CTestPint2View::OnBeginPrinting(CDC* pDC, CPrintInfo* /*pInfo*/)
{  //me add
 // TODO: add extra initialization before printing
 if (m_pMemDC->GetSafeHdc()) m_pMemDC->DeleteDC();
 m_pMemDC->CreateCompatibleDC(pDC);

 CClientDC dc(this);
 CRect rect;
 GetClientRect(rect);
 m_pMemDC->SetMapMode(MM_ANISOTROPIC);
 m_pMemDC->SetWindowExt(dc.GetDeviceCaps(LOGPIXELSX),dc.GetDeviceCaps(LOGPIXELSY));
 m_pMemDC->SetViewportExt(m_pMemDC->GetDeviceCaps(LOGPIXELSX),m_pMemDC->GetDeviceCaps(LOGPIXELSY));

 if (m_pBm->GetSafeHandle()) m_pBm->DeleteObject();
 m_pBm->CreateCompatibleBitmap(&dc,rect.Width(),rect.Height());
 m_pMemDC->SelectObject(m_pBm);
 dc.DPtoLP(rect); //Convert to Logical Coordinates
 m_rect = rect; //Save Logical Coordinates
 m_pMemDC->BitBlt(0,0,rect.Width(),rect.Height(),&dc,0,0,SRCCOPY);
 
 //This will prevent creation of the Memory device context
 //and our Bitmap object a second time.
 // TODO: add extra initialization before printing
}

void CTestPint2View::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
 // TODO: add cleanup after printing
}

/////////////////////////////////////////////////////////////////////////////
// CTestPint2View diagnostics

#ifdef _DEBUG
void CTestPint2View::AssertValid() const
{
 CView::AssertValid();
}

void CTestPint2View::Dump(CDumpContext& dc) const
{
 CView::Dump(dc);
}

CTestPint2Doc* CTestPint2View::GetDocument() // non-debug version is inline
{
 ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CTestPint2Doc)));
 return (CTestPint2Doc*)m_pDocument;
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CTestPint2View message handlers

void CTestPint2View::OnPrint(CDC* pDC, CPrintInfo* pInfo)
{
 
 // TODO: add code to print the controls
//The Following code scales the image based on printer resolution.
 pDC->SetMapMode(MM_ANISOTROPIC);
 pDC->SetWindowExt(m_pMemDC->GetDeviceCaps(LOGPIXELSX),m_pMemDC->GetDeviceCaps(LOGPIXELSY));
 pDC->SetViewportExt(pDC->GetDeviceCaps(LOGPIXELSX),pDC->GetDeviceCaps(LOGPIXELSY));
 pDC->StretchBlt(0,0,m_rect.Width(),m_rect.Height(),m_pMemDC,0,0,m_rect.Width(),m_rect.Height(),SRCCOPY);

 /* old
 // TODO: Add your specialized code here and/or call the base class
 CDC *dc=this->GetDC();
 CView::OnPrint(dc, pInfo);
   */
  }

4: 

void TestDrawDlg::OnPrintForm()
{
 CMainFrame *win=(CMainFrame*)AfxGetMainWnd();
 HWND hwin=win->m_hWnd;

 //::SendMessage(hwin,ID_FILE_PRINT,NULL,NULL);
 EndDialog(0);
 // ::SendMessage(hwin,ID_FILE_PRINT_SETUP,NULL,NULL);
 CWinApp *app=::AfxGetApp();
 CTestPint2View *view =  (CTestPint2View*)win->GetActiveView();
 //view->OnFilePrint();
 //CDC *pDC=this->GetDC();
 //view->OnPrint(pDC,NULL);
 view->OnFilePrintPreview();
}
5:but to print dlg ,must move the dlg,otherwise will only print the view:
void TestDrawDlg::OnOK()

{   HWND wnd=this->m_hWnd;
 ::MoveWindow(wnd,0,0,900,900,TRUE);
 Invalidate();
 // TODO: Add extra validation here
 //Invalidate();
 //CDialog::OnOK();
    CPaintDC dc(this);
    CPen MyNewPen;
    COLORREF mm_color=RGB(233,4,5);//dlg.m_color;
    MyNewPen.CreatePen(PS_SOLID,10,mm_color);//RGB(255,0,0)
    CPen* pOriginalPen;
    pOriginalPen=dc.SelectObject(&MyNewPen);
       int m_Radius=45;
    CRect MyRectangle(30,40,20+m_Radius*2,10+m_Radius*2);
    dc.Ellipse(&MyRectangle);
    dc.LineTo(443,444);
   
    dc.SelectObject(pOriginalPen);
   
    //delete dc;

}

posted on 2004-03-30 18:57  abraham  阅读(1032)  评论(2)    收藏  举报