Solved

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

Posted on 2008-10-06
8
485 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Don't Miss ATEN at InfoComm 2017!

Visit booth #2167 to see the  new ATEN VM3200 32 x 32 Modular Matrix Switch. Other highlights include the VE8950 4K HDMI Over IP Extender, VS1912 12-Port DP Video Wall Media Player  and VK2100 ATEN Control System. Register now with Free Pass Code ATEN288!

 
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

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

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

An article on effective troubleshooting
An introduction to the wonderful sport of Scam Baiting.  Learn how to help fight scammers by beating them at their own game. This great pass time helps the world, while providing an endless source of entertainment. Enjoy!
This video Micro Tutorial explains how to clone a hard drive using a commercial software product for Windows systems called Casper from Future Systems Solutions (FSS). Cloning makes an exact, complete copy of one hard disk drive (HDD) onto another d…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

739 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