2

I have C#.Net 3.5 project in VS2010 where I would like to add ActiveX Control dynamically and I followed article 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();

But when I try to add ActiveX to my WinForms (Controls.Add(m_axCtrl);)

I get error message

"ActiveX controls only accept fonts that are defined in GraphicsUnit.Point. Parameter name: font"

And when I looked into the AXHost source code from Microsoft. It is coming from

    /// <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;
        }
    }

So I guess I am supposed to change my FontGraphicsUnit to Point. But I dont know how to make it work. Any help in this would be greatly appreciated.

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
Saqwes
  • 325
  • 3
  • 12

1 Answers1

2

This resolved it for me in the past:

        // 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)); 

I placed that in the constructor of the ActiveX.

Hope this helps.

TravisWhidden
  • 2,142
  • 1
  • 19
  • 43