Solved

Add a font to a textbox and size the font according to the textbox's height

Posted on 2008-10-06
8
476 Views
Last Modified: 2013-12-05
I am using eVC along with MFC for Windows CE 5.0. I would like to be able to resize a textbox and have the font resize along with it.

I already know how to change a font for a textbox, what I need specifically is some code that will create a font to fit the dimensions of the textbox. At the moment, I basically keep trying different point sizes until the text fits. There has to be another way.

Thank you in advance.
0
Comment
Question by:Anthony2000
  • 4
  • 4
8 Comments
 
LVL 19

Expert Comment

by:alb66
Comment Utility
I never found another way.
Usually I start with a big font and then reduce it point by point until it fit the textbox. Of course you must do it programmatically.
How to retrieve the font height:
	m_oFont.CreateFont(  -25,

								0,

								0,

								0,

								FW_SEMIBOLD,

								0,

								0,

								0,

								ANSI_CHARSET,

								OUT_STROKE_PRECIS,

								CLIP_STROKE_PRECIS,

								0,

								FIXED_PITCH,

								"Courier New" );

	m_oEdit.SetFont( &m_oFont );

 

	// ... le dimensioni...

	CDC* pDC = m_oEdit.GetDC();

	CFont* pFont = pDC->SelectObject( &m_oFont );

	TEXTMETRIC tm;

	pDC->GetTextMetrics( & tm );

	LONG cy = tm.tmHeight;

Open in new window

0
 
LVL 8

Author Comment

by:Anthony2000
Comment Utility
I guess that I could create a lookup table when the program starts-up or in the OnInitDialog function. I am surprised that there is no other way.
0
 
LVL 8

Author Comment

by:Anthony2000
Comment Utility
I looked at http://msdn.microsoft.com/en-us/library/2ek64h34(VS.80).aspx quickly, and it appears that you can specify the height and the create font function is supposed to find the closest match. Alb66, are you familiar with this?
0
 
LVL 19

Expert Comment

by:alb66
Comment Utility
<<< to find the closest >>
It means that when you call CFont::Create the font mapper may be unable to create exactly what you specified with the parameters. For example, if the font has only some fixed size, if you specified a not available heigth, the font mapper choose one size from the avilable sizes.
( I hope you can understand... this concept is quite complex for my english...)
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 19

Accepted Solution

by:
alb66 earned 500 total points
Comment Utility
<<< ... I could create a lookup table... >>>
No, you haven't to create a lookup table. In your OnInitDialog() you can create a loop that search for the font.
It should be something like the following ( you must define a CFont member variable called m_oFont):
BOOL CYourDlg::OnInitDialog()

{

	CDialog::OnInitDialog();
 

	CRect rc;
 

	CWnd* pWnd = GetDlgItem( IDC_EDIT1 );  <--- the ID of your textbox

	pWnd->GetClientRect( &rc );

	int nHeight = rc.Height();             <--- the height of the textbox
 

	int nFontHeight = -50;                 <--- the initial size

	

	CDC* pDC = pWnd->GetDC();
 

	while( 1 )

	{

       m_oFont.CreateFont( nFontHeight,

                           0,

                           0,

                           0,

                           FW_SEMIBOLD,

                           0,

                           0,

                           0,

                           ANSI_CHARSET,

                           OUT_STROKE_PRECIS,

                           CLIP_STROKE_PRECIS,

                           0,

                           FIXED_PITCH,

                           "Courier New" );

 

        pDC->SelectObject( &m_oFont );

        TEXTMETRIC tm;

        pDC->GetTextMetrics( & tm );

        LONG cy = tm.tmHeight;
 

		  if ( cy < nHeight )           <--- if the font height is less than the control height we have finished

		  {

	        pWnd->SetFont( &m_oFont );

			  break;

		  }
 

		  nFontHeight++;                <--- otherwise we decrease the font size

		  m_oFont.DeleteObject();

	}
 
 
 
 

	return TRUE;  // return TRUE unless you set the focus to a control

	// ECCEZIONE: le pagine delle proprietà OCX devono restituire FALSE

}

Open in new window

0
 
LVL 8

Author Comment

by:Anthony2000
Comment Utility
Ciao alb66,

I have tried it and it works. I also did the following and this appears to work as well, but I don't understand why? I don't fully understand Microsoft's documentation on CreateFont.

Specifically, I don't understand the difference between passing  in a negative height versus a positive value. Can you explain?

Ho visto che lei abita in Italia. Non scrivo molto bene, ma ho pensato di fare una prova. Grazie per tua aiuta.

BOOL CFontTestDlg::OnInitDialog()

{

    CDialog::OnInitDialog();
 

    // Set the icon for this dialog.  The framework does this automatically

    //  when the application's main window is not a dialog

    SetIcon(m_hIcon, TRUE);			// Set big icon

    SetIcon(m_hIcon, FALSE);		// Set small icon

	

    CenterWindow(GetDesktopWindow());	// center to the hpc screen
 

    // TODO: Add extra initialization here

    CString x;

    CRect rc;

 

    //CWnd* pWnd = GetDlgItem( IDC_EDIT1 );  //<--- the ID of your textbox

    //pWnd->GetClientRect( &rc );
 

    m_my_edit.GetClientRect(&rc);

    int nHeight = rc.Height();             //<--- the height of the textbox

 

    int nFontHeight = -50;                 //<--- the initial size

       

    //CDC* pDC = pWnd->GetDC();

    CDC* pDC = m_my_edit.GetDC();

 

    while( 1 )

    {

	x.Format(_T("height = %d\n"), nHeight);

	OutputDebugString((LPCTSTR)x);
 

        m_oFont.CreateFont(nFontHeight,

                           0,

                           0,

                           0,

                           FW_SEMIBOLD,

                           0,

                           0,

                           0,

                           ANSI_CHARSET,

                           OUT_DEFAULT_PRECIS, //OUT_STROKE_PRECIS,

                           CLIP_DEFAULT_PRECIS, //CLIP_STROKE_PRECIS,

                           0,

                           FIXED_PITCH,

                           _T("Courier New") );

 

        pDC->SelectObject( &m_oFont );

        TEXTMETRIC tm;

        pDC->GetTextMetrics( & tm );

        LONG cy = tm.tmHeight;

 

	x.Format(_T("height of font = %d\n"), cy);

	OutputDebugString((LPCTSTR)x);
 

        if ( cy < nHeight )           //<--- if the font height is less than the control height we have finished

        {

            //pWnd->SetFont( &m_oFont );

	    m_my_edit.SetFont(&m_oFont);

            break;

        }

 

        nFontHeight++;                //<--- otherwise we decrease the font size

        m_oFont.DeleteObject();

    }
 

    /////////////////////////////////////////////////////////////////

    // my method of using CreateFont with height of text box

    /////////////////////////////////////////////////////////////////

    m_my_edit2.GetClientRect(&rc);

    int nHeight2 = rc.Height();             //<--- the height of the textbox

 
 

    CDC* pDC2 = m_my_edit2.GetDC();
 

    x.Format(_T("height2 = %d\n"), nHeight2);

    OutputDebugString((LPCTSTR)x);

 

    m_oFont2.CreateFont(nHeight2,

                       0,

                       0,

                       0,

                       FW_SEMIBOLD,

                       0,

                       0,

                       0,

                       ANSI_CHARSET,

                       OUT_DEFAULT_PRECIS, //OUT_STROKE_PRECIS,

                       CLIP_DEFAULT_PRECIS, //CLIP_STROKE_PRECIS,

                       0,

                       FIXED_PITCH,

                       _T("Courier New") );
 

    pDC2->SelectObject( &m_oFont2 );

    TEXTMETRIC tm2;

    pDC2->GetTextMetrics( & tm2 );

    LONG cy2 = tm2.tmHeight;
 

    x.Format(_T("height of font2 = %d\n"), cy2);

    OutputDebugString((LPCTSTR)x);
 

    m_my_edit2.SetFont(&m_oFont2);
 
 

    //////////////////////////////////////	

    return TRUE;  // return TRUE  unless you set the focus to a control

}

Open in new window

0
 
LVL 8

Author Closing Comment

by:Anthony2000
Comment Utility
Mille grazie!
0
 
LVL 19

Expert Comment

by:alb66
Comment Utility
Positive numbers refer to cell height, while negative numbers refer to character height.
See at http://support.microsoft.com/?scid=kb%3Ben-us%3B32667&x=14&y=14 for a complete explanation.
... Your italian is better than my english ...   ;-)

Ciao,
  Alb66
0

Featured Post

Are end users causing IT problems again?

You’ve taken the time to design and update all your end user’s email signatures, only to find out they’re messing up the HTML, changing the font and ruining the imagery. What can you do to prevent this? Find out how you can save your signatures from end users today.

Join & Write a Comment

Update 11/3/2014 - Although the below article will get you to relocate the WINSXS folder, Microsoft has finally released a utility to reduce the size of the WINSXS folder. For some reason, it's not that straightforward. It only works on Windows 2008…
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
Windows 8 comes with a dramatically different user interface known as Metro. Notably missing from the new interface is a Start button and Start Menu. Many users do not like it, much preferring the interface of earlier versions — Windows 7, Windows X…
With the advent of Windows 10, Microsoft is pushing a Get Windows 10 icon into the notification area (system tray) of qualifying computers. There are many reasons for wanting to remove this icon. This two-part Experts Exchange video Micro Tutorial s…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now