2009-12-16 36 views
0

我們有一個C++遺留應用程序,並且使用從父C++應用程序使用COM調用的c#applet進行擴展。他們帶來了非模態的窗戶。此外,我認爲這些.NET窗口不是C++應用程序的適當子類,因爲EnumChildWindows忽略了它們,而EnumWindows發現它們。但是,一個類似於孩子的行爲仍然存在,因爲如果關閉父C++應用程序,c#窗口也會關閉。來自傳統C++應用程序的C#子進程窗口問題

我所有這一切的基本問題是,如果用戶調用其中一個c#applet,然後無意中單擊父(C++)應用程序窗口,c#窗口將下降到背景。如果用戶想要將其帶回頂端,他們應該能夠在TaskBar中單擊它的圖標。不幸的是,出於一些奇怪的原因,通常有必要單擊任務欄圖標三次!第一次應該給頂部帶來一個隱藏的窗口,但事實並非如此。第二次點擊最小化隱藏窗口,第三次將其成功還原。

有其他人在橋接遺留問題時遇到這個錯誤/功能 - >。NET鴻溝?我想知道是否可以截獲我的C#applet的任務欄圖標上的第一次單擊,並以某種方式強制它回到頂端。 :-)

我一直在嘗試用下面的:

[DllImport("User32.dll")] 
    private static extern int ShowWindow(IntPtr hwnd, IntPtr nCmdShow); 

,但即使我得到這個工作,我仍然需要攔截第一點擊鼠標。謝謝你的幫助!

回答

1

如果C#窗口實際上是子窗口,它會工作嗎?可以通過將父HWND作爲參數傳遞給C#COM對象,然後使用PInvoke在C#窗口上調用SetParent來完成此操作。 (我從來沒有這樣做過,但聽起來至少和ShowWindow和任務欄一樣安全?)

(注意SetParent的文檔中的註釋,您可能還需要擺弄子窗口?窗口標誌)

(根據C#窗口類型,它可能已經有可以使用Handle屬性,否則你可以雜牌一個的PInvoke調用FindWindow函數來獲得它的句柄)

+0

那聲音。有希望。我明天要出行,但週末會試一試! – 2009-12-17 20:02:09

+0

所以我試過這個,它確實將C#窗口變成了傳統C++窗口的子窗口。它現在位於指定父級的窗口區域內,不能再最小化到任務欄。我很可能現在必須在C++應用程序中管理它。感謝您的建議! – 2009-12-21 18:51:12

+0

//代碼... [DllImport(「User32.dll」)] static extern int GetForegroundWindow(); [DllImport(「User32.dll」)] private static extern int SetParent(int hwndChild,int hwndParent); public void ShowMyFormAsChildOf(int hwndParent) {MyForm form = new MyForm(); form.Show(); //立即調用... \t SetWindowParent(hwndParent); } private void SetWindowParent(int parenthwnd) { if(0!= parenthwnd) { int handle = GetForegroundWindow(); SetParent(handle,parenthwnd); } } – 2009-12-21 18:57:39

0
// Here's the code... 
[DllImport("User32.dll")] 
    static extern int GetForegroundWindow(); 
    [DllImport("User32.dll")] 
    private static extern int SetParent(int hwndChild, int hwndParent); 


    public void ShowMyFormAsChildOf (int hwndParent) 
    { 
    MyForm form = new MyForm(); 
    form.Show(); // immediately after .Show(), it is the foreground window! 
    SetWindowParent(hwndParent); 
    } 

    private void SetWindowParent(int parenthwnd) 
    { 
    if (0 != parenthwnd) 
    { 
     int handle = GetForegroundWindow(); 
     SetParent(handle, parenthwnd); 
    } 
    } 
+0

IntPtr會不會更安全? HWND是一種指針/句柄類型,因此您希望它在64位平臺上進行擴展。 – ChrisV 2009-12-21 19:10:48