Adjust RichTextBox font size under High DPI setting - c#

My C# application includes grids with both simple text boxes and richtext boxes. Often the richtext boxes contain rich text copied and pasted from elsewhere, and often the rtf markup includes hardcoded font size (\fsXX, XX in half points). In most cases the rich text font size is the same or close to the simple text font size.
When the DPI scaling is set to anything other than the default 96 the rich text is distorted as follows:
a) When the application is NOT set to be DPI aware the richtext is shown smaller than the simple text and is blurry.
b) When the application is set to be DPI aware the rich text is larger than the simple text.
Is there a means to allow or force the richtext to scale with the simple text, short of editing the markup directly?

Try to set its property WordWrap to true.

One thing that might solve the problem is to set the RichTextBox on a form and set the AutoScaleMode property of the form to None (AutoScaleMode Enumeration documentation)

<script type="text/javascript">
tinyMCE.init({
mode: "textareas",
theme: "advanced",
plugins: "safari,spellchecker,pagebreak,style,layer,table,save,advhr,advimage,advlink,emotions,iespell,inlinepopups,insertdatetime,preview,media,searchreplace,print,contextmenu,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras,template,imagemanager,filemanager",
theme_advanced_buttons1: "bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,|,styleselect,formatselect,fontselect,fontsizeselect",
theme_advanced_buttons2: "cut,copy,paste,pastetext,pasteword,|,search,replace,|,bullist,numlist,|,outdent,indent,blockquote,|,undo,redo,|,link,unlink,anchor,image,cleanup,code,|,insertdate,inserttime,preview,|,forecolor,backcolor",
theme_advanced_buttons3: "tablecontrols,|,hr,removeformat,visualaid,|,sub,sup,|,charmap,emotions,iespell,media,advhr,|,print,|,ltr,rtl,|,fullscreen",
theme_advanced_buttons4: "insertlayer,moveforward,movebackward,absolute,|,styleprops,spellchecker,|,cite,abbr,acronym,del,ins,attribs,|,visualchars,nonbreaking,template,blockquote,pagebreak,|,insertfile,insertimage",
theme_advanced_toolbar_location: "top",
theme_advanced_toolbar_align: "left",
theme_advanced_statusbar_location: "bottom",
theme_advanced_resizing: false,
template_external_list_url: "js/template_list.js",
external_link_list_url: "js/link_list.js",
external_image_list_url: "js/image_list.js",
media_external_list_url: "js/media_list.js"
});
</script>
<td class="textboxmain" style="height:300px; "><asp:TextBox id="textbox1" TextMode="MultiLine" Height="100%" runat="server" placeholder="test............"></asp:TextBox></td>

Maybe you can use a WPF form, so you don't have to worry about de DPI of different screens

Please try the following, it is supported only in .NET Framework 4.5.2 onwards. Microsoft has covered a few more controls for HighDpiAutoresizing.
<appSettings>
<add key="EnableWindowsFormsHighDpiAutoResizing" value="true" />
</appSettings>

Related

How do I get the real font used by a Windows Form?

When displaying text in a Windows Form (like a TextBox for example) I can enter Chinese characters in it if I want, and they will get displayed correctly. However, the font used by the Form is not actually capable of rendering Chinese. As a result, if I try to use the Form's font in order to create a PDF file containing the text, it won't work:
myPdf.RenderText(myForm.Font, myForm.Text);
In this case in Windows 10, myForm.Font is just Microsoft Sans Serif (or Arial in older Windows versions,) which does not support Chinese (or CJK in general) and as a result the PDF won't render correctly when embedding the Arial font in it.
So, how do I find out which font is really used to render text in a Windows Form? It can't be what the Font property says it is. In this particular case, it's probably "SimSun" or some other system font that supports Chinese characters, but there doesn't seem to be any way whatsoever to find out.
How do I deal with this? If it's not possible to get the true font of a Form, then could I somehow detect what font (or combination of fonts) Windows would use to render a piece of text on a Form before actually displaying the text on that Form?

richtextbox is shrinking at run time however textbox is not

i'm at a loss as to why my parameter richtextbox is resizing its height at runtime. I'm assuming that it has something to do with my high def displays.
all other fields are simply textbox.
any suggestions as to fields I can check? Form.AutoScale is set to font.
http://imgur.com/a/S0dq0
It seems that this issue is directly related to the use of the font "Microsoft YaHei". Changing the font to San Serif immediately resolved the issue.

RibbonToggleButton displays image badly

I'm using Ribbon from RibbonControlsLibrary.dll 4.0.0.11019, .NET 4.0, C#, WPF.
It can be downloaded with a free samples here: http://www.microsoft.com/en-us/download/details.aspx?id=11877
The problem comes when a RibbonToggleButton is displayed. Then it's image seems to be a little bit broken, like a part of image is shifted for a few pixels.
EDIT: Thanks to kind people I can post and image now:
Here are some details:
Image is displayed in it's large variant
Image size is 32x32
I set image scaling to none for all the images of Ribbon
Image is broken only when the text in RibbonToggleButton has 1 line
Same image displays correctly in any other kind of button (RibbonButton, RibbonSplitButton etc.)
The image is displayed correctly when I set the font size in Windows to Medium (125%)
My OS is Windows 8
When I set the VerticalContentAlignment for the toggle button to "Bottom", the Image starts to display correctly, but the whole Ribbon starts to look ugly.
I experience this problem for all the toggle buttons, including those which are in a Microsoft's free samples.
I guess that probably the Image doesn't have enough space, so it's compressed from 32x32 to some smaller size.
I use the theme that is made of Microsoft's Generic theme, that is included in the RibbonControlsLibrary.dll. I guess I could fix the RibbonToggleButton template somehow, but I have no idea what to fix there.
Any ideas?
In the xaml declaration of your UserControl / Window put that line:
RenderOptions.BitmapScalingMode="HighQuality"

How to add "styled" documentation tooltip in C#?

How do I add "styled" (bold, italic etc.) tips in C#? Or is there any way to do this?
PS: That code on image doesn't work.
The "styling" in your image is not part of the C# specification. It's just your editor (Sublime?) that does some basic parsing, and sets the text color of items it recognizes.
You can see for yourself if you open your so-called "styled" source code in a plain text editor such as TextEdit or Notepad. Even if you can see colors again, they are assigned by that software in turn.

make Label control text look as good as it does in VS form designer

In VS2008 I designed a form for a C# dll. The dll is a plugin for a somewhat older app (ca. 2005): let's call it "OldApp". In VS form designer, the text in Label controls on my form is nicely rendered: antialiased and properly kerned. But when I bring up this form within OldApp (where the C# dll runs as a plugin), the text in Label controls looks ugly. It's legible, but the kerning is poor: the letters are spaced further apart and at seemingly random offsets. Anything I can do to make the text labels from within OldApp look as good as they do in VS's form designer? I doubt the specific font matters, but it's Arial, 7.2 pt (VS2008 default). I tried playing with the two relevant lines in Program.cs (see below), to no effect.
Application.EnableVisualStyles(); // tried using it and commenting it out
Application.SetCompatibleTextRenderingDefault(true); // tried true and false
I found a similar problem on MSDN forums that mentions adding the following line after the EnableVisualStyles() method.
Application.DoEvents()
Seems to be a bug in older .NET versions...which version are you using?
After an investigation I have some findings, so I'll just answer my own question:
The bad news: the old-style text rendering used by OldApp is what's causing the problem. I verified it by toggling the UseCompatibleTextRendering property for the label control in VS. The font distortion I see is the same one I see in OldApp. Which means that the Application.SetCompatibleTextRenderingDefault(false) line in my code has no effect. OldApp will ignore it and do old-style rendering anyway.
As suggested by DeviantSeev using a bigger font helps a bit. It doesn't get rid of the bad kerning, it just makes it less noticeable. I increased the font from 7.2pt to 8pt only (not 12pt), because the dialog box becomes too big otherwise. The way to do this is in the form's Font property (not the control's). This way, you'll change all controls uniformly (if their Font property is set to default).
The font sizes in VS appear to be discrete rather than continuous, or maybe there's an int() rounding off involved. Increasing the font from 7.2pt to 7.4pt results in very little change, while at 7.5pt the font makes a sudden jump in size.
Forms have an AutoScaleMode property. If it's set to Font and the form is resizeable, the form will resize in VS in proportion to the change in font size. This way, in VS you can find an acceptable middle ground between a (legible) font size and a bloated dialog. However, be careful: the auto-scale operation can suddenly go awry, for example if you change the Font units from points to pixels, inches, etc. You may suddenly end up with microscopic controls or a form bigger than your screen and hitting undo won't fix it. You really don't want to re-design your form again, so save it before any font unit change and then again when you're happy with what you see.

Categories

Resources