I'm having a problems with the Graphics.DrawString() method. My UserControl uses a monospaced font to draw on a panel, and clearly, DrawString() formats the text in such a way, that it isn't exactly monospaced anymore. I have been using a StringFormat in the hope of fixing this, but without success:
StringFormat sf = new StringFormat(StringFormatFlags.NoClip | StringFormatFlags.FitBlackBox | StringFormatFlags.LineLimit | StringFormatFlags.MeasureTrailingSpaces);
The code for drawing the text:
for (int i = 0; i < document.Text.Count; i++)
g.DrawString(document.Text[i], Font, Brushes.Black, new PointF(offset - 2, offset + Font.Height * i), sf);
For instance, when drawing "eeeeeeee", in Consolas 9, every now and then, two e's will be 1 pixel closer to another. Is there a way to display the text correctly, monospaced?
(My actual problem: I'm making my own textbox control from scratch, as an exercise and for fun. It's going quite well actually, but the panel displaying text should be able to position the caret according to the mouse click location. Finding the correct line is easy, because of the Font's Height property. Finding the current index in that line, however, is not. For non-monospaced fonts I can't imagine a way to determine the caret index, but for monospaced fonts it should be doable, since the width of a symbol is fixed. So, maybe there is a better way to determine this?)
Kind regards, Jacco