我已經建立了一個包含Winform的C#dll。我想從Excel VBA打開表單。第一次嘗試總是成功的。它打開並做所有我想要它做的事情。然後我關閉使用右上角的紅色XExcel VBA和C#DLL:實例化WinForms對象只能執行一次。第二次嘗試有一個錯誤
它,當我嘗試打開它第二次我總是得到錯誤信息:「在應用程序中創建第一個IWin32Window對象之前SetCompatibleTextRenderingDefault必須叫」 來源:System.Windows.Forms
WinForms類是由Visual Studio生成的標準類。我實例它在不同的類:
namespace Schnittstellenvererbung
{
[ComVisible(true), Guid("5D16EABF-B89F-45A1-8E4D-ACFAA084BF6F")]
[InterfaceType(ComInterfaceType.InterfaceIsDual)]
public interface ISchnittstellenvererbung
{
string ShowForm1();
}
[ComVisible(true), Guid("6A9EF0BB-BFF3-456E-B025-CB6A25F81F59")]
[ProgId("Test.SchnittstellenvererbungExecuteProgram")]
[ClassInterface(ClassInterfaceType.None)]
public class ExecuteProgram : ISchnittstellenvererbung
{
public string ShowForm1()
{
try
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Form1 Oberflaeche;
Oberflaeche = new Form1();
Oberflaeche.ShowDialog();
}
catch(Exception e)
{
MessageBox.Show(e.Message + e.Source);
}
return "Done";
}
}
}
在Excel VBA調用非常簡單:
Sub test()
Dim y As String
Dim x As New Schnittstellenvererbung.ExecuteProgram
y = x.ShowForm1
End Sub
當我關閉Excel並重新打開它,它再次工作的第一次嘗試,但同樣的第二次嘗試失敗。
我想知道爲什麼會出現此錯誤,因爲SetCompatibleTextRenderingDefault方法始終在插入表單之前調用。
想法?
也許這可以幫助https://social.msdn.microsoft.com/Forums/windows/en-US/26960a4c -040a-40b4-baea-15adc275b44c/setcompatibletextrenderingdefault-must-before-the-first-iwin32window-object-is-created-in?forum = winforms – GuidoG
另外不要忘記處置Overflaeche。 GC做了很有趣的事情,如果你不這樣做,我不會驚訝,如果它會成爲問題的一部分 – GuidoG
可能是它是問題的一部分,但處置並不能解決問題。我在函數中添加了close和dispose命令。僅用於測試: –