ActiveX控件仅接受GraphicsUnit.Point中定义的字体。参数名称:font

时间:2014-05-14 05:47:08

标签: c# activex axhost

我在VS2010中有C#.Net 3.5项目,我想动态添加ActiveX控件,我跟着文章 http://www.codeproject.com/Articles/10822/Dynamically-adding-ActiveX-controls-in-managed-cod

        Type type = Type.GetTypeFromProgID(strProgId, true);
        m_axCtrl = new AxControl(type.GUID.ToString());

        ((ISupportInitialize)(m_axCtrl)).BeginInit();
        SuspendLayout();

        m_axCtrl.Enabled = true;
        m_axCtrl.Name = "axCtrl";
        m_axCtrl.TabIndex = 0;

        Controls.Add(m_axCtrl);
        Name = "AxForm";
        ((ISupportInitialize)(m_axCtrl)).EndInit();
        Resize += new EventHandler(AxForm_Resize);
        ResumeLayout(false);
        OnResize();
        Show();

但是当我尝试将ActiveX添加到我的WinForms(Controls.Add(m_axCtrl);)时,

我收到错误消息

  

“ActiveX控件只接受在中定义的字体   GraphicsUnit.Point。参数名称:字体“

当我查看来自Microsoft的AXHost源代码时。它来自

    /// <devdoc>
    ///     Maps from a System.Drawing.Font object to an OLE IFont
    /// </devdoc>
    [EditorBrowsable(EditorBrowsableState.Advanced)]
    protected static object GetIFontFromFont(Font font) {
        if (font == null) return null;

        if (font.Unit != GraphicsUnit.Point)
            throw new ArgumentException(SR.GetString(SR.AXFontUnitNotPoint), "font");

        try {
            return (UnsafeNativeMethods.IFont)UnsafeNativeMethods.OleCreateIFontIndirect(GetFONTDESCFromFont(font), ref ifont_Guid);
        }
        catch {
            Debug.WriteLineIf(AxHTraceSwitch.TraceVerbose, "Failed to create IFrom from font: " + font.ToString());
            return null;
        }
    }

所以我想我应该将我的FontGraphicsUnit改为Point。但我不知道如何让它发挥作用。任何帮助都将非常感激。

1 个答案:

答案 0 :(得分:2)

这在过去为我解决了这个问题:

        // Due to an exception you will get at runtime, the Font needs to be defined in points. 
        // The .net error will say "ActiveX controls only accept fonts that are defined in GraphicsUnit.Point. Parameter name: font"
        // This resolves that problem. Must be run before the Init 
        base.Font = new System.Drawing.Font("Microsoft Sans Serif", 12.0F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)0)); 

我把它放在ActiveX的构造函数中。

希望这有帮助。