font size

I draw a text with a specific height with TextOut routine but, after a mouse action, I want this text size (font size) to fit inside a specific rectangle; so, I should know the font height that should be set according to this rectangle width.  Do anyone know how to do it (this routine will be inserted or called from a mouse click event handle).
Thanks in advance.
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.


CSize GetOutputTextExtent( const CString& str ) const;

Return Value: The dimensions of the string (in logical units) returned in a CSize object.

.... and check it's width and height against the coordinates created from your mouse movements.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
marakrAuthor Commented:
Thanks for your answer. I was just wondering how would I really change it (I meant pDC->SelectObject(...)).  I really don't know how to work with those device contexts.
Could you give me bit more details?
1] set a (bool m_bMouseDown) to true when LMB is down and to false when LMB is released.

2] When the mouse is down, set a member CSize variable to equal the current CPoint variable passed in the override.

3] When the "LMB Up" is fired, check the difference between the CSize variable and the now-supplied CPoint variable

4] Apply the following pseudo code:

for (int n = 1; n < MAXSIZE; n++)
  Create a CFont object
  Get the LOGFONT structure and
  increment it's size

  Use the CDC::SelectObject to use
  the font

  Use GetOutputTextExtent() to compare
  the new size of the text with the
  CSize that you got from the user's
  mouse drag movements

  If either the new height or new
  width reaches the relative dimensions
  of the user-specified CSize, then go
  ahead and use CDC::DrawText to do
  it's thang, and 'continue' to leave
  the for-loop...!

I hope this helps.

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Fonts Typography

From novice to tech pro — start learning today.