On Oct 17, 11:03*am, "Jeff Johnson" <i....@enough.spam> wrote:
> "Dom" <dolivas...@gmail.com> wrote in message
>
> news:ed13a456-6f08-4da0-8910-(E-Mail Removed)...
>
> > The rectangle for each word is obtained through
> > CreateGraphics().MeasureString which gets passed four arguments:
> > The problem is that each word has a noticable white space trailing
> > it. *Sometimes it is large, sometimes small, but always there. *This
> > makes the text look strange, especially with a space coming before the
> > commas.
>
> I seem to recall reading that MeasureString() is simply not reliable. I
> believe it has to do with GDI+ itself. But it's been quite a while since
> I've seen any discussion on the issue, so I could be remembering things
> wrong.
I now have the answer to this question, and I want to complete this
post in case others have a similar problem.
First, CreateGraphics().MeasureString, and TextRenderer.MeasureText
don't do a good job of measuring a string. After some testing, it
seems like it pads the ending, and spaces are not measured accurately.
Also, CreateGraphics().DrawString does a poor job, since it does not
draw the string at the requested X and Y. It seems to start drawing
at X+?.
This site does a good job of explaining why:
http://windowsclient.net/articles/gdiptext.aspx
This site gives two work arounds:
http://www.codeproject.com/KB/GDI-pl...urestring.aspx
But the two work arounds still did not give me the precision I
needed. The solution is really remarkably simple. Bypass the
graphics object in CSharp altogether. Go directly to the Windows
API. You need GetTextExtentPoint32, TextOut, and several related
API's, such as BeginPaint, EndPaint, GetDC, etc.
This will give you absolute precision. I can now get the exact letter
that the mouse is hovering over. I can block a string of text at
will, and I can even insert a carat at the right place when the mouse
is clicked. IT doesn't matter what font is used. It is always
accurate. Nothing else is needed.