How to get the TextExtent of a string

D

Dom

In part of my code, I need to know the rectangle that a string will
fit into, if it is written in a given font and a given Device Context
(DC may be an old term). Back in the days of "C" and Windows 3, you
had to create a DC, Select a Font into the DC, then call
GetTextExtentPoint32, which took the string as an argument. It
returned the rectangle.

I can do this in CSharp by calling the same API functions, but my
guess is that it is done often enough that it should be wrapped up
somewhere. I've looked, can't find it. Any help?

Dom
 
D

Dom

In part of my code, I need to know the rectangle that a string will
fit into, if it is written in a given font and a given Device Context
(DC may be an old term). Back in the days of "C" and Windows 3, you
had to create a DC, Select a Font into the DC, then call
GetTextExtentPoint32, which took the string as an argument. It
returned the rectangle.

I can do this in CSharp by calling the same API functions, but my
guess is that it is done often enough that it should be wrapped up
somewhere. I've looked, can't find it. Any help?

Dom

Son of a ...

I just found VisualStyleRenderer
 
G

Guest

Sure thing. It's wrapped under the Graphics class, and it's called
MeasureString(...).

Hope this helps.


Saul
 
G

Guest

/// <summary>
/// Summary description for TextTrimming.
/// </summary>
public class TextTrimmer : IDisposable
{
#region Events & Delegates
public delegate void TextTrimmingHandler(object o, TrimmingTextEventArgs
i_args);
public event TextTrimmingHandler TextTrimmed;
#endregion

#region Data members
private Graphics m_graphics = null;
private Control m_Control = null;
private SizeF m_stringMeasure = new SizeF();
private bool m_isTrimmed = false;
private string m_trimmedNewValue = "";
private string m_measuredString = "";
private Font m_measuredFont = null;
#endregion

#region Consts
public const string TRIM_TEXT = "...";
#endregion

#region Properties
public int StringWidth
{
get
{
return (int) m_stringMeasure.Width;
}
}

public bool Trimmed
{
get
{
return m_isTrimmed;
}
}

public int StringHeigth
{
get
{
return (int) m_stringMeasure.Height;
}
}

public string TrimmedString
{
get
{
if( m_trimmedNewValue.Length == 0 )
{
return m_measuredString;
}
return m_trimmedNewValue;
}
}

public string MeasureString
{
get
{
return m_measuredString;
}
}
#endregion

public TextTrimmer(Control i_control)
: this(i_control, i_control.Text, i_control.Font)
{

}

public TextTrimmer(Control i_control, string i_textToMeasure)
: this(i_control, i_textToMeasure, i_control.Font)
{
}

public TextTrimmer(Control i_control, string i_textToMeasure, Font
i_fontToMeasure)
{
m_Control = i_control;

m_graphics = m_Control.CreateGraphics();

m_measuredString = i_textToMeasure;

m_measuredFont = i_fontToMeasure;

m_stringMeasure = m_graphics.MeasureString(m_measuredString,
m_measuredFont);
}

public void Apply()
{
Apply(m_Control.Width, 30);
}

public void Apply(int i_refWidth, int i_assistRefWidth)
{
if( StringWidth == 0 )
{
return;
}

// Add 5 (assummed) pixel because the layout have some unvisible width
which hurt the text value visibility
if (StringWidth + 5 < i_refWidth)
{
return;
}

char[] crArray = m_measuredString.ToCharArray();

int textWidthInPixels = 0;

StringBuilder builder = new StringBuilder();

for (int i = 0; i < crArray.Length; i++)
{
builder.Append(crArray.ToString());

textWidthInPixels = (int) (m_graphics.MeasureString(
builder.ToString(),
m_measuredFont).Width);

if (textWidthInPixels >= i_refWidth - i_assistRefWidth)
{
m_isTrimmed = true;

m_trimmedNewValue += TRIM_TEXT;

break;
//m_stringMeasure.Width = textWidthInPixels + 30;
//m_valueTextLength = textWidthInPixels + 30;
}
else
{
m_trimmedNewValue = builder.ToString();
}
}

string oldString = m_measuredString;

m_Control.Text = m_trimmedNewValue;

OnTextTrimmed( oldString, m_trimmedNewValue );
}


protected void OnTextTrimmed(string i_oldString, string i_newTrimmedString)
{
if( TextTrimmed != null )
{
TextTrimmed(this,
new TrimmingTextEventArgs(m_Control,
i_oldString,
i_newTrimmedString));
}
}


#region IDisposable Members

public void Dispose()
{
if( m_graphics != null)
{
m_graphics.Dispose();
}

GC.SuppressFinalize(this);
}

#endregion
}

#region public class TrimmingTextEventArgs : EventArgs
public class TrimmingTextEventArgs : EventArgs
{
private string m_allText;
private string m_trimmedText;
private Control m_refControl;

public string AllText
{
get
{
return m_allText;
}
}


public string TrimmedText
{
get
{
return m_trimmedText;
}
}


public Control RefControl
{
get
{
return m_refControl;
}
}
public TrimmingTextEventArgs(Control i_refControl, string i_allText,
string i_trimmedText)
{
m_refControl = i_refControl;
m_allText = i_allText;
m_trimmedText = i_trimmedText;
}
}
#endregion

Enjoy.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top