Fog Creek Software
Discussion Board

Welcome! and rules

Joel on Software


I'm really confused on this one. I'm trying to acquire where a certain portion of text ends so that I can place a cursor there (don't ask). To make a simple test of the MeasureString method, I did this:

private void HandlePaint(object sender, System.Windows.Forms.PaintEventArgs e)
    paint = e.Graphics;
    string mainString = "Come here!";

    paint.DrawString(mainString, defaultFont, defaultBrush, 0, 0);
    paint.DrawString("o", defaultFont, new SolidBrush(Color.Red),
    paint.MeasureString("C", defaultFont).Width, 0);

But it's all misaligned. The red o is printed a good few pixels to the right of the word. If this is a dumb question, I'm sorry. But I can't find any help elsewhere.

Dylan Moreland
Thursday, October 3, 2002

I seem to recall that Petzold discusses this, on page
409 in Programming Windows with C#. To avoid the extra pixels, you should use StringFormat.GenericTypographic and pass this as the 5th parameter to MeasureString.

Sorry if this turns out to be totally far off...

Roland Kaufmann
Friday, October 4, 2002

*  Recent Topics

*  Fog Creek Home