Click to See Complete Forum and Search --> : GetTextExtentPoint32 API


Fromethius
July 19th, 2007, 06:44 PM
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. :)

Coder Noob
July 19th, 2007, 08:05 PM
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...

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.

Fromethius
July 19th, 2007, 08:56 PM
Alright thanks, I'll try it and get back to you ASAP

Fromethius
July 19th, 2007, 09:22 PM
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.

Coder Noob
July 19th, 2007, 09:29 PM
Yes, you can use CreateFont(), but that won't retrieve a window's font.


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.

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

Fromethius
July 19th, 2007, 09:48 PM
Sorry for being such a noob but I'm still kind of new too this x_x:



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

Coder Noob
July 19th, 2007, 09:58 PM
Did this line compile?

GetTextExtentPoint32(ps.hdc, guildText, length, size);

I believe you have to use the address-of operator for 'size'

GetTextExtentPoint32(ps.hdc, guildText, length, &size);

size.cx should contain the width of 'guildText'


How much are your values off?

Fromethius
July 19th, 2007, 09:59 PM
Did this line compile?

GetTextExtentPoint32(ps.hdc, guildText, length, size);

I believe you have to use the address-of operator for 'size'

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..

Coder Noob
July 19th, 2007, 10:17 PM
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.

// 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. :(

Fromethius
July 19th, 2007, 10:50 PM
I'm trying something like this:

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:

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?

Coder Noob
July 19th, 2007, 11:48 PM
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...

LPSIZE size = new SIZE();

to just this

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