How to get the exact text margins used by TextRenderer
If one day you have wanted a Label or TextBox of formulaic Windows Forms that it realizes a little more how in the web, then you there is will have imagined that there is no intuitive way to do a Label or TextBox automatically to fit its height so that the text fits it contains.
Although it is possible that it is not intuitive, definitely it is not impossible.
In this example, I will use a TextBox (easily it can use a tag) that mates to the top part of a form. To use this, MyTextBox adds aTextBox called the form and establish the base in DockStyle. Top. To wire up the event(case) Resize of the picture of text to this events(cases) controler.
private void MyTextBox_Resize( object sender, EventArgs e ) { // Grab a reference to the TextBox TextBox tb = sender as TextBox; // Figure out how much space is used for borders, etc. int chromeHeight = tb.Height - tb.ClientSize.Height; // Create a proposed size that is very tall, but exact in width. Size proposedSize = new Size( tb.ClientSize.Width, int.MaxValue ); // Measure the text using the TextRenderer Size textSize = TextRenderer.MeasureText( tb.Text, tb.Font, proposedSize, TextFormatFlags.TextBoxControl | TextFormatFlags.WordBreak ); // Adjust the height to include the text height, chrome height, // and vertical margin tb.Height = chromeHeight + textSize.Height + tb.Margin.Vertical; }
If he(she) wants to change the size of one tag or a picture of text that is not connected (for example, one that is in a FlowLayoutPanel or another Panel, or has just placed in the form), next, he(she) can handle the size of the enclosed form in its place and you have only to modify straight the properties of the Control.
How to get the exact text margins used by TextRenderer
TextRenderer offers different advantages for drawing text in controls.
It lets you have free complex text support and permits your controls to show text from the same rendering engine giving a constant text layout across the User Interface.
By default, the text rendered in all controls of Microsoft .NET Framework 2.0 uses GDI and there are several controls that can’t render text using GDI+ like for example ListView, and TextBox.
In Microsoft .NET Framework 2.0, all Windows Forms controls offer the same support level for complex scripts. Some Windows Forms controls in early releases could render text using only the Graphic class with the GDI+ inherited complex scripts limitation.
By default, these controls can now render text using TextRenderer class.
For additional information regarding TextRenderer, you may visit MSDN Magazine January 2001 issue.