在項目中,用From2 啟動 Report 正常,用From1 啟動 Report 失敗,日志:
2007-05-12 13:11:06
StartGenerateReportTask: System.Runtime.InteropServices.SEHException: 外部元件發生異常。
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
at System.Windows.Forms.ComponentManager.System.Windows.Forms.UnsafeNativeMethods+IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.RunDialog(Form form)
at System.Windows.Forms.Form.ShowDialog(IWin32Window owner)
at System.Windows.Forms.Form.ShowDialog()
at ZTE.CCG.Report.ReportController.StartGenerateReportTask(String contractHeader) in ***/report/quotationgenerationmodule/reportcontroller.cs:line 469
由于From2 和 Report 是同一個人開發的,直接啟動 ReportController 是沒有問題的,而我們自己通過相同的方法啟動 ReportController 就出現了問題。開始以為是我們程式那裡出現了問題,跟蹤代碼發現這個異常不固定,很難确定是那裡出現了問題。
雖然顯示的外部組建發生異常,可應用程式中沒有調用任何其他的第三方應用程式哦。
對比Form1 和Form2 發現:
Form1:
[STAThread]
public static void Main(string[] args)
{
// lots of code...
// it's same of the code posted on Form2.
}
Form2:
Application.EnableVisualStyles();
Application.DoEvent();
更改Form1中的代碼繼續執行,程式正常執行,沒有任何錯誤。
取消 那兩句話,錯誤又再次出現。
再查詢了一下MSDN ,發現 :
“此方法啟用應用程式的 Windows XP 可視化樣式。如果控件和作業系統支援可視化樣式,則控件将以這種樣式進行繪制。若要使 EnableVisualStyles 生效,必須在應用程式中建立任何控件之前調用它;EnableVisualStyles 通常是 Main 函數的第一行。當調用 EnableVisualStyles 時,無需單獨的清單即可啟用可視化樣式。
對于支援 FlatStyle 屬性的控件,請確定将 FlatStyle 屬性設定為 FlatStyle.System 值。”
搜尋 FlatStyle ,發現在 Report 裡面很多自定義控件這麼寫的:
this.xxxControlName.FlatStyle = System.Windows.Form.FlatStyle.System;
也就是說,控件是使用的系統的樣式。在使用系統樣式的時候必須告訴系統啟用虛拟樣式。
否則可能出現上述應用程式異常。
為了驗證,這個猜想,在From1 中注釋掉:
Application.EnableVisualStyles();
Application.DoEvent();
果然程式執行時回出現異常。
在google 上搜尋了一下,發現很多上述類似異常的都或多或少提及到XP 樣式了。
相關資源:
1).NET 1.1 Framework Fix:
<a href="http://support.microsoft.com/Default.aspx?kbid=899511">http://support.microsoft.com/Default.aspx?kbid=899511</a>
2)VS2005 中解決方法:
<a href="http://support.microsoft.com/kb/897298">http://support.microsoft.com/kb/897298</a>
<a href="http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=48878&SiteID=1">http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=48878&SiteID=1</a>
<a href="http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=388724&SiteID=1">http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=388724&SiteID=1</a>