Code showing How to print a dialog widow...

Discussion in 'MFC' started by 燕山剑, May 29, 2007.

  1. 燕山剑

    燕山剑 New Member

    Joined:
    May 31, 2007
    Messages:
    7
    Likes Received:
    0
    Trophy Points:
    0
    Location:
    爱国者同盟 Chinese Patriot League
    Home Page:
    http://projects.takingitglobal.org/tds
    Code showing How to print a dialog...

    Code:
    LONG CMyOcx::PrintAAA(SHORT pagenumber)
    {
    	AFX_MANAGE_STATE(AfxGetStaticModuleState());
    
        // Instantiate a CPrintDialog.
        CPrintDialog *prtDlg =
                  new CPrintDialog(FALSE, PD_PAGENUMS | PD_NOSELECTION |  PD_RETURNDC, NULL);
    
        // Initialize some of the fields in PRINTDLG structure.
        	if(pagenumber < 0) 
    	{
          prtDlg->m_pd.nMinPage = prtDlg->m_pd.nFromPage = 1;
          prtDlg->m_pd.nMaxPage = prtDlg->m_pd.nToPage = m_UFormBook.GetPageCount();
    	}
    	else
    	{
          prtDlg->m_pd.nMinPage = prtDlg->m_pd.nFromPage = pagenumber+1;
          prtDlg->m_pd.nMaxPage = prtDlg->m_pd.nToPage = min(pagenumber+1,m_UFormBook.GetPageCount());
    	}
    
    	if (prtDlg->DoModal() == IDCANCEL)     // Get printer settings from user
            return NO_ERROR;
    
       // Determine the starting and ending page numbers for the range 
       // of pages to be printed.
       int from_page, to_page;
       if (prtDlg->PrintAll())              // print all pages in the document
       {
          from_page = prtDlg->m_pd.nMinPage;
          to_page = prtDlg->m_pd.nMaxPage;
       }
       else if (prtDlg->PrintRange())       // print only a range of pages 
       {                                // in the document 
          from_page = prtDlg->GetFromPage();
          to_page = prtDlg->GetToPage();
       }
       else if (prtDlg->PrintSelection())   // print only the currently selected 
                                        // items
       {
          from_page = to_page = -1;     // -1 to denote unknown yet
       }
       from_page--;
    
    	CDC dcDMJ;
    
        CString strTitle;                       // Get the application title
        strTitle.LoadString(IDS_UFILLER);
    	
    	DOCINFO oeDI;
    	memset(&oeDI, 0, sizeof(DOCINFO));
    	oeDI.cbSize = sizeof(DOCINFO);
        oeDI.lpszDocName = strTitle;
    
    	int ufmLogPPI  =96;
    
    	dcDMJ.Attach(prtDlg->GetPrinterDC());     // Get and attach a printer DC
        dcDMJ.m_bPrinting = TRUE;
    
    	int paper_oewidth ;
    	int paper_oeheight;
    
    	int xLogPPI;
    	int yLogPPI;
    
    	 paper_oewidth  = dcDMJ.GetDeviceCaps(PHYSICALWIDTH);
    	 paper_oeheight = dcDMJ.GetDeviceCaps(PHYSICALHEIGHT);
    
    	 xLogPPI = dcDMJ.GetDeviceCaps(LOGPIXELSX);
    	 yLogPPI = dcDMJ.GetDeviceCaps(LOGPIXELSY);
       
    	// Variable needed to store printer info.
        CSize PrintOffset;
    
    	if(dcDMJ.StartDoc(&oeDI) == -1)
    	{
    		return ERROR_SPL_NO_STARTDOC; 
    	}
    
    	m_blPrinting =true;
    		
    	CString strKey;
    	CUPageDlg *iepDlg;
    
    	LPDEVMODE pDevMode=NULL;
    
    	   paper_c_oewidth =paper_oewidth;
    	   paper_c_oeheight =paper_oeheight;
    
    	   x_c_LogPPI =xLogPPI;
    	   y_c_LogPPI =yLogPPI;
    
    	//draw bmpoeAll here, or it can be done in another function
        for (UINT page= from_page;page < to_page; page++)
        {
    	   iepDlg =m_UFormBook.GetPageByIndex(page,true);
    	   if(iepDlg->m_bPrintProtect)
    	   {
    			continue;
    	   }
    
    	 //calculate pages
    	   if((iepDlg->formExtproperties->m_nPageWidth  >iepDlg->formExtproperties->m_nPageHeight)  != (paper_c_oewidth > paper_c_oeheight)) 
    	   {		
    		   pDevMode = prtDlg->GetDevMode();
    		   if(! pDevMode)
    		   {
    			   return ERROR_SPL_NO_STARTDOC; 
    		   }
      
    		   pDevMode->dmOrientation = iepDlg->formExtproperties->m_nPageWidth  >iepDlg->formExtproperties->m_nPageHeight ?
    										DMORIENT_LANDSCAPE : DMORIENT_PORTRAIT;
    
    		   dcDMJ.ResetDC(pDevMode);
     
    		   paper_oeheight =paper_c_oewidth;		
    		   paper_c_oewidth =paper_c_oeheight;		
    		   paper_c_oeheight =paper_oeheight;
    
    		   x_c_LogPPI =yLogPPI;
    		   y_c_LogPPI =xLogPPI;
    	   }
         	  
    	   ratio_c_oex = (double) x_c_LogPPI / (double) ufmLogPPI;
    	   ratio_c_oey = (double) y_c_LogPPI / (double) ufmLogPPI;
    
    	   if(iepDlg->formExtproperties->m_nPageWidth  * ratio_c_oex   / paper_c_oewidth  < 1.1  &&
    	      iepDlg->formExtproperties->m_nPageHeight * ratio_c_oey   / paper_c_oeheight < 1.1 )
    	   {		
         	dcDMJ.SetMapMode(MM_ISOTROPIC);
       	    dcDMJ.SetWindowExt( ufmLogPPI, ufmLogPPI);
    	    dcDMJ.SetViewportExt( x_c_LogPPI, y_c_LogPPI );
    
    		dcDMJ.StartPage();
    	
    		iepDlg->OnEraseBkgnd( &dcDMJ );
    		iepDlg->PrintDlg1( &dcDMJ);
    			
    		dcDMJ.EndPage();
    	   }
    	}
    
    	m_blPrinting =false;
    
    	dcDMJ.EndDoc();
    	dcDMJ.Detach();
    	dcDMJ.DeleteDC();
    
    	return NO_ERROR;
    }
    ...
    LONG CMyDlg::PrintDlg1(CDC* prdc4)
    {
    	CRect rectClient,rectDst;
    	CWnd* pwndChild = GetWindow(GW_CHILD);	
     
    	CUFBaseShape* pBaseShape;
     
    	while (pwndChild)
        {
    		pBaseShape = dynamic_cast<CUFBaseShape*>(pwndChild);
             
    		if(pBaseShape)  
    		{
    			pwndChild->SendMessage(WM_PRINT,(WPARAM)((CDC*)prdc4)->m_hDC,0);
    	    }
    
    		 pwndChild = pwndChild->GetNextWindow();
          }
    						
    	return 0;
    }
    ...
    
    LRESULT CMyEdit:: OnPrint(WPARAM wwdc, LPARAM lParam)
    {
    	if(GetCompBase()->IsPrintProtect())
    	{
    		return 0;
    	}
    	UpdateData();
    
        CDC* iepDC = CDC::FromHandle((HDC)wwdc);
    
    	CRect rUIP=m_rectSizeAndPosition;
    	rUIP.InflateRect(-m_nFrameWidth, -m_nFrameWidth);
    	iepDC->FillSolidRect(rUIP,RGB(255,255,255));         //m_compBase.GetBkColor());
    
    	CString str6Text;
    	GetWindowText(str6Text);
    
    	if(str6Text.GetLength())
    	{
    	 // Ensure the printer DC is in MM_TEXT mode.
    	  int nSaveDC = iepDC->SaveDC();
    	  iepDC->SetMapMode ( MM_TEXT );
    
    	  UF2Rect( rUIP, ((CMyOcx*)(GetParent()->GetParent()))->ratio_oex);
    
    	  if(! m_compCommon.GetMultiLine()) 
    	  {
    	    CSize Extent = iepDC->GetTextExtent(str6Text);
    		int int6Text =rUIP.CenterPoint().y;
    
    		rUIP.top =int6Text - Extent.cy/2;
    		rUIP.bottom =int6Text + Extent.cy/2;
    	  }
    
    	  // This code basically taken from MS KB article Q129860
    	  FORMATRANGE frIP;
    
    	 // Rendering to the same DC we are measuring.
    	  ZeroMemory(&frIP, sizeof(frIP));
    	  frIP.hdc = frIP.hdcTarget = (HDC)wwdc;
    
       // Set up the page (convert 0.01mm to twips)
    	  frIP.rcPage.left     = (rUIP.left * 1440/((CMyOcx*)(GetParent()->GetParent()))->xLogPPI);
    	  frIP.rcPage.top      = (rUIP.top * 1440/((CMyOcx*)(GetParent()->GetParent()))->yLogPPI) ;
    	  frIP.rcPage.right    = (rUIP.right * 1440/((CMyOcx*)(GetParent()->GetParent()))->xLogPPI);
    	  frIP.rcPage.bottom   = (rUIP.bottom * 1440/((CMyOcx*)(GetParent()->GetParent()))->yLogPPI) ;
    
    	 // Set up 0" margins all around.
    	  frIP.rc.left   = frIP.rcPage.left ;//+ 1440;  // 1440 TWIPS = 1 inch.
    	  frIP.rc.top    = frIP.rcPage.top ;//+ 1440;
    	  frIP.rc.right  = frIP.rcPage.right ;//- 1440;
    	  frIP.rc.bottom = frIP.rcPage.bottom ;//- 1440;
    
    	 // Default the range of text to print as the entire document.
    	  frIP.chrg.cpMin = CharFromPos(CPoint(0,0));
    	  CRect rc5Bound;
    	  GetClientRect(&rc5Bound);
    	  frIP.chrg.cpMax = CharFromPos(rc5Bound.BottomRight());
    	  if(frIP.chrg.cpMax ==0 )  frIP.chrg.cpMax =-1;
    
          FormatRange(&frIP,true);
    
    	    // Print as much text as can fit on a page. The return value is
    	    // the index of the first character on the next page. Using TRUE
    	    // for the wParam parameter causes the text to be printed.
    	    FormatRange(&frIP,true);
    		DisplayBand(&frIP.rc );
    
    	 // Tell the control to release cached information.
    	  FormatRange(NULL,false);
    	  iepDC->RestoreDC(nSaveDC);
    	}
    
        rUIP=m_rectSizeAndPosition;
    
    	if(m_compBase.IsFlat()) 
    	{
    		int nWidth = m_compBorder.GetBorderWidth();
    
    		CPen penBorder(PS_NULL,0,RGB(0,0,0));
    		CPen* pOldPen = iepDC->SelectObject(&penBorder);
            CBrush brushBk(RGB(255,255,255));         //m_compBase.GetBkColor());
    		CBrush* pOldBrush = iepDC->SelectObject(&brushBk);
    
    
    
    
    
    
    		iepDC->Rectangle(rUIP.left,rUIP.top,rUIP.right+1,rUIP.top+nWidth+1);
    		iepDC->Rectangle(rUIP.right-nWidth,rUIP.top,rUIP.right+1,rUIP.bottom+1);
    		iepDC->Rectangle(rUIP.left,rUIP.bottom-nWidth,rUIP.right+1,rUIP.bottom+1);
    		iepDC->Rectangle(rUIP.left,rUIP.top,rUIP.left+nWidth+1,rUIP.bottom+1);
    
    		CBrush brushBorder(m_compBorder.GetBorderColor());
    		iepDC->SelectObject(&brushBorder);
    		CRect rcTotal = rUIP;
    
    		if ( m_compBorder.GetBorderStyle() == BORDERSTYLETYPE_FLAT )
    		{
    			if (m_compBorder.IsHaveBorderTop() )
    			{
    				iepDC->Rectangle(rUIP.left,rUIP.top,rUIP.right+1,rUIP.top+nWidth+1);
    			}
    			if ( m_compBorder.IsHaveBorderRight() )
    			{
    				iepDC->Rectangle(rUIP.right-nWidth,rUIP.top,rUIP.right+1,rUIP.bottom+1);
    			}
    			if (m_compBorder.IsHaveBorderDown() )
    			{
    				iepDC->Rectangle(rUIP.left,rUIP.bottom-nWidth,rUIP.right+1,rUIP.bottom+1);
    			}
    			if (m_compBorder.IsHaveBorderLeft() )
    			{
    				iepDC->Rectangle(rUIP.left,rUIP.top,rUIP.left+nWidth+1,rUIP.bottom+1);
    			}
    		}
    
    		iepDC->SelectObject(pOldPen);
    		iepDC->SelectObject(pOldBrush);
    	}
    	else
    	{
    		CRect rcTotal = rUIP;
    
    
    
    
    
    
    
    		if ( m_compBorder.GetBorderStyle() == BORDERSTYLETYPE_SUNKEN )
    		{
    			iepDC->Draw3dRect(rcTotal,RGB(128,128,128),RGB(255,255,255));
    			rcTotal.InflateRect(-1,-1,-1,-1);
    			iepDC->Draw3dRect(rcTotal,RGB(0,0,0),gfxData.m_crBtnFace);
    		}
    		else 
    		{
    			iepDC->Draw3dRect(rcTotal,RGB(255,255,255),RGB(0,0,0));
    			rcTotal.InflateRect(-1,-1,-1,-1);
    			iepDC->Draw3dRect(rcTotal,gfxData.m_crBtnFace,RGB(128,128,128));
    		}
    	}
    	return 0;
    }
     
  2. NewsBot

    NewsBot New Member

    Joined:
    Dec 2, 2008
    Messages:
    1,267
    Likes Received:
    2
    Trophy Points:
    0
    Looks like an advance MFC then Win32. Moved to Advance MFC forum.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice