Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I tried out your <code>GetFontWidth()</code>, and the width of a character returned was <strong>7</strong>.<br> I then tried out <code>TextRenderer.MearureText</code> on varying lengths of text and had values ranging from <strong>14</strong> through to <strong>7.14</strong> for text of length 1 to 50 respectively with an average character width of <strong>7.62988874736612</strong>.</p> <p>Here is the code I used:</p> <pre><code>var text = ""; var sizes = new System.Collections.Generic.List&lt;double&gt;(); for (int i = 1; i &lt;= 50; i++) { text += (i % 10).ToString(); var ts = TextRenderer.MeasureText(text, this.Font); sizes.Add((ts.Width * 1.0) / text.Length); } sizes.Add(sizes.Average()); Clipboard.SetText(string.Join("\r\n",sizes)); </code></pre> <p>Not satisfied with the results of my little 'experiment', I decided to see how the text was rendered onto the form. Below is a screen capture of the form (magnified 8x).</p> <p><a href="https://i.stack.imgur.com/BPb6F.png" rel="nofollow noreferrer" title="click for full size"><img src="https://i.stack.imgur.com/BPb6F.png" alt="Magnified font measurement" title="click for full size"></a></p> <p>On close inspection, I observed that </p> <ol> <li>There was an amount of separation between the characters. This made the length of a block of text (<code>1234567890</code>) is <strong>74</strong> pixels long.</li> <li>There is some space (3px) in front of the text being drawn even though the left padding is 0.</li> </ol> <h3>What does this mean to you?</h3> <ul> <li>If you use your code to calculate the width of a font character, you fail to account for the separating space between two characters.</li> <li>Using the <code>TextRenderer.DrawText</code> can give you varying character widths rendering it quite uselesss.</li> </ul> <h3>What are your remaining options?</h3> <ul> <li>The best way I can see out of this is to hard-code the placement of your text. That way you know the position of each character and can accurately place the cursor at any desired location.<br> Needless to say, this is likely going to call for a lot of code.</li> <li>Your second option is to run tests like I did to find the length of a block of text and then divide by the length of the block to find the average character width.<br> The problem with this is that your code is not likely to scale properly. For example, changing the size of the font or the user's screen DPI can cause a lot of trouble for the program.</li> </ul> <h3>Other things I observed</h3> <ul> <li>The space inserted in-front of the text is equivalent to the width of the caret (<em>2px</em> in my case) plus 1px (Total of 3px).</li> <li>Hard-coding the width of each character to 7.4 works perfectly. </li> </ul>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload