Re: Graphics.MeasureString too short
- From: "Michael F. Sargent" <msargent@xxxxxxxx>
- Date: Fri, 10 Oct 2008 08:12:15 -0400
MeasureString has some usable overloads that may help you. For example, I use:
SizeF tabSize = graphics.MeasureString( "M", font, rectfText.Size, StringFormat.GenericTypographic );
This overload is described in http://msdn.microsoft.com/en-us/library/ms142109.aspx
The note under Remarks is of interest:
The MeasureString method is designed for use with individual strings and includes a small amount of extra space before and after the
string to allow for overhanging glyphs. Also, the DrawString method adjusts glyph points to optimize display quality and might
display a string narrower than reported by MeasureString. To obtain metrics suitable for adjacent strings in layout (for example,
when implementing formatted text), use the MeasureCharacterRanges method or one of the MeasureString methods that takes a
StringFormat, and pass GenericTypographic. Also, ensure the TextRenderingHint for the Graphics is AntiAlias.
Hope this helps,
Mike
"Rune Huseby" <rune@xxxxxxxxx> wrote in message news:Xns9B32A42732476runehusebynewschello@xxxxxxxxxxxxxxxx
"Rainer Queck" <Rainer@xxxxxxxxxxxxxxx> wrote in
news:u9l9Z7eKJHA.5328@xxxxxxxxxxxxxxxxxxxx:
Hello NG,
I am trying to adjust the column width of a DataGridView to the Text
in its column header.
To do this I am using the Graphics.MeasureString method. Unfortunatly
I alway get a too short string width.
What is it I am doing wrong?
Here the code I am using:
private void MinimizeDgvColumns(DataGridView dgv)
{
using (Graphics g = dgv.CreateGraphics())
{
foreach (DataGridViewColumn c in dgv.Columns)
{
string s = c.HeaderCell.Value.ToString();
c.Width = (int)graphics.MeasureString(s,
dgv.ColumnHeadersDefaultCellStyle.Font).Width;
}
}
}
You need to have space for the gridlines to, and some margin.
This is how the text-box-columns calculate its preferred size:
protected internal override Size GetPreferredSize(Graphics g, object value)
{
Size extents = Size.Ceiling(g.MeasureString(GetText(value),
DataGridTableStyle.DataGrid.Font));
extents.Width += xMargin*2 + this.DataGridTableStyle.GridLineWidth;
extents.Height += yMargin;
return extents;
}
hint: The .NET source-code can be downloaded using this tool:
http://www.codeplex.com/NetMassDownloader
--
Rune Huseby
.
- References:
- Graphics.MeasureString too short
- From: Rainer Queck
- Re: Graphics.MeasureString too short
- From: Rune Huseby
- Graphics.MeasureString too short
- Prev by Date: can't find webservice using IP
- Next by Date: Re: can't find webservice using IP
- Previous by thread: Re: Graphics.MeasureString too short
- Next by thread: Moving a file on a FTP server
- Index(es):
Relevant Pages
|