It might be easier if somebody just shot me
-
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
-
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
Check out GetTextExtentPoint32() API function. I've a strong feeling this is what you need. -Shanker.
-
Check out GetTextExtentPoint32() API function. I've a strong feeling this is what you need. -Shanker.
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