GetTextExtentPoint32 API
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11

Thread: GetTextExtentPoint32 API

  1. #1
    Join Date
    Jun 2007
    Posts
    45

    GetTextExtentPoint32 API

    When using this with the DrawText API, I make the rectangle that the text is contained in the width of the return value of GetTextExtentPoint32


    I know I am using the right parameters, but sometimes the rectangle is way too big (over 15 pixels wider than the actual width of the text) and sometimes the rectangle is too small.



    It seems that this API is a little.. screwed up perhaps? If I input "WWWWWWWWWWWW" for example, the rectangle containing the text is around 20 pixels too big but if I input "iiiiiiiiiiii", the rectangle is around 20 pixels too small.



    I tried using SetTextCharacterExtra, but that just screwed things up even more. I'm looking for a more accurate and precise way to set the rectangle's width via the width of the text. Btw, there will never be any spaces, numbers, or symbols as the text. The width of the text will only ever contain the 26 lowercase letters and 26 uppercase letters.



    Thanks for any help.

  2. #2
    Join Date
    Aug 2006
    Posts
    34

    Re: GetTextExtentPoint32 API

    I had the same problem when I was developing a listbox portion of my map editor. However, I don't know if your reason for this problem was the same as mine.

    I was using a custom font that was smaller than the default system font. GetTextExtentPoint32() will return the width of your string of text assuming the default font if no other is selected. So before you call GetTextExtentPoint32(), you want to select your font...
    Code:
    SelectObject(hDC, yourFont); // The device context and your font, respectively
    GetTextExtentPoint32(...);
    That should solve the problem, assuming you have a custom font. If not, then I am not sure what to say. I haven't worked with text extents that much.

  3. #3
    Join Date
    Jun 2007
    Posts
    45

    Re: GetTextExtentPoint32 API

    Alright thanks, I'll try it and get back to you ASAP

  4. #4
    Join Date
    Jun 2007
    Posts
    45

    Re: GetTextExtentPoint32 API

    I can't quite seem to fiqure out how to get the handle of the Font? I need this for the second parameter for SelectObject. The only way I found was using CreateFont but that doesn't really seem practical.

  5. #5
    Join Date
    Aug 2006
    Posts
    34

    Re: GetTextExtentPoint32 API

    Yes, you can use CreateFont(), but that won't retrieve a window's font.

    Code:
    HFONT hFont = CreateFont(...); // hFont now holds the font
    
    // You can now set the font later using WM_SETFONT
    if you just want to retrieve the font of a window or control without actually setting it, use WM_GETFONT.
    Code:
    HFONT hFont = SendMessage(hWnd, WM_GETFONT, 0, 0); // returns the font
    Now use your font handle hFont as an argument for SelectObject().

    Good luck

    Ryan

  6. #6
    Join Date
    Jun 2007
    Posts
    45

    Re: GetTextExtentPoint32 API

    Sorry for being such a noob but I'm still kind of new too this x_x:


    Code:
    int length = (int)_tcslen(guildText);
    HFONT hFont = (HFONT)SendMessage(hwnd, WM_GETFONT, 0, 0);
    SelectObject(ps.hdc, hFont);
    GetTextExtentPoint32(ps.hdc, guildText, length, size);
    How's that look? I'm still not getting the correct values from GetTextExtentPoint32. They're always off

  7. #7
    Join Date
    Aug 2006
    Posts
    34

    Re: GetTextExtentPoint32 API

    Did this line compile?
    Code:
    GetTextExtentPoint32(ps.hdc, guildText, length, size);
    I believe you have to use the address-of operator for 'size'
    Code:
    GetTextExtentPoint32(ps.hdc, guildText, length, &size);
    size.cx should contain the width of 'guildText'


    How much are your values off?

  8. #8
    Join Date
    Jun 2007
    Posts
    45

    Re: GetTextExtentPoint32 API

    Quote Originally Posted by Coder Noob
    Did this line compile?
    Code:
    GetTextExtentPoint32(ps.hdc, guildText, length, size);
    I believe you have to use the address-of operator for 'size'
    Code:
    GetTextExtentPoint32(ps.hdc, guildText, length, &size);
    size.cx should contain the width of 'guildText'


    How much are your values off?
    When I change size to &size, it throws an error.

    And the values are off like usual. For example, if I type "WWWWWWWWWWWW", it is 20 pixels over. If I type "iiiiiiiiiiii", they are 20 under.. etc.. the usual old deal.. Should I try using GetCharABCWidths instead?

    Although I can't get that to work much so I don't know..

  9. #9
    Join Date
    Aug 2006
    Posts
    34

    Re: GetTextExtentPoint32 API

    Hmmm, well our problems may ultimately be different after all. An ABC structure is out of my hands. I've never had experience with that. But here, I'll pull up my old code that I used to get my stuff working. Maybe it'll help.
    Code:
     
    // Create and set the font for the listbox
    HFONT hFont = CreateFont(11, 6, 0, 0, FW_MEDIUM, FALSE, FALSE, FALSE, ANSI_CHARSET,
          OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH, "Arial");
    SendMessage(m_hList, WM_SETFONT, (WPARAM)m_hFont, TRUE);
    
    // Get the text extent and apply it for horizontal scrolling
    HDC hDC = GetDC(hListBox);
    
    SelectObject(hDC, (HFONT)hFont);
    
    int iItemLength = strlen(szTilesetPath); // Get the length of the tileset path
    
    GetTextExtentPoint32(hDC, szTilesetPath, iItemLength, &ExtentSize); 
    
    // Set the horizontal extent
    SendMessage(m_hList, LB_SETHORIZONTALEXTENT, ExtentSize.cx + 5, 0); 
    // The + 5 adds a little room between the end of the text and the right border of the window
    
    ReleaseDC(m_hList, hDC);
    Sorry I couldn't be more help.

  10. #10
    Join Date
    Jun 2007
    Posts
    45

    Re: GetTextExtentPoint32 API

    I'm trying something like this:

    Code:
    int length = (int)_tcslen(guildText);
    HFONT hFont = CreateFont(0, 0, 0, 0, FW_BOLD, FALSE, FALSE, FALSE, ANSI_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH, L"Arial");
    SelectObject(ps.hdc, hFont);
    GetTextExtentPoint32(ps.hdc, guildText, length, size);
    It's giving the same results if I used just this:

    Code:
    GetTextExtentPoint32(ps.hdc, guildText, length, size);
    I think the HFONT declaration and initialization and the SelectObject call does basically nothing. I think I am doing something wrong?

  11. #11
    Join Date
    Aug 2006
    Posts
    34

    Re: GetTextExtentPoint32 API

    Creating the font and selecting it does do something. Just to make sure, I changed the 2nd parameter of CreateFont() to 20, and the rectangle was REALLY big compared to when it used to be at 11.

    Just curious, what is ps? Is it supposed to be a PAINSTRUCT, or is it a class you developed?

    I've got one last idea before I go to bed. Try changing...
    Code:
    LPSIZE size = new SIZE();
    to just this
    Code:
    SIZE size; // remember to try using the & operator in GetTextExtentPoint32 for this!
    After this, I'm pretty much out of ideas. Like I said, I don't much work with text extents, but I'm here to offer any help that I can.

    Let me know how it goes.

    Ryan

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center