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