CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Jul 2003
    Posts
    64

    Getting Font Dimensions

    Hello. I'm wondering if there is a way to get the dimensions (height, width) of a specific character or string using a certain windows font.
    So is there any way I can find out this information if I have a font name ie) Arial, and a CString ? I basically just need the ratio of the string height to the width.
    I don't want to draw the text, just need this height to width ratio. Either for each character, or the whole string.
    Thanks for any help or suggestions.

  2. #2
    Join Date
    Apr 2000
    Location
    Boston
    Posts
    124

    Re: Getting Font Dimensions

    Use GetTextExtent.

    check out this post.

  3. #3
    Join Date
    May 1999
    Location
    West Sussex, England
    Posts
    1,939

    Re: Getting Font Dimensions

    In MFC you could do it like this:

    Code:
    CDC dc;
    
    dc.CreateCompatibleDC(NULL);
    dc.SaveDC();
    dc.SelectObject(&font);
    CString text("Text i want to measure");
    CSize size = dc.GetTextExtent(text);
    dc.RestoreDC(-1);
    dc.DeleteDC();
    Please use meaningful question titles - "Help me" does not let me know whether I can help with your question, and I am unlikely to bother reading it.
    Please remember to rate useful answers. It lets us know when a question has been answered.

  4. #4
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,633

    Re: Getting Font Dimensions

    Quote Originally Posted by CodeGoose
    I'm wondering if there is a way to get the dimensions (height, width) of a specific character or string using a certain windows font.
    So is there any way I can find out this information if I have a font name ie) Arial, and a CString ? I basically just need the ratio of the string height to the width.
    I don't want to draw the text, just need this height to width ratio. Either for each character, or the whole string.
    Thanks for any help or suggestions.
    You could use GetTextExtentPoint32 (suppose most precise from its family). There are so many ways to do the thing - for example DrawText with DT_CALCRECT flag.

    About calculating sizes for particular font name. You can do it being selecting already created font into device context and then performing necessary call(s) for size retrieving. There's no way to get font metrics for non-created font (having its name only).
    Best regards,
    Igor

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured