Measure label size
I'm implementing a custom control with some la开发者_如何学运维bels on it and I need to measure the size of those labels to have an optimal layout. In this way I can properly show the control for each font and font size.
Could you tell me how can I do that, please? Thank you.The correct way to size and arrange your custom control according to the size of its sub-elements is to override MeasureOverride and ArrangeOverride.
See the links for details, but in a nutshell, your control is supposed to (in MeasureOverride):
- call
UIElement.Measure
on all children (which will include your labels), which will return the size that each of your children would like to have, - calculate your own desired size and
- return this size to the framework (by using it as the return value of
MeasureOverride
).
Afterwards, in ArrangeOverride, you get the size allocated to your control by the framework as a parameter and you
- determine how much of the space you want to allocate to each of your child elements and
- call
UIElement.Arrange
on each child element.
you might want to look up ev.Graphics.MeasureString(str,font)
精彩评论