2009-08-11 34 views

回答

0

在創建自己的應用程序窗口之前,請調用GetForegroundWindow。 否則調用GetWindow(your_hwnd,GW_HWNDNEXT)來查找指定下面的下一個窗口。

+0

要知道,有一些情況下,如果前一個窗口被關閉,這將無法正常工作,例如。 – JBRWilkinson 2010-01-19 11:28:18

1

我正在尋找相同的功能 - 我有一個應用程序保持在屏幕上打開,用戶在進入三個第三方應用程序之一時可以調用我的應用程序上的按鈕。

當他們點擊我應用程序上的按鈕時,我需要確定他們上次使用的三個應用程序中的哪一個,以便知道要與哪個數據庫進行通信。我沿着查看GetForeGroundWindow和GetWindow的路線走了,但是我從GetWindow得到的Window句柄總是指向一個標題爲M的窗口。我使用了Managed Windows API工具中的Winternal Explorer工具,並且可以找到M句柄返回並且它是我以後的過程的'孩子' - 但是從這個句柄我不能得到進程名稱。

我已經完成了一個小例子應用程序使用簡單的窗體形式 - 我讚美它,然後使記事本的焦點,然後單擊我的按鈕,我得到的句柄 - 但當看到所有進程的MainWindowHandle,它沒有列出,但使用Winternal Explorer我可以看到這是記事本進程的子進程。

任何建議,爲什麼我只得到這個子進程句柄返回而不是實際的進程句柄?

示例代碼如下 - 被一個Windows XP SP 3臺機器上運行

using System; 
using System.Runtime.InteropServices; 
using System.Windows.Forms; 

namespace TestWindowsAPI 
{ 
    public partial class Form1 : Form 
    { 
     [DllImport("user32.dll")] 
     public static extern IntPtr GetForegroundWindow(); 

     [DllImport("user32.dll", SetLastError = true)] 
     static extern IntPtr GetWindow(IntPtr hWnd, uint uCmd); 

     public Form1() 
     { 
      InitializeComponent(); 
     } 

     private void button1_Click(object sender, EventArgs e) 
     { 
      IntPtr thisWindow = GetForegroundWindow(); 
      IntPtr lastWindow = GetWindow(thisWindow, 2); 

      tbThisWindow.Text = thisWindow.ToString(); 
      tbLastWindow.Text = lastWindow.ToString(); 
     } 
    } 
} 
+0

如果您使用的是WIN32 Windows Hook,那麼您可以在第三方應用程序中執行操作時自動調用您的代碼。有關更多信息,請參閱以下文章:http://support.microsoft.com/kb/318804 – JBRWilkinson 2010-01-19 11:34:43