[x]
Posted via EE Mobile

Search, ask, and monitor your questions on the go with EE Mobile. Visit Experts Exchange from your mobile device and never be out of touch again.

Question
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

9.0

Nee some help with my code CPrintDialog (500 Points Grade A) please take a look

Asked by tsp2002 in Microsoft Visual C++.Net

Tags: cprintdialog

This is a code that runs fine. I can select an none default printer with this code.
I also can select the paper size with DEVMODE.
#################################################
void CDrucken_DEVMODEDlg::OnBnClickedButton1()
{
     HANDLE hDevMode;
     HANDLE hDevNames;
       if (GetPrinterDevice(_T("Dell AIO Printer A920"), &hDevNames, &hDevMode))
     //if (GetPrinterDevice(_T("EPSON LQ-680Pro Port 2 Wiegekarte"), &hDevNames, &hDevMode))
     {
         
          AfxGetApp()->SelectPrinter(hDevNames, hDevMode);

          CPrintDialog dlgPrint(FALSE,PD_PAGENUMS|PD_USEDEVMODECOPIESANDCOLLATE , this);

         //--------------------------- Papier Auswahl DEVMODE ----------------------------------
          if(AfxGetApp()->GetPrinterDeviceDefaults(&dlgPrint.m_pd))
          {
              LPDEVMODE dev = dlgPrint.GetDevMode();
              GlobalUnlock(dev);
                            
                                 dev->dmOrientation=DMORIENT_PORTRAIT;
                                  //dev->dmOrientation=DMORIENT_LANDSCAPE;
                                  //dev->dmPaperSize=DMPAPER_A4;
             //dev->dmPaperSize=DMPAPER_A5;
                                       
                    dev->dmPaperLength=1020;          
                    dev->dmPaperWidth=1480;            
                    dev->dmDefaultSource=DMBIN_TRACTOR;
                    dev->dmCopies=2;
                    dev->dmFields |=DM_PAPERLENGTH|DM_PAPERWIDTH|DM_ORIENTATION|DM_DEFAULTSOURCE|DM_COPIES;
              }
         //----------------------------------------------------------------------------
               CFont font;
                 CFont oldfont;
                  
               CString strSchriftart="Comic Sans MS";

             BOOL fRet=font.CreateFont(
                -50,
                 0,
               0,
                 0,
                600,
                 0,
                 0,
                 0,
                DEFAULT_CHARSET,
                OUT_CHARACTER_PRECIS,
                CLIP_CHARACTER_PRECIS,
                DEFAULT_QUALITY,
                DEFAULT_PITCH|FF_DONTCARE,
                strSchriftart);


               if(fRet !=TRUE)
               {
                     MessageBox("Error could not create font");
               }
   
              //----------------------------------------------------------------------------

          dlgPrint.m_pd.Flags &= ~PD_RETURNDC;   // Reset flag set by constructor.
          // Force the CPrintDialog to use our device mode & name.
          dlgPrint.m_pd.hDevMode  = CopyHandle(hDevMode);
          dlgPrint.m_pd.hDevNames = CopyHandle(hDevNames);
          //===============================================
              if(dlgPrint.DoModal()==IDOK)
            {
               GlobalFree(hDevMode);
               GlobalFree(hDevNames);
               hDevMode  = CopyHandle(dlgPrint.m_pd.hDevMode);  // Save new copies.
               hDevNames = CopyHandle(dlgPrint.m_pd.hDevNames); // Save new copies.
         
                     //Use CreatePrinterDC() instead GetPrinterDC(). Like this

               HDC hDCPrint = dlgPrint.CreatePrinterDC();
               DOCINFO Print;
               memset( &Print, 0, sizeof(DOCINFO) );
               Print.cbSize = sizeof (DOCINFO);
               Print.lpszDocName = "Drucken im Dialog";
               Print.lpszOutput = (LPTSTR) NULL;
               Print.lpszDatatype = (LPTSTR) NULL;
               Print.fwType = 0;
        

               if(StartDoc(hDCPrint,&Print)>=0)
               {
                    StartPage(hDCPrint);    
                               
                              SetMapMode(hDCPrint,MM_LOMETRIC);
                    int nLaenge = GetDeviceCaps(hDCPrint,VERTSIZE);
                        int nBreite = GetDeviceCaps(hDCPrint,HORZSIZE);
                              int nRechts = nBreite * 10 -50;
                        int nUnten = -nLaenge * 10 +50;
                                     

                              CString A="Urlaubsantrag";
                              //-----------------------------------
                              ::SelectObject(hDCPrint,(HFONT)font);
                    //-----------------------------------

                   CPrintInfo* pInfo=(CPrintInfo*)this;
                           int page=0;
                           
                         page=pInfo->m_nCurPage;
                           CString str_page;
                           str_page.Format("%d",page);

                   TextOut(hDCPrint,100,-10,str_page,strlen(str_page));


                              //strlen(A) Länge vom String
                              //TextOut(hDCPrint,100,200,"Hallo",5) geht so auch muß nur immer Länge zählen
                         //------------------------------------------------------------
                              ::TextOut(hDCPrint,100,-50, A,strlen(A));  
                     
                              
                              font.DeleteObject();::SelectObject(hDCPrint,(HFONT)oldfont);
                          TextOut(hDCPrint,100,-120,"=============",13);

                           A="Name : ...............       Vorname : ..........";
                         TextOut(hDCPrint,100,-200, A,strlen(A));
                           A="vom  : ...............           bis : ..........";
                   TextOut(hDCPrint,100,-400, A,strlen(A));
                           A="Datum: ........   Unterschrift: ...............";
                         TextOut(hDCPrint,100,-600, A,strlen(A));
                           A="genehmigt:                       Datum: .........";
                         TextOut(hDCPrint,100,-800, A,strlen(A));
                        //------------------------------------------------------------------
                                         
                         
                    EndPage(hDCPrint);

                      
                    EndDoc(hDCPrint);

                        
                    DeleteDC(hDCPrint);
               }
            }
            GlobalFree(dlgPrint.m_pd.hDevMode);   // Because DoModal was called,
          GlobalFree(dlgPrint.m_pd.hDevNames);  // we need to free these.
       }
}
#################################################
Here is my new code. It has 3 functions:

void OnBeginnPrinting(CDC* pDC, CPrintInfo* pInfo);
void OnPrint(CDC* pDC, CPrintInfo* pInfo);
void OnEndPrinting((CDC* pDC, CPrintInfo* pInfo);

This code below works fine. I would like to change this code that I can select an default printer like the code above with HDC. How can I convert/change the HDC to CDC???

How can I do this. Please help...I am a beginner !
#################################################
void CDrucken_DEVMODEDlg::OnBeginPrinting(CDC* pDC, CPrintInfo* pInfo)
{
      //Druckbereich in 0,1mm
      pDC->SetMapMode(MM_LOMETRIC);
      pInfo->SetMaxPage(1);

      //--------------- Neue Schrift erstellen ------------------
      ASSERT(m_fontUeberschrift==0);
      m_fontUeberschrift= new CFont;
      ASSERT(m_fontUeberschrift);

        m_fontUeberschrift->CreateFont(-50,
                                         0,
                                       0,
                                         0,
                                         600,
                                         0,
                                            0,
                                         0,
                                         DEFAULT_CHARSET,
                                         OUT_CHARACTER_PRECIS,
                                         CLIP_CHARACTER_PRECIS,
                                         DEFAULT_QUALITY,
                                         DEFAULT_PITCH|FF_DONTCARE,
                                         "Arial");
    //---------------------------------------------------------    

}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
void CDrucken_DEVMODEDlg::OnPrint(CDC* pDC, CPrintInfo* pInfo)
{                    
                      //Druckbereich in 0,1mm
                      pDC->SetMapMode(MM_LOMETRIC);
                                    

                           CString A;
                           CString str;
                           A.Format("Page: %d",m_currentPageNo);

                           CFont* pOldFont = pDC->SelectObject(m_fontUeberschrift);
                           pDC->TextOut(100,-200,"Dies ist meine Überschrift");

                           pDC->SelectObject(pOldFont);
                           pDC->TextOut(100,-50,A);

                           //-----aktuelle Seite +1 -----------
                           m_currentPageNo=m_currentPageNo+1;
                          //---------------------------------
}
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
void CDrucken_DEVMODEDlg::OnEndPrinting(CDC* pDC, CPrintInfo* pInfo)
{
      delete m_fontUeberschrift;
      m_fontUeberschrift=NULL;
}
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%



//*****************************************************
//*****************************************************
//*****************************************************
void CDrucken_DEVMODEDlg::OnBnClickedButton2()
{
      HANDLE hDevMode;
      HANDLE hDevNames;

      // if (GetPrinterDevice(_T("Dell AIO Printer A920"), &hDevNames, &hDevMode))  
       //{
      //       AfxGetApp()->SelectPrinter(hDevNames, hDevMode);
         
      
   

               CPrintDialog dlgPrint(FALSE,PD_PAGENUMS|PD_USEDEVMODECOPIESANDCOLLATE,this);

             //xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
                   if(dlgPrint.DoModal()==IDOK)
                   {
                     CDC dc;
                     dc.Attach(dlgPrint.GetPrinterDC());
                 dc.m_bPrinting=TRUE;

              //---------------------------------------------------------------------------------
              LPDEVMODE dev=dlgPrint.GetDevMode();
                GlobalUnlock(dev);
                
              dev->dmOrientation=DMORIENT_LANDSCAPE; //Querformat
              //  dev->dmOrientation=DMORIENT_PORTRAIT;  //Hochformat
              //  dev->dmPaperSize=DMPAPER_A4;
              //  dev->dmPaperSize=DMPAPER_A5;
                dev->dmPaperLength=1480;
                dev->dmPaperWidth=1020;
                dev->dmCopies=1;  
                dev->dmDefaultSource=DMBIN_TRACTOR;
     
              
             dev->dmFields |=DM_PAPERLENGTH|DM_PAPERWIDTH|DM_ORIENTATION|DM_DEFAULTSOURCE;
                            dc.ResetDC(dev);

                   dlgPrint.m_pd.Flags &= ~PD_RETURNDC;   // Reset flag set by constructor.
                  
                    
                    //------------------------------------
                      DOCINFO Print;
               memset( &Print, 0, sizeof(DOCINFO) );
               Print.cbSize = sizeof (DOCINFO);
               Print.lpszDocName = "Drucken im Dialog";
               Print.lpszOutput = (LPTSTR) NULL;
               Print.lpszDatatype = (LPTSTR) NULL;
               Print.fwType = 0;
                //-----------------------------------
             
                        
                     BOOL bPrintingOK = dc.StartDoc(&Print);
                 CPrintInfo pInfo;
                 pInfo.m_rectDraw.SetRect(0,0,dc.GetDeviceCaps(HORZRES), dc.GetDeviceCaps(VERTRES));
                 
               //~~~~~~~~~~~~~~~~~~~~~~~~~
                 OnBeginPrinting(&dc,&pInfo);  // call prepare
               //~~~~~~~~~~~~~~~~~~~~~~~~~


               //=========================================
                 for(UINT page=pInfo.GetMinPage();page<=pInfo.GetMaxPage() &&bPrintingOK ; page++)
               {
                   //begin a new page
                       dc.StartPage();
                   pInfo.m_nCurPage=page;
                   //~~~~~~~~~~~~~~~~~~~~~~~~~
                     OnPrint(&dc,&pInfo); // call Print your Page
                   //~~~~~~~~~~~~~~~~~~~~~~~~~
                     bPrintingOK=(dc.EndPage()>0);
                 }
               //===========================================

                     //~~~~~~~~~~~~~~~~~~~~~~~~~
                   OnEndPrinting(&dc,&pInfo); // call your clean up
                   //~~~~~~~~~~~~~~~~~~~~~~~~~
                        if(bPrintingOK)
                           {
                      dc.EndDoc();
                           }
                           else
                           {
                                 dc.AbortDoc();
                           }
                     
                           dc.DeleteDC();

                }
                  //xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

         // GlobalFree(dlgPrint.m_pd.hDevMode);   // Because DoModal was called,
          //GlobalFree(dlgPrint.m_pd.hDevNames);  // we need to free these.

}
#################################################

Thank you very much
Best regards,
Thomas





[+][-]08/18/06 05:59 AM, ID: 17341842Accepted Solution

View this solution now by starting your 30-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

About this solution

Zone: Microsoft Visual C++.Net
Tags: cprintdialog
Sign Up Now!
Solution Provided By: lakshman_ce
Participating Experts: 1
Solution Grade: A
 
[+][-]08/21/06 12:16 AM, ID: 17354028Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]08/21/06 06:57 AM, ID: 17355843Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]08/22/06 06:09 AM, ID: 17363385Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]08/22/06 07:46 PM, ID: 17369004Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]08/23/06 05:40 AM, ID: 17371931Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
 
Loading Advertisement...
20091111-EE-VQP-89