2010-04-28 72 views
2

我需要將我的System.Windows.Forms.Form顯示爲非託管C++ HWND的子窗口。這是C#SDK代碼檢索的NativeWindow:託管窗體作爲非託管HWND的子代

public static NativeWindow MainWindow() 
{ 
    Diagnostics.Process process = Diagnostics.Process.GetCurrentProcess(); 
    if (null == process) 
    return null; 
    IntPtr handle = process.MainWindowHandle; 
    if (IntPtr.Zero == handle) 
    return null; 

    NativeWindow wnd = new NativeWindow(); 
    wnd.AssignHandle(handle); 

    return wnd; 
} 

這是它是如何在插件中實現:

IWin32Window rh_wnd = Rhino.RhinoApp.MainWindow(); 
DocEditor.Show(rh_wnd); 

這個工程....大部分時間。但它也往往不能在第一時間我把這個代碼:

HWND Error http://www.freeimagehosting.net/uploads/f29bc27823.png

再次調用它,一切工作正常。 這是怎麼回事?!?

+0

指針問題.... – 2010-04-29 00:53:00

回答

2

可能是因爲rh_wnd爲空?至少有兩種情況,您將從MainWindow()返回null。可能是一個好主意,檢查

IWin32Window rh_wnd = Rhino.RhinoApp.MainWindow(); 
if (rh_wnd != null) 
    DocEditor.Show(rh_wnd); 

如果上述停止錯誤,你可能要檢查它的上述條件返回null,並從那裏走。

希望這會有所幫助。