Skip to content

r-pankevicius/UnicodeInWinForms

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Unicode In WinForms

Use CompatibleTextRendering=false to avoid issues displaying Unicode Emoji characters, see image below.

But that's too easy if you control the launcher app

Change Main() method in Program.cs to this and you'll run into "Ancient launcher" situation (like here, I was using ArcMap as a container):

/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
	Application.EnableVisualStyles();

	// This is what WinForms app wizard creates today
	//Application.SetCompatibleTextRenderingDefault(false);

	// This is what you may expect if running in "old container"
	Application.SetCompatibleTextRenderingDefault(true);

	Application.Run(new Form1());
}

.. and Unicode chars in all labels are not displayed anymore.

If the container has called SetCompatibleTextRenderingDefault you can't change it to another value. Trying to execute Application.SetCompatibleTextRenderingDefault(false) later will give you an exeption:

System.InvalidOperationException:
SetCompatibleTextRenderingDefault must be called before the first IWin32Window object is created in the application.

How to solve this problem?

The solution

The Segoe UI Emoji font was the solution. The dialog now looks like this: If container has called Application.SetCompatibleTextRenderingDefault(true) then only the last label using Segoe UI Emoji font is displayed OK.

If you need to create font for dynamically added controls:

static readonly Font FontForLabelWithUnicodeChars = new Font("Segoe UI Emoji", 8.25f);

If "Segoe UI Emoji" is not found in the system, you'll get the fallback font "Microsoft Sans Serif".

If in legacy forms app you want just to change font for only these labels that contains unicode chars, you can check it with code:

bool hasUnicodez = label.Text.Any(ch => char.GetUnicodeCategory(ch) == System.Globalization.UnicodeCategory.Surrogate);`

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages