2009-08-11 76 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