Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/300.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#helpProvider SetHelpString不支持unicode_C#_Winforms_Unicode - Fatal编程技术网

C#helpProvider SetHelpString不支持unicode

C#helpProvider SetHelpString不支持unicode,c#,winforms,unicode,C#,Winforms,Unicode,我用它来表示对我的控制的帮助 我输入控件的帮助字符串。但这并不能正确显示字符串 p/S: 我的语言是越南,是unicode字体 以下是我编程时的文字:“Chúc mừ纳尼姆ớ我“ 以下是显示的文本: 这是一个老错误,原因有两个: HelpProvider的基础API使用的默认字体不支持unicode字符 HelpProvider的基础API不支持Unicode 解决这两个问题后,可以正确显示Unicode字符: HelpExtensions.ShowPopup2(button1, "

我用它来表示对我的控制的帮助

我输入控件的帮助字符串。但这并不能正确显示字符串

p/S:

我的语言是越南,是unicode字体

以下是我编程时的文字:“Chúc mừ纳尼姆ớ我“

以下是显示的文本:


这是一个老错误,原因有两个:

  • HelpProvider的基础API使用的默认字体不支持unicode字符
  • HelpProvider的基础API不支持Unicode
解决这两个问题后,可以正确显示Unicode字符:

HelpExtensions.ShowPopup2(button1, "متن آزمایشی", Control.MousePosition);

第一个问题出现在
Help
类(,)中,该类创建了,但没有为其指定任何字体。因此,将使用不支持Unicode字符的默认字体

  • 一种可能的修复方法是使用默认字体,如支持Unicode字符的
    SystemFonts.CaptionFont
对于第二个问题,您需要在Windows中更改一个设置,为此:

  • 转到控制面板→ 区域→ “管理”选项卡,然后在“非Unicode程序的语言”部分,单击“更改系统区域设置…”按钮,然后在下一个对话框中,选择您选择的语言,例如波斯语

    或者,为了支持其他语言,您可以选择“Beta:使用Unicode UTF-8实现全球语言支持”,即Beta

下面是
HelpExtensions
类:

using System;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;
public static class HelpExtensions
{
    public static void ShowPopup2(Control parent, string caption, Point location, Font font = null, Color? backColor = null, Color? foreColor = null)
    {
        font = font ?? SystemFonts.CaptionFont;
        backColor = backColor ?? Color.FromKnownColor(KnownColor.Window);
        foreColor = foreColor ?? Color.FromKnownColor(KnownColor.WindowText);

        var popup = new HH_POPUP();
        popup.clrBackground = new COLORREF(backColor.Value);
        popup.clrForeground = new COLORREF(foreColor.Value);
        popup.pt = new POINT(location);
        var pszText = Marshal.StringToCoTaskMemAuto(caption);
        popup.pszText = pszText;
        var pszFont = Marshal.StringToCoTaskMemAuto(
            $"{font.Name}, {font.Size}, , " +
            $"{(font.Bold ? "BOLD" : "")}" +
            $"{(font.Italic ? "ITALIC" : "")}" +
            $"{(font.Underline ? "UNDERLINE" : "")}");
        popup.pszFont = pszFont;
        try
        {
            HtmlHelp(parent.Handle, null, HTMLHelpCommand.HH_DISPLAY_TEXT_POPUP, popup);
        }
        finally
        {
            Marshal.FreeCoTaskMem(pszText);
            Marshal.FreeCoTaskMem(pszFont);
        }
    }

    [Flags()]
    public enum HTMLHelpCommand : uint
    {
        HH_DISPLAY_TOPIC = 0,
        HH_DISPLAY_TOC = 1,
        HH_DISPLAY_INDEX = 2,
        HH_DISPLAY_SEARCH = 3,
        HH_DISPLAY_TEXT_POPUP = 0x000E,
        HH_HELP_CONTEXT = 0x000F,
        HH_CLOSE_ALL = 0x0012
    }

    [DllImport("hhctrl.ocx", SetLastError = true, EntryPoint = "HtmlHelpW", CharSet = CharSet.Unicode)]
    static extern int HtmlHelp(IntPtr hWndCaller,
        [MarshalAs(UnmanagedType.LPWStr)] string pszFile,
        HTMLHelpCommand uCommand,
        [MarshalAs(UnmanagedType.LPStruct)] HH_POPUP dwData);

    [StructLayout(LayoutKind.Sequential)]
    struct COLORREF
    {
        int ColorRef;

        public COLORREF(int lRGB)
        {
            ColorRef = lRGB & 0x00ffffff;
        }
        public COLORREF(Color color) : this(color.ToArgb())
        {
        }
    }
    [StructLayout(LayoutKind.Sequential)]
    class POINT
    {
        public int x;
        public int y;
        public POINT(int x, int y)
        {
            this.x = x;
            this.y = y;
        }
        public POINT(Point p) : this(p.X, p.Y)
        {
        }
    }
    [StructLayout(LayoutKind.Sequential)]
    struct RECT
    {
        public int left;
        public int top;
        public int right;
        public int bottom;
        public RECT(int left, int top, int right, int bottom)
        {
            this.left = left;
            this.top = top;
            this.right = right;
            this.bottom = bottom;
        }
        public RECT(Rectangle r) : this(r.Left, r.Top, r.Right, r.Bottom)
        {
        }
    }
    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
    class HH_POPUP
    {
        internal int cbStruct = Marshal.SizeOf(typeof(HH_POPUP));
        internal IntPtr hinst = IntPtr.Zero;
        internal int idString = 0;
        internal IntPtr pszText;
        internal POINT pt;
        internal COLORREF clrForeground = new COLORREF(-1);
        internal COLORREF clrBackground = new COLORREF(-1);
        internal RECT rcMargins = new RECT(-1, -1, -1, -1);
        internal IntPtr pszFont;
    }
}
帮助提供者2组件

我创建了一个支持Unicode字符的
HelpProvider2
组件。它还公开字体、前景色和背景色属性:

下载或克隆

  • 存储库:
  • 下载

您确定字体中包含字符吗?它将与带有helpstringIt的控件的字体相同,这是一个已知的bug()。。。遗憾的是,HelpProvider是无人使用的未知功能之一,因此它所存在的bug仍然存在。如果您可以使用
工具提示
:@Charlieface不,它没有。它使用固定字体(尝试使用Comic Sans设置控件,帮助提示仍然使用原始字体)。我在这里发布了一个答案,说明如何修复它,还创建了一个支持Unicode、字体、前景色和背景色的控件。我可能会建议对.NET 5进行修复。如果您对答案有任何疑问,请告诉我。