Solved

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

Posted on 2008-10-06
8
482 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
Easy, flexible multimedia distribution & control

Coming soon!  Ideal for large-scale A/V applications, ATEN's VM3200 Modular Matrix Switch is an all-in-one solution that simplifies video wall integration. Easily customize display layouts to see what you want, how you want it in 4k.

 
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Storage devices are generally used to save the data or sometime transfer the data from one computer system to another system. However, sometimes user accidentally erased their important data from the Storage devices. Users have to know how data reco…
Possible fixes for Windows 7 and Windows Server 2008 updating problem. Solutions mentioned are from Microsoft themselves. I started a case with them from our Microsoft Silver Partner option to open a case and get direct support from Microsoft. If s…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…

839 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