time size of Label (Compact Framework)
I refer to the Visual Studio design view. When adding a label using the full .NET framework, System.Windows.Forms.Label
controls have an AutoSize
property set to True by default. If I drop a Label
control onto a form, the default font is "Microsoft Sans Serif, 12pt, Regular" and the height of the control is 20. Change the font to (say) "Tahoma, 14pt, Bold" and the height automatically changes to 23. Fine.
But in the CF (Compact-Framework), Label
controls do not have the AutoSize
property. When I change the font as described above the height of the Label
does not change (staying at 20) and some of the text is chopped off).
My question is: Short of opening a full .NET project and testing my font selection, is there a way to know what height to set my Label
at design-time?
You can use the graphics object for to measure the height of the string, for example in Paint
event or OnPaint
method:
Graphics g = e.Graphics;
float lineHeight = g.MeasureString(this.Text, this.Font).Height;
it can be used in inherited label. for example you can override OnPaint
method or somewhere else and do this, then it will run in design-time.
More resources:
DrawText
from coredll.dll
. 上一篇: 更改数量滚动条在面板上移动