Specific Font Size question

  • Thread starter Paul Gorodyansky
  • Start date
P

Paul Gorodyansky

Hi,

I've read most of the discussions about
"calculation of string width in pixels or points" - they mostly talk
about MeasureString (and that it's not exact width) or more presize
but requiring much more code MeasureCharacterRanges -
and no one answered this guy's question (I had the same) -
http://www.dotnet247.com/247reference/msgs/39/198205.aspx

Any way, my task is _not_ to draw anything but for existing fixed-width
font - "MS Sans Serif, 8" - to calculate the width of the string, so I
used this simplistic approach I've found on the Web:

string Label;
.....
Font defFont = new Font("MS Sans Serif", 8);
float TextWidth = defFont.SizeInPoints * Label.Length;

Works Ok, but I saw on screen that if I choose a Bold variant of
the font, then the string width _increases_.

So I did the following, but .NET C# returns me the same value
as with non-Bold case:

Font defFontBold = new Font("MS Sans Serif", 8,
(System.Drawing.FontStyle)1);

float TextBoldWidth = defFontBold.SizeInPoints * Label.Length;

Does anyone know why SizeInPoints is not changing for Bold style?
 
B

Bob Powell [MVP]

MS Sans Serif is not a fixed width font. It's proportional. Making the font
bold will increase the length of the string.

Courier new is a fixed width font.

SizeInPoints returns a measure of the height of the font in points. Changing
the style of the font won't change it's height.

--
Bob Powell [MVP]
Visual C#, System.Drawing

Ramuseco Limited .NET consulting
http://www.ramuseco.com

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.
 
P

Paul Gorodyansky

Thanks!

I was browsing through SO many pages about this issue at once
that probably lost control over my brain :) - the page about
font.SizeInPoints was probably #65 in the row, so I did not realize
that it was completely wrong - because I do know that 8 is a height
and not a width :)

Thanks, again,
 
P

Paul Gorodyansky

Hi,

How to get System.Windows.Forms.PaintEventArgs -
e.Graphics - without really painting anything?

I saw many examples of what I need with a function where
an argument is e.Graphics but could not find a way how to
_call_ such function, i.e. how to get e.Graphics in first place...

Thanks,
Paul
 
P

Paul Gorodyansky

The code where I need to add string width calculations, look
like this (so I just need to find out how to obtain e.Graphics used
as an argument in the very last line)

==============================
namespace fieldValidater
{
class Class1
[STAThread]
static void Main(string[] args)
{
checkStringsWidth(args[0], args[1]);
}
static void StringsWidth(string formName, string serverName)
{
...
Font defFont = new Font("MS Sans Serif", 8);

<loop to get strings>
<current string processing:>
....
int a2 = MeasureDisplayStringWidth(Graphics g, curString, defFont);
}
}
===============================
 

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