2010-03-08 14 views
2

我試圖讓我的C#表單在第三方應用程序中正確地成爲父對象,我擁有控件的句柄,我希望我的表單成爲父對象,但是不能似乎得到它的工作。在第三方應用程序中創建窗口作爲孩子

alt text http://img693.imageshack.us/img693/8871/examplec.jpg

我想創建自己的狀態,以便它是MDICLIENT的一部分,處理005E0ED6。就像Window 01D7157D一樣。

這可能嗎?如果是這樣可以在C#中完成?

回答

2

你是怎麼試過的?你試過SetParent?看到下面的StackOverflow問題,看看它是否有幫助。 Embedding HWND into external process using SetParent

+0

是的我試過設置Parent,但我的表單是在MDIClient之外創建的,似乎並不想成爲它的一部分。 – 2010-03-08 02:59:46

+0

你可以顯示代碼嗎? – Josh 2010-03-08 03:00:51

+0

啊我還沒有看到這個問題,我看看它。 – 2010-03-08 03:01:06

1

此代碼似乎工作:

[DllImport("user32.dll")] 
    private static extern 
     IntPtr GetWindowThreadProcessId(IntPtr hWnd, IntPtr ProcessId); 

    [DllImport("user32.dll")] 
    private static extern 
     IntPtr AttachThreadInput(IntPtr idAttach, IntPtr idAttachTo, int fAttach); 

     WinAPI.SetParent(this.Handle, otherappshandle); 

     IntPtr otherprocessID = GetWindowThreadProcessId(otherappshandle, new IntPtr(0)); 
     IntPtr threadID = new IntPtr(AppDomain.GetCurrentThreadId()); 

     AttachThreadInput(threadID , otherprocessID , 1); 
+0

該變量應該被命名爲'otherThreadID'而不是'otherprocessID',因爲GetWindowThreadProcessId()的返回值是一個線程ID – torvin 2011-05-22 17:56:07

0

好運。我走下了這條路,發現我最終放棄了足夠多的惱人的陷阱。

的setparent()等將讓你的存在方式部分,但有一幫小陷阱的就眼睜睜的看着整個系統(消息泵堵塞等),只是使它成爲一個時間片。

對於WinForms,特別是,我強烈建議只在主進程中運行UI(如果可以的話),並且如果要在另一個進程中隔離處理,請改爲執行此操作。

相關問題