KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
System.Windows.Forms.TextRenderer.DrawText method renders formatted text with or without left and right padding depending on the value of the flags parameter: TextFormatFlags.NoPadding - fits the text tightly into the bounding box, TextFormatFlags.GlyphOverhangPadding - adds some left and right margins, TextFormatFlags.LeftAndRightPadding - adds even bigger margins. Now, my question is how can I get the exact amount of padding (left and right) added by DrawText to the text for a given device context, string, font etc? I've dug into .NET 4 with .NET Reflector and found that TextRenderer calculates "overhang padding" which is 1/6 of the font's height and then multiplies this value to calculate left and right margins using these coefficients: left 1.0, right 1.5 for TextFormatFlags.GlyphOverhangPadding , left 2.0, right 2.5 for TextFormatFlags.LeftAndRightPadding . The resulting values are rounded up and passed to the DrawTextExA or DrawTextExW native API functions. It's difficult to recreate this process because font's height is taken not from System.Drawing.Font but from System.Windows.Forms.Internal.WindowsFont and these classes return different values for the same font. And a lot of other internal BCL classes from the System.Windows.Forms.Internal namespace are involved. Decompiling all of them and reusing their code in my app is not an option, because that would be a serious .NET implementation dependency. That's why I need to know if there is some public API in WinForms or at least which Windows functions I can use to get the values of left and right margins. Note: I've tried to TextRenderer.MeasureText with and without padding and compare the r
Tags (comma-separated)
Save Edits
Cancel