Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. It might be easier if somebody just shot me

It might be easier if somebody just shot me

Scheduled Pinned Locked Moved C / C++ / MFC
comhelptutorial
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    Lost User
    wrote on last edited by
    #1

    Back to my old chestnut of printing from a dialog based app. Got the text printing on a page using CDC::TextOut(), after setting the logical units to physical units using CDC::SetMapMode. What I want to be able to do is select the Font I am using to print, and determine the dimensions of a given string in that font using the same units so I can determine how many lines will fit on a page as well as a line. I can't find any of these functions in the online help, the MDI/SDI printing code generated doesn't help and I can't find any demos lying around the Internet on how to do this correctly from Dialogs. Can somebody help with an answer or a gun (I'm in Australia, if you read the thread in the Lounge earlier in the week you would understand. Michael Martin Pegasystems Pty Ltd Australia martm@pegasystems.com +61 413-004-018

    S 1 Reply Last reply
    0
    • L Lost User

      Back to my old chestnut of printing from a dialog based app. Got the text printing on a page using CDC::TextOut(), after setting the logical units to physical units using CDC::SetMapMode. What I want to be able to do is select the Font I am using to print, and determine the dimensions of a given string in that font using the same units so I can determine how many lines will fit on a page as well as a line. I can't find any of these functions in the online help, the MDI/SDI printing code generated doesn't help and I can't find any demos lying around the Internet on how to do this correctly from Dialogs. Can somebody help with an answer or a gun (I'm in Australia, if you read the thread in the Lounge earlier in the week you would understand. Michael Martin Pegasystems Pty Ltd Australia martm@pegasystems.com +61 413-004-018

      S Offline
      S Offline
      Shankar Chandra Bose
      wrote on last edited by
      #2

      Check out GetTextExtentPoint32() API function. I've a strong feeling this is what you need. -Shanker.

      D 1 Reply Last reply
      0
      • S Shankar Chandra Bose

        Check out GetTextExtentPoint32() API function. I've a strong feeling this is what you need. -Shanker.

        D Offline
        D Offline
        Derek Lakin
        wrote on last edited by
        #3

        I haven't had chance to look at checking the text extent, but I think Shanker has helped there. The following example shows printing a title in centred, bold, 16 point, Times New Roman and then some other text left-aligned, 8 point, Courier New. The example is the message handler for a Print button on a dialog. Hopefully this should help with your font problem :)

        void CPrintTestDlg::OnButtonPrint()
        {
        // Ask the user which printer they want to use
        CPrintDialog dlg (FALSE);
        if (IDOK != dlg.DoModal())
        return;

        CWaitCursor wait; // Show a busy cursor while printing
        
        // Get the dialog to create a printer DC for this printer
        HDC printerDC = dlg.CreatePrinterDC();
        
        // Create a CDC connected to this HDC
        CDC	dc;
        dc.Attach( printerDC );
        
        // Determine the printable area of the page
        CPoint pt( GetDeviceCaps(dc.m\_hDC, HORZRES),
        		   GetDeviceCaps(dc.m\_hDC, VERTRES) );
        dc.DPtoLP( &pt );
        CRect rectPage( 0, 0, pt.x, pt.y );
        
        // Create the document information
        CString strTitle = "Document Title";
        DOCINFO docInfo;
        docInfo.cbSize = sizeof(DOCINFO);
        docInfo.lpszDocName = strTitle; 
        docInfo.lpszOutput = 0; 
        docInfo.lpszDatatype = 0;
        
        // Start the doc and page
        dc.StartDoc( &docInfo );
        dc.StartPage( );
        
        // Print out some text in our chosen font
        int currentY = 0; // Maintain our current position down the page
        // Specify a LOGFONT for the font we require
        LOGFONT\* pLogFont = new LOGFONT;
        pLogFont->lfCharSet = ANSI\_CHARSET;
        pLogFont->lfClipPrecision = CLIP\_DEFAULT\_PRECIS;
        pLogFont->lfEscapement = 0;
        CString strFaceName = "Times New Roman"; // Change font name here to whatever you need
        lstrcpy (pLogFont->lfFaceName, strFaceName);
        // Calculation allows us to specify the font size in points
        pLogFont->lfHeight = -MulDiv (16, GetDeviceCaps (dc.m\_hDC, LOGPIXELSY), 72); // 16 point font
        pLogFont->lfItalic = FALSE;
        pLogFont->lfOrientation = 0;
        pLogFont->lfOutPrecision = OUT\_DEFAULT\_PRECIS;
        pLogFont->lfPitchAndFamily = DEFAULT\_PITCH | FF\_DONTCARE;
        pLogFont->lfQuality = DEFAULT\_QUALITY;
        pLogFont->lfStrikeOut = FALSE;
        pLogFont->lfUnderline = TRUE;
        pLogFont->lfWeight = FW\_BOLD;
        pLogFont->lfWidth = 0;
        
        // Create and select the font
        CFont\* pFont = new CFont;
        pFont->CreateFontIndirect( pLogFont );
        CFont\* pOldFont = dc.SelectObject( pFont );
        // Centre alignment
        dc.SetTextAlign( TA\_CENTER );
        dc.TextOut( rectPage.Width() / 2, rectPage.top, strTitle ); // Display title line
        // Update our position down the page
        currentY += abs (pL
        
        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups