[DllImport("user32.dll")]// 获取桌面上显示最前的窗口句柄
public static extern IntPtr GetForegroundWindow(); [DllImport("user32.dll")]
public static extern int GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount);
public static string GetWindowText(IntPtr hwnd)
{
StringBuilder title = new StringBuilder(256);
GetWindowText(hwnd, title, title.Capacity);
return title.ToString();
}
public static extern IntPtr GetForegroundWindow(); [DllImport("user32.dll")]
public static extern int GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount);
public static string GetWindowText(IntPtr hwnd)
{
StringBuilder title = new StringBuilder(256);
GetWindowText(hwnd, title, title.Capacity);
return title.ToString();
}
确实是获得了最前的窗口。但这个还有个问题:A程序是小窗体 设置了topmost. B程序是活动窗体。 B窗体激活, A窗体可见并且只挡住B窗体的一点(A程序窗体比B小),这时A程序并没有被B挡出。 但这里获得的是B的名字。从而程序会不断地激活A, 这是没有必要的。我希望的是只有A被其它程序遮挡了, 再激活A。