gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 10:45:21 24 4
gpt4 key购买 nike

我在 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 controls only accept fonts that are defined in GraphicsUnit.Point. Parameter name: font"

当我查看 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。但我不知道如何让它发挥作用。在这方面的任何帮助将不胜感激。

最佳答案

这在过去为我解决了:

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

希望这对您有所帮助。

关于c# - ActiveX 控件只接受在 GraphicsUnit.Point 中定义的字体。参数名称 : font,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23646550/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com