IDeviceContext OR How to MeasureText out of the Paint Event
-
Hello, I'd like to Resize a control that contains any text. If I use TextRenderer.MeasureText there is always a Padding, also if I use the Flag NoPadding:
Size size = TextRenderer.MeasureText("dddd", this.Font, new Size(Int32.MaxValue, Int32.MaxValue), (TextFormatFlags.NoPadding));
I found out that i need to set a IDeviceContext like that:private void MyControl_Paint(object sender, PaintEventArgs e) { Size size = TextRenderer.MeasureText(e.Graphics, "dddd", this.Font, new Size(Int32.MaxValue, Int32.MaxValue), (TextFormatFlags.NoPadding)); }
My Problem is: I dont want to Resize the Control in the Paint-Event, so I guess I need to get somehow the current IDeviceContext, but how? Or do I use the TextRenderer.MeasureText method in a wrong way? Thank You for Your Answers! -
Hello, I'd like to Resize a control that contains any text. If I use TextRenderer.MeasureText there is always a Padding, also if I use the Flag NoPadding:
Size size = TextRenderer.MeasureText("dddd", this.Font, new Size(Int32.MaxValue, Int32.MaxValue), (TextFormatFlags.NoPadding));
I found out that i need to set a IDeviceContext like that:private void MyControl_Paint(object sender, PaintEventArgs e) { Size size = TextRenderer.MeasureText(e.Graphics, "dddd", this.Font, new Size(Int32.MaxValue, Int32.MaxValue), (TextFormatFlags.NoPadding)); }
My Problem is: I dont want to Resize the Control in the Paint-Event, so I guess I need to get somehow the current IDeviceContext, but how? Or do I use the TextRenderer.MeasureText method in a wrong way? Thank You for Your Answers!Solution:
IDeviceContext deviceContext = this.CreateGraphics();
ARTICLE AT: Friday, April 28, 2006 Calculating Size of Multi-Line WinForm Label With MeasureText http://geekswithblogs.net/paulmehner/Default.aspx -
Solution:
IDeviceContext deviceContext = this.CreateGraphics();
ARTICLE AT: Friday, April 28, 2006 Calculating Size of Multi-Line WinForm Label With MeasureText http://geekswithblogs.net/paulmehner/Default.aspxDon't forget to dispose of the Graphics object you just created. It's a very common mistake that leads to resource leaks.
Regards, mav -- Black holes are the places where God divided by 0...