首页 > 解决方案 > Application.SetCompatibleTextRenderingDefault(false); 时出现运行时错误 已设置

问题描述

尝试从Console应用程序转换为应用Winform程序。以下Winform代码编译良好,但在运行时出现以下错误。我在网上经历了类似错误的各种解决方案,但仍然有点困惑。也许,这里有人可以为我的以下特定代码提供帮助:

注意:这可能与这篇文章的问题无关。但以防万一:我micaut 1.0 Type Library在我的VS2017项目中引用了以下代码所需的内容。

错误[在 Winform 上]:

在应用程序中创建第一个 IWin32Window 对象之前,必须调用 SetCompatibleTextRenderingDefault。

来自控制台应用程序的代码

using System;
using System.Windows.Forms;
using micautLib;

namespace MathInputPanel
{
    static class Program
    {
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            MathInputControl ctrl = new MathInputControlClass();
            ctrl.EnableExtendedButtons(true);
            ctrl.Show();
            ctrl.Close += () => Application.ExitThread();
            Application.Run();
        }
    }
}

尝试将上述代码转换为 Winform 应用程序[出现错误]:

using System;
using System.Windows.Forms;
using micautLib;

private void button1_Click(object sender, EventArgs e)
{
    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false); //error occurs here

    MathInputControl ctrl = new MathInputControl();

    ctrl.EnableExtendedButtons(true);
    ctrl.Show();
    ctrl.Close += () => Application.ExitThread();
    Application.Run();
}

标签: c#office-interop

解决方案


正如错误试图告诉您的那样,您只能在创建第一个表单之前调用该函数。

将其移至Main()(如果它不存在)。


推荐阅读