• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 601
  • Last Modified:

Changing the print font (SIZE) on a MFC Dialog app.

Background info:
I have created a dialog application and in the application the user has an option to print.  I have created a button on the dialog that when double clicked, the function for printing some text info will be printed.  In the function for printing I want to change the font-Size, type, etc. but i am having problems with changing the font. The text will print out to the HP laser 4 printer,but regardless of what font type I create using CFont.Create,  the
font never changes.  

MY QUESTION:

How do I change the font in a dialog application using CFont and CDC since their is no CView class or OnDraw(CDC* pDC) function.

Thanks,

ARG1
0
arg1
Asked:
arg1
  • 2
  • 2
1 Solution
 
chensuCommented:
You need to select the font object into the printer DC before printing.

CFont MyFont;
MyFont.CreatePointFont(160, _T("Arial"), &PrinterDC);

CFont *pOldFont = PrinterDC.SelectObject(&MyFont);

// PrinterDC.TextOut ...

PrinterDC.SelectObject(pOldFont);
0
 
arg1Author Commented:
Hello Chensu,

Here is a copy of my printing code.
It will print to the default printer, but the font will not change.  Take a look and let me know what you think.  My system is a win95 box.

Thanks,

ARG1

void CTestDlg::OnDoubleclickedButton1()
{
      
      CPrintDialog dlg(FALSE);//Creates a print dialog box
            
      CFont* pOldFont;
      CFont Cfont;
      CSize pagesize;
      CDC PrintDC;// printer device context
      DOCINFO info;//structure for the descriptive name of document to be printed
      BOOL value;
      
        memset(&info,0,sizeof(info));
        info.cbSize=sizeof(info);//The size, in bytes, of the stream or byte array.
        info.lpszDocName=_T("Test Report");//document name

            // Retrieve the current printer defaults from the Windows .ini file
    // as necessary, or uses the last printer configuration set by the
   // user in Print Setup.
   CWinApp* app = AfxGetApp();
   if (!app->GetPrinterDeviceDefaults(&dlg.m_pd))
    {
    // Fails to create a printer device context for printing because
    // no printer is being installed. Bring up dialog box to alert the
    // users that they need to install a printer.  This step is optional.
    if (app->DoPrintDialog(&dlg) != IDOK)
       return ;
    }

    // Creates a printer DC from the DEVMODE and DEVNAMES structures.
    if (dlg.CreatePrinterDC() == NULL) return ;

    //value=Cfont.CreateFont(-120,0,0,0,FW_NORMAL,FALSE,FALSE,0,ANSI_CHARSET,OUT_DEFAULT_PRECIS,CLIP_DEFAULT_PRECIS,DEFAULT_QUALITY,DEFAULT_PITCH|FF_SWISS,"Arial");
      //ASSERT(value);
      PrintDC.Attach(dlg.m_pd.hDC);//Obtain print device context
      
      Cfont.CreatePointFont(160,_T("Arial"),&PrintDC);
      pOldFont=PrintDC.SelectObject(&Cfont);
      
      //pagesize.cx=PrintDC.GetDeviceCaps(PHYSICALWIDTH);//RETURNS PRINTER PAGEWIDTH IN PIXELS
      //pagesize.cy=PrintDC.GetDeviceCaps(PHYSICALHEIGHT);//RETURNS PRINTER PAGE HEIGHT IN PIXELS
      PrintDC.StartDoc(&info);
      PrintDC.StartPage();
      
      PrintDC.TextOut(100,500,_T("FOOG"),4);
      
      PrintDC.EndPage();
      PrintDC.EndDoc();
      PrintDC.SelectObject(pOldFont);
      

}
0
 
chensuCommented:
Put SelectObject right before TextOut because StartPage resets the attributes of the DC.

PrintDC.StartPage();

pOldFont=PrintDC.SelectObject(&Cfont);
PrintDC.TextOut(100,500,_T("FOOG"),4);
PrintDC.SelectObject(pOldFont);

PrintDC.EndPage();
0
 
arg1Author Commented:
YOU ARE A GDI GURU,

Thanks for the insight.  I suspected the attributes, but I just did not look.  

Thanks again,

ARG1
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now