【问题标题】:Run time Error when Application.SetCompatibleTextRenderingDefault(false); is setApplication.SetCompatibleTextRenderingDefault(false) 时的运行时错误;已设置
【发布时间】:2019-05-17 02:18:13
【问题描述】:

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

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

错误 [在 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


    【解决方案1】:

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

    把它移到Main()(如果它还没有的话)。

    【讨论】:

    • 您的建议有效(谢谢)。但由于它是一个Winform 应用程序,其中Program.cs 已经设置了Application.SetCompatibleTextRenderingDefault(false);。我不必移动那条线——而是从我上面的button1_Click(…) 事件中删除。您可能想在回复中提及这一点(因为它会帮助其他人)。我会以Answer 回复您。关于您的类似回复,请参阅我的comment
    • @nam:澄清
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-06
    • 2015-05-04
    • 2015-01-26
    相关资源
    最近更新 更多