c# - How can i determine the width of a drawn string? -
this question has answer here:
i'm drawing string using following:
g.drawstring(somestring, somefont, new solidbrush(color.black), x starts, y starts));
problem:
sometimes want align drawn string on left side
of line
. but, doesn't work if don't know string length be.
possible noob solution:
count number of chars, multiply trial , error value, , subtract on x position, this:
string somestring = convert.tostring(math.round(10 * somevalue) / 10) + "m"; int stringcount1 = somestring.count(); g.drawstring(somestring, somefont, new solidbrush(color.black), x -stringcount1 * trialanderrorvalue, y));
this doesn't work because, can guess, i'm creating number, , number can have decimal separator
or not.
and problem, because decimal separator not wide character, still counts, sending string neptune-far line. or can decent trial , error value number decimal separator, , if number doesn't have decimal separator above line (and ruin drawing).
any hints? possibly forcing string have ,0
in case number integer?
edit:
i've created method return asizef
object given string , font.
static sizef measurestringmin(string measurestring, font stringfont, painteventargs e) { //// set string. //string measurestring = "measure string"; //font stringfont = new font("arial", 16); // measure string. sizef stringsize = new sizef(); stringsize = e.graphics.measurestring(measurestring, stringfont); return stringsize; }
the problem when recall function using like
sizef stringsize = new sizef(); stringsize = measurestringmin(somestring, somefont, ????)
i never know put in third variable, hints on this? why need painteventargs
?
you need measurestring
function! use exact dimensions of string before draw it, , adjust position accordingly.
Comments
Post a Comment