[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 499
  • Last Modified:

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

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
Anthony2000
Asked:
Anthony2000
  • 4
  • 4
1 Solution
 
alb66Commented:
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
 
Anthony2000Author Commented:
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
 
Anthony2000Author Commented:
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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

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

 
alb66Commented:
<<< 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
 
alb66Commented:
<<< ... 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
 
Anthony2000Author Commented:
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
 
Anthony2000Author Commented:
Mille grazie!
0
 
alb66Commented:
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: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 4
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now