日语Windows OS上的C#应用程序-以全角字符显示拉丁语 [英] C# application on Japanese Windows OS - Present Latin as Full-Width characters
问题描述
我有一个C#winform应用程序,该应用程序安装在日语Windows 7上。
有些标签显示的字体非常宽,导致它们的字体大小不正确。
I have a C# winform application, that is installed on a Japanese windows 7. Some of the labels are presented with a very wide fonts, causing them to not mach the size of the from.
经过一些研究,我被告知可能是半角/全角问题。
是否有任何办法可以强制所有字符串显示为半角宽度?
After some research I was told it might be a half/full width issue. Has it any way to force all strings to be presented as Half width?
例如,此部分显示不正确:
For example, this part is not shown correctly:
modelSizeLabel.Text = String.Format("X:{0:0.0},Y:{1:0.0},Z:{2:0.0} [{3}]",
(Model.BBox.dx),
(Model.BBox.dy),
(Model.BBox.dz - Model.Sink),
uc.To.ToString() //units enum
);
推荐答案
基本上我知道有2种方法宽度的字母:
Basically there are 2 approaches I know to deal with full-width letters:
1。使用 String.Normalize()方法
1. Using String.Normalize() method
此方法使用标准的 Unicode归一化将全角(zenkaku)转换为半角(hankaku)时的形式:
This approach uses standard Unicode normalization forms when converting full-width (zenkaku) to half-width (hankaku):
public static string ToHalfWidth(string fullWidth)
{
return fullWidth.Normalize(System.Text.NormalizationForm.FormKC);
}
NB:这被认为是转换字母,数字和标点符号的最简单方法用日语IME编写的ANSI编码,但我仍然不知道它如何影响任何假名/汉字字母。
NB: This is considered simplest approach to convert letters, numbers and punctuations covered by ANSI encoding written in Japanese IME, but I still don't know how it impact any kana/kanji letters.
2。使用P / Invoke在kernel32.dll中调用 LCMapString
方法
2. Using P/Invoke to call LCMapString
method in kernel32.dll
此方法需要调用外部DLL资源 kernel32.dll
与 API方法 LCMapString
,并在 LCMapStringEx
函数(注意某些标志是互斥的,请归功于 rshepp / John Estropia ):
This approach requires calling external DLL resource kernel32.dll
with API method LCMapString
, with flag defined in LCMapStringEx
function (note that some flags are mutually exclusive, implementation credit to rshepp/John Estropia):
// edited from /a/40836235
private const uint LOCALE_SYSTEM_DEFAULT = 0x0800;
private const uint LCMAP_HALFWIDTH = 0x00400000;
[DllImport("kernel32.dll", CharSet = CharSet.Unicode)]
private static extern int LCMapString(uint Locale, uint dwMapFlags, string lpSrcStr, int cchSrc, StringBuilder lpDestStr, int cchDest);
public static string ToHalfWidth(string fullWidth, int size)
{
StringBuilder sb = new StringBuilder(size);
LCMapString(LOCALE_SYSTEM_DEFAULT, LCMAP_HALFWIDTH, fullWidth, -1, sb, sb.Capacity);
return sb.ToString();
}
用法示例:
// by default Japanese IME automatically convert all vocal letters to respective kana letters,
// so I used consonants except "n"
Label1.Text = ToHalfWidth("0123456789bcdfghjklmpqrstvwxyz");
Label2.Text = ToHalfWidth("0123456789bcdfghjklmpqrstvwxyz", 256);
PS:您可以将以上两种方法包装在一个helper / service类中,以在同一名称空间中使用。
PS: You can wrap both methods above in a helper/service class for usage across the same namespace.
相关问题:
这篇关于日语Windows OS上的C#应用程序-以全角字符显示拉丁语的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!