Measure a String without using a Graphics object?

前端 未结 5 1078
梦谈多话
梦谈多话 2020-11-27 19:57

I am using pixels as the unit for my font. In one place, I am performing a hit test to check if the user has clicked within the bounding rectangle of some text on screen. I

5条回答
  •  Happy的楠姐
    2020-11-27 20:37

    This may not be but a repeat of others, but my problem was the unwanted Graphics object as well. After listening to the above frustrations I simply tried:

    Size proposedSize = new Size(int.MaxValue, int.MaxValue);
    TextFormatFlags flags = TextFormatFlags.NoPadding;
    Size ressize = TextRenderer.MeasureText(content, cardfont, proposedSize, flags);
    

    (where 'content' is the string to measure and cardfont the font it is in)

    ... and was in luck. I could use the result to set the width of my column in VSTO.

提交回复
热议问题