对于把sample framework做成控件最关键的地方该在于 mainloop() 函数. 我看www.mdxinfo.com上看过一篇将原来的 main()的函数改成InitializeGraphics(); 而且在方法的最后将 this.sampleFramework.MainLoop(); 删掉。 然后在主程序的main() 函数中添加 Form1 form = new Form1(); form.Show(); form.directXWindow.Framework.MainLoop(); 来运行出控件模型。
其实这里有很多值得改进的地方。我们仔细分析一下MainLoop()方法。
publicvoid MainLoop() { .. //关键就是从这里开始 using (System.Windows.Forms.Control app = Window) { // Hook the application's idle event System.Windows.Forms.Application.Idle +=new EventHandler(OnApplicationIdle); // Start the application if (app is System.Windows.Forms.Form) { System.Windows.Forms.Application.Run(app as System.Windows.Forms.Form); } else { // Show the window app.Show(); System.Windows.Forms.Application.Run(); } } State.IsInsideMainloop =false; }
System.Windows.Forms.Application.Idle += new EventHandler(OnApplicationIdle); 是在应用程序空闲的时候调用了绘图的方法。
而
if (app is System.Windows.Forms.Form) { System.Windows.Forms.Application.Run(app as System.Windows.Forms.Form); } else { // Show the window app.Show(); System.Windows.Forms.Application.Run(); }