Using System.Drawing.Graphics MeasureString() in a console Applicaiton

  • Thread starter Thread starter Paul Hoad
  • Start date Start date
P

Paul Hoad

I'm trying to use MeasureString() to determine the length in pixels of a
string

However to do I need a System.Drawing.Graphics object

to do this I need to create a System.Drawing.Graphics object for which there
is only
two constructors

System.Drawing.Graphics.FromHdc

and

System.Drawing.Graphics.FromHwnd

My application is a C# Console application and I don't have and Forms to
pass the handle
into.

Does anyone know of a way of me overcoming this problem? perhaps I can get
the DesktopWindow in C#?

public SizeF HowBigIsThisText(string myString)
{
Font MyFontObject = new Font("MS San Serif",8);
System.Draing.Graphics gr=System.Drawing.Graphics.FromHwnd(<Don't know
what handle to provide here>)

return gr.MeasureStringmyString,MyFontObject);
}

Many thanks in advance

Dr Paul Hoad
 
Paul said:
I'm trying to use MeasureString() to determine the length in pixels
of a string

However to do I need a System.Drawing.Graphics object

to do this I need to create a System.Drawing.Graphics object for
which there is only
two constructors

System.Drawing.Graphics.FromHdc

and

System.Drawing.Graphics.FromHwnd

My application is a C# Console application and I don't have and
Forms to pass the handle
into.

Does anyone know of a way of me overcoming this problem? perhaps I
can get the DesktopWindow in C#?

public SizeF HowBigIsThisText(string myString)
{
Font MyFontObject = new Font("MS San Serif",8);
System.Draing.Graphics gr=System.Drawing.Graphics.FromHwnd(<Don't
know what handle to provide here>)

return gr.MeasureStringmyString,MyFontObject);
}

Many thanks in advance

Dr Paul Hoad

Just create a form, set its Visible to false, then use its CreateGraphics
method.

- Pete
 
Back
Top