Solved

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

Posted on 2008-10-06
8
481 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
ID: 22657399
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
ID: 22658416
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
ID: 22658433
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
Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

 
LVL 19

Expert Comment

by:alb66
ID: 22658518
<<< 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
 
LVL 19

Accepted Solution

by:
alb66 earned 500 total points
ID: 22658679
<<< ... 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
ID: 22686491
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
ID: 31503565
Mille grazie!
0
 
LVL 19

Expert Comment

by:alb66
ID: 22764929
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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The use of stolen credentials is a hot commodity this year allowing threat actors to move laterally within the network in order to avoid breach detection.
When you start your Windows 10 PC and got an "Operating system not found" error or just saw  "Auto repair for startup" or a blinking cursor with black screen. A loop for Auto repair will start but fix nothing.  You will be panic as there are no back…
The viewer will learn how to successfully create a multiboot device using the SARDU utility on Windows 7. Start the SARDU utility: Change the image directory to wherever you store your ISOs, this will prevent you from having 2 copies of an ISO wit…
The Task Scheduler is a powerful tool that is built into Windows. It allows you to schedule tasks (actions) on a recurring basis, such as hourly, daily, weekly, monthly, at log on, at startup, on idle, etc. This video Micro Tutorial is a brief intro…

822 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