2

我在 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。但我不知道如何使它工作。对此的任何帮助将不胜感激。

4

1 回答 1

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 的构造函数中。

希望这可以帮助。

于 2014-09-23T21:09:39.793 回答