MiRa_2346

Hi !

I'm trying to create a matrix with columns that automatically resize in size to the content. I'm experiencing difficulties with using something like this.

string sText = "hello";
CSize size;
CClientDC dc(this);
CFont font;
size = dc.GetTextExtent(sText.c_str());

The GetTextExtens gives something which is close to the correct size but still a bit off. I could get all of the font properties at this time, but i'm unaware as to where i should put them.



Re: Visual C++ General Is there a way to get the width and height of text in pixels from a specific font?

Ramkrishna Pawar

You could try CDC::GetOutputTextExtent (MFC)






Re: Visual C++ General Is there a way to get the width and height of text in pixels from a specific font?

Simple Samples

CDC contains two device contexts. GetTextExtent and GetOutputTextExtent do the same thing except for each of the two device contexts.

The documentation is not as clear as it could be. Perhaps the size of the text is calculated by multiplying the average of character widths times the number of characters. I have read that text sizes are imprecise for that reason. If so, then perhaps you need to get text metrics to do the calculation yourself.






Re: Visual C++ General Is there a way to get the width and height of text in pixels from a specific font?

Viorel.

In addition, you could consider also the DrawText or DrawTextEx functions with DT_CALCRECT flag.





Re: Visual C++ General Is there a way to get the width and height of text in pixels from a specific font?

MiRa_2346

This sounds good, getting the textmetrics couldn't help because the font and fontsize is not considered. And textmetrix contains a ton of values for letter height but of width only max and average.

I continued the survey to other things around over at MSDN and what sounded good to me was GetTextExtentPoint32, however it was a failure.

I think i'v seen this feature in excel and other stuff like that.

i think i'll look more into this

"In addition, you could consider also the DrawText or DrawTextEx functions with DT_CALCRECT flag."

------------------------------------------------
edit:

I'm apalled by the performance of these.

they all return what seems to be exactly the same and wrong size.

i just feel that it has to be there, but where




Re: Visual C++ General Is there a way to get the width and height of text in pixels from a specific font?

MiRa_2346

if you make a matrix with MFC and doubleclick on the header divider the column on the left side gets automatically sized to content of the column, excludin the header content.

i would like to know how this happens




Re: Visual C++ General Is there a way to get the width and height of text in pixels from a specific font?

MiRa_2346

Now i'v got something. It doesn't work perfectly, but somewhat good enough.

The reason i didn't get it to work before was of my own cause ( as always ). i wasn't setting the font properly.

i'll show how to set it in case someone wonders about the same thing.

string sText = "example";
CSize size;
CClientDC dc(this);
CFont *font;
font = GetFont();
dc.SelectObject(font);
CRect rect;
size = dc.GetTextExtent(sText.c_str(), sText.length());

somehow the width is still too small, but at least it's always the same amount, adding 20 to size.cx helped me along. It may be caused by something else that the container doesn't get big enough.

Thanks for helping me with this.




Re: Visual C++ General Is there a way to get the width and height of text in pixels from a specific font?

Simple Samples

MiRa_2346 wrote:
if you make a matrix with MFC

There is nothing in MFC called a matrix. Within the context of MFC, that term is ambiguous.






Re: Visual C++ General Is there a way to get the width and height of text in pixels from a specific font?

MiRa_2346

ok, well, what i meant was a thing like excel chart. I just couldn't remember the correct MFC class name for the thing.

I guess it's CListCtrl with more than 1 column, but i guess as there are other possibilities.

It seems as if the CHeaderCtrl does the resize thing, however, i can't find the spot where the magic happens.




Re: Visual C++ General Is there a way to get the width and height of text in pixels from a specific font?

Simple Samples

The MSDN does have peculiarities, one of which is that we need to know the exact name of stuff we use; without knowing the exact name, the MSDN will often refuse to help us.

There seems to be a belief that volunteers in these forums are psychic but unfortunately I am not. I also seem unable to help much if I don't know what you are using.

If you need to create an owner-drawn CListCtrl then you might need to use Device Contexts and use functions such as GetTextExtent but if you don't need to get fancy then it is much easier to look at the documentaion.

Look at CListCtrl Members. I think you will find functions for determining the column width necessary for displaying a string in a column and for setting column widths using a variety of options.






Re: Visual C++ General Is there a way to get the width and height of text in pixels from a specific font?

MiRa_2346

Yes, i do understand why such information is relevant. Thank you for the help, GetTextExtent worked nicely for me after i got to know how to use it.