Solved

Dynamically creating CFont to fit in CRect area??

Posted on 2002-05-04
8
342 Views
Last Modified: 2013-11-20
What is the best way to resize a CFont to fit in a given area of my Custom Control?  The cx and cy of the control both change and have to be taken into account.  I guess the best way to explain, is using a CRect, I want my text to scale so it always fits in the CRect; top to bottom, left to right.  I know how to tie the CFont height variable to  CRect.Height() but I also need to take into accont the Width().  Anyway... There must be an easy way to accomplish my goal that I am not seeing.  All suggestions welcome and code samples greatly appreciated.

-Michael
0
Comment
Question by:Surfer
8 Comments
 
LVL 16

Expert Comment

by:AlexNek
ID: 6989991
2 Surfer
AFAIK it is not possible to "fill exact" any rectangle with font. But for kerning emulation you can try to use char by char output. Or try to use RichEdit from V2.0
0
 
LVL 86

Expert Comment

by:jkr
ID: 6989993
You could check the actual size of the text you want to place in the rect using 'GetTextExtentPoint32()' - however, I am not aware that there is a more comfortable "other way round"...
0
 
LVL 49

Expert Comment

by:DanRollins
ID: 6990707
This code seems to work.  All it does is keep trying smaller and smaller versions of the font until the text fits.  So you can easily end up with a font that is too 'short' just so that its width fits in the box.  There is *no* command that will produce a 'tall,skinny' font from a regular font to handle that situation.  

(However, one idea would be to print tht text to the DC, capture a bitmap of it, then use StretchDIBits to generate one that was 'just right' then display that bitmap -- instead of the actual text.

IMPORTANT: You will need to add some error checking to the code below; it can easily get into an infinte loop.
During testing, I found that the font was staying the same size because the parent dialog's font was MS Sans Serif -- a non TrueType font.  

I am having to release the DC becasue I was working with a EDIT window which as the CS_PARENTDC  class bit.

CFont gcFont;

void CD03Dlg::OnButton1()
{
     CString   sText;
     LOGFONT   rLF;
     CRect     rc;
     CSize     cSz;

     m_ctlEdit.GetWindowText( sText );

     CFont* pFont= m_ctlEdit.GetFont();
     pFont->GetLogFont( &rLF );
     
     m_ctlEdit.GetWindowRect( &rc );

     CDC*   pDC=      m_ctlEdit.GetDC();
     CFont* pOldFont= pDC->SelectObject( pFont );

     while ( 1 ) {
          cSz=  pDC->GetTextExtent( sText );
          if ( cSz.cx <= rc.Width() && cSz.cy <= rc.Height() ) {
               pDC->SelectObject( pOldFont );
               break;
          }
          rLF.lfHeight += (rLF.lfHeight > 0 ? -1: 1);
          gcFont.DeleteObject();
          BOOL fRet= gcFont.CreateFontIndirect( &rLF );

          pDC->SelectObject( &gcFont );
     }
     pDC->SelectObject( pOldFont );
     m_ctlEdit.ReleaseDC( pDC );
     
     m_ctlEdit.SetFont( &gcFont, TRUE );
     m_ctlEdit.Invalidate();
}

0
 

Author Comment

by:Surfer
ID: 6991689
(However, one idea would be to print tht text to the DC, capture a bitmap of it, then use StretchDIBits
to generate one that was 'just right' then display that bitmap -- instead of the actual text.

This sounds interesting.  Can you please give me a code sample?

-Michael
0
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 

Author Comment

by:Surfer
ID: 6991693
(However, one idea would be to print tht text to the DC, capture a bitmap of it, then use StretchDIBits
to generate one that was 'just right' then display that bitmap -- instead of the actual text.

This sounds interesting.  Can you please give me a code sample?

-Michael
0
 
LVL 49

Expert Comment

by:DanRollins
ID: 6992384
This works beautifully.  I've outfdone myself here!
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

BOOL TextToBitmap(  LPCSTR szText, CFont* pcFont, CRect* prcDest, CBitmap* pcBmpDest )
{
     CBitmap     cBmpCanvas;
     CWindowDC     dcWin( AfxGetMainWnd() );
     CDC           memSrcDC;
     CDC           memDestDC;

     pcBmpDest->DeleteObject();
     
     memSrcDC.CreateCompatibleDC( &dcWin );

     CFont*   pOldFont=      memSrcDC.SelectObject( pcFont );

     CSize cszSrc= memSrcDC.GetTextExtent( szText, strlen(szText) );

     cBmpCanvas.CreateCompatibleBitmap( &dcWin, cszSrc.cx, cszSrc.cy );

     CBitmap* pOldSrcBitmap= memSrcDC.SelectObject( &cBmpCanvas );

     CRect rcCanvas( CPoint(0,0), cszSrc );
     BOOL fRet= memSrcDC.ExtTextOut( 0,0, ETO_OPAQUE, &rcCanvas, szText, strlen(szText), 0 );

     //------------- now bmpCanvas has the 'raw' image
     //------------- stretch that to fit desired rectangle

     memDestDC.CreateCompatibleDC( &dcWin );

     pcBmpDest->CreateCompatibleBitmap( &dcWin, prcDest->Width(), prcDest->Height() );
     CBitmap* pOldDestBitmap= memDestDC.SelectObject( pcBmpDest );

     memDestDC.SetStretchBltMode( HALFTONE ); // looks good!
     memDestDC.SetBrushOrg(0,0);
     
     memDestDC.StretchBlt(
          0,0, prcDest->Width(), prcDest->Height(),
          &memSrcDC,
          0,0,  cszSrc.cx,cszSrc.cy,
          SRCCOPY
     );

     memSrcDC.SelectObject( pOldSrcBitmap );
     memSrcDC.SelectObject( pOldFont );

     memDestDC.SelectObject( pOldDestBitmap );

     return( TRUE );
}

//--------------------------------- example test
// it takes test fome an edit box, generates a bitmap, and sets it into a CStatic.

void CD03Dlg::OnButton1()
{
     CRect rcDest(   0,0, 100, 20 );

     static CBitmap cBmp; // must persist so that SetBitmap, below won't croak

     CFont cFont;
     cFont.CreatePointFont( 120, "Arial" );

     CString sText;
     GetDlgItemText( IDC_EDIT1, sText );

     BOOL fRet= TextToBitmap( sText, &cFont,  &rcDest, &cBmp );

     m_ctlBmpDisplay.SetBitmap( (HBITMAP)cBmp.m_hObject );
}
0
 
LVL 49

Accepted Solution

by:
DanRollins earned 200 total points
ID: 7007777
Hey Surfer,
I worked a LONG TIME to create that perfect solution for you.  Why have you not responded?

-- Dan
0
 

Author Comment

by:Surfer
ID: 7007784
Because I have been EXTREMELY busy working on a couple other projects.  Your solution was indeed a very elegant approach that I think will do the trick.  Thanks for your time and patients!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

This article is an update and follow-up of my previous article:   Storage 101: common concepts in the IT enterprise storage This time, I expand on more frequently used storage concepts.
A Bare Metal Image backup allows for the restore of an entire system to a similar or dissimilar hardware. They are highly useful for migrations and disaster recovery. Bare Metal Image backups support Full and Incremental backups. Differential backup…
This tutorial will walk an individual through the process of installing the necessary services and then configuring a Windows Server 2012 system as an iSCSI target. To install the necessary roles, go to Server Manager, and select Add Roles and Featu…
This tutorial will show how to configure a single USB drive with a separate folder for each day of the week. This will allow each of the backups to be kept separate preventing the previous day’s backup from being overwritten. The USB drive must be s…

932 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

12 Experts available now in Live!

Get 1:1 Help Now