Версия для печати темы
Нажмите сюда для просмотра этой темы в оригинальном формате
Форум программистов > Разработка Windows Forms > DrawString и MeasureString


Автор: amarenkov 7.5.2008, 08:13
Добрый день.

Возникла необходимость нарисовать при помощи GDI+ линию после определенного символа в строке (ну, что-то типа курсора smile). Делаю так:
Код

e.SourceEvent.Graphics.DrawString(fstr_line, fobj_textFont, fobj_textBrush, commandRct);
string tmp_str = fstr_line.Substring(0, fint_position);
sf = e.SourceEvent.Graphics.MeasureString(tmp_str, fobj_textFont);
e.SourceEvent.Graphics.DrawLine(new Pen(Color.Blue), commandRct.Left + sf.Width, commandRct.Top, commandRct.Left + sf.Width, commandRct.Bottom);

Но, как показывает практика, линия соооовсем не попадает в нужное место. В чем может быть проблема? Какое может быть решение?

Заранее спасибо smile.

Автор: mr.DUDA 7.5.2008, 09:30
Цитата
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.

Автор: amarenkov 7.5.2008, 13:01
Большое спасибо, mr.DUDA. Функция MeasureCharacterRanges и MSDN мне помогли smile.

Powered by Invision Power Board (http://www.invisionboard.com)
© Invision Power Services (http://www.invisionpower.com)