CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Jun 2021
    Posts
    51

    Why Creatfont not working?

    Hi,
    I'm using the font Calibri,
    I need to calculated fontwidth of CString L"testtextwidth" from the fontsize 14.5 and 16

    I used the Createfont function as follows:

    Code:
    //initialize
    HDC hDC = ::GetDC(NULL);
    CFont hFont;
    double height=14.5;
    
    //creat font
    hFont.CreateFontW(height, 0, 0, 0,
    bold == 1 ? FW_BOLD : FW_NORMAL,FALSE, FALSE, FALSE, ANSI_CHARSET,OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY,DEFAULT_PITCH | FF_DONTCARE, L"Calibri");
    
    //get font size
    SelectObject(hDC, hFont);
    SIZE size = { 0 }; 
    GetTextExtentPoint32(hDC, L"testtextwidth", _tcslen(L"testtextwidth"), &size);
    and the same with height=16.
    however both of these sizes (size.y) give the same width.

    where did i go wrong?
    2 diferrent font size but same width?

    Thanks you!
    Last edited by Dang.D.Khanh; July 28th, 2021 at 11:49 AM.

  2. #2
    VictorN's Avatar
    VictorN is online now Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,076

    Re: Why Creatfont not working?

    Please, edit your post adding proper code indentations! Otherwise it is absolutely non-readable.
    Victor Nijegorodov

  3. #3
    Join Date
    Jun 2021
    Posts
    51

    Re: Why Creatfont not working?

    Quote Originally Posted by VictorN View Post
    Please, edit your post adding proper code indentations! Otherwise it is absolutely non-readable.
    Hi Sir,
    I want to calculate the width of a string, the above code is what I have from this post.

    From h=11point I switch to 14.5pixel and calculate the width like the code, the problem is h=12point has the same width as well.
    What should I do to fix it?

  4. #4
    VictorN's Avatar
    VictorN is online now Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,076

    Re: Why Creatfont not working?

    Quote Originally Posted by Dang.D.Khanh View Post
    ... the above code is what I have from this post.
    Sorry, but I could not find in this post anything similar to what you have posted as a "code".
    Victor Nijegorodov

  5. #5
    Join Date
    Jun 2021
    Posts
    51

    Re: Why Creatfont not working?

    Quote Originally Posted by VictorN View Post
    Sorry, but I could not find in this post anything similar to what you have posted as a "code".
    Hi Sir,
    You can see I'm getting ideas from that post.
    The usage function are all there.
    Code:
    CreateFont
    GetTextExtentPoint32
    DrawText
    I have used CFont instead of HFONT because result has not changed.

Tags for this Thread

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