5

My users do a lot of bi-directional text editing, it is not uncommon for them to sprinkle the text with some Left-to-right and/or Right-to-left marks. Sometimes they want to see where those marks are located in the text in order to move or delete them.
The TextBox control in Windows Forms offers a default context menu with some Unicode-related entries, one of which is Show Unicode control characters

Enabling this option will force the control to draw the glyphs defined in the corresponding font for those non-printable characters.

For example, if I set the Text property of a TextBox control to "Hello \u200E World!" and enable this option, I will get the LRM character rendered using its glyph as defined in the font file (font used is Segoe UI).

If we open the Segoe UI font in a font-editing software (I used FontForge), we can see that indeed there are glyphs defined for the LRM and RLM code points.

I also found that StringFormatFlags enumeration can be used to control how these characters are rendered in GDI+, specifically by providing the DisplayFormatControl flag to the StringFormat object:

private void Form_Paint(object sender, PaintEventArgs e)
{
    var text = "Hello \u200E World!";
    var g = e.Graphics;

    // Will draw LRM symbol with its representative glyph
    var fmt = new StringFormat(StringFormatFlags.DisplayFormatControl);
    g.DrawString(text, Font, Brushes.Black, 10, 10, fmt);
}

However, I haven't found anything similar in WPF TextBox or RichTextBox control: the default context menu provides only Copy, Cut and Paste commands and none of the control properties (including attached ones) seem to enable the drawing of control characters with their font-defined glyphs.

Is there a way to draw Unicode control characters with their representative glyphs in WPF TextBox or RichTextBox without resorting to various hacks, such as replacing those characters with other ones in a converter?

Mikhail
  • 59
  • 1
  • 3
  • WPF text controls obviously do not support this. How about adding a WindowsFormsHost to your Window and coding something like this: `var tBox = new System.Windows.Forms.TextBox {Text = "Hello \u200E World!"}; formsHost.Child = tBox;` ? – LocEngineer Dec 04 '17 at 16:19
  • @LocEngineer using WindowsFormsHost is my "plan B" if there is absolutely no way to achieve it using native WPF controls. The main reason I don't just switch to winforms host is that controls look ugly and I'm not very good at theming winforms controls. I'm still hoping somebody experienced the same issue and came up with a good way of enabling this option. Everywhere it says that WPF text handling is an improvement in every way compared to WinForms, so perhaps there is still hope! :) – Mikhail Dec 04 '17 at 18:32

0 Answers0