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.
This resolved it for me in the past:
I placed that in the constructor of the ActiveX.
Hope this helps.