目的:输出ForeGroundWindow's Title
using System;
using System.Runtime.InteropServices;
using System.Text; 
using System.Diagnostics;
namespace 遍历窗口
{
public delegate bool CallBack(IntPtr hwnd, int lParam);

/// <summary>
/// Class1 的摘要说明。
/// </summary>
class EnumWindowsApp
{
public static int i = 0;
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
public static void Main(string[] args)
{
CallBack myCallBack = new CallBack(EnumWindowsApp.Report);
CloseWindow();
EnumWindows(myCallBack, 0);


}
[DllImport("User32.dll")] public static extern int EnumWindows(CallBack x, int y);
[DllImport("User32.dll")]
public static extern int GetWindowText(IntPtr hwnd, ref StringBuilder lpString, long cch);
[DllImport("User32.dll")]
public static extern IntPtr GetActiveWindow();
[DllImport("User32.dll")]
public static extern IntPtr GetForegroundWindow();
public static void CloseWindow()
{
IntPtr ForegroundWindowHandle = GetForegroundWindow(); 
string WindowTitleName = WindowName(ForegroundWindowHandle);
}
public static string WindowName(IntPtr handle)
{
IntPtr WINDOW_HANDLE = handle;
StringBuilder WindowTitle = new StringBuilder(255);
int Title_Length = GetWindowText(WINDOW_HANDLE,ref WindowTitle,255);
string Title_Name = WindowTitle.ToString();
if (Title_Length>0) Debug.WriteLine(Title_Name);
return Title_Name;
}
public static bool Report(IntPtr hwnd, int lParam) {   
Debug.Write("Window handle is :");
Debug.WriteLine(hwnd);
Debug.WriteLine(EnumWindowsApp.i);
EnumWindowsApp.i++;

/*
string WindowTitle = "";
int Title_Length = GetWindowText(hwnd,ref WindowTitle,255);
if (Title_Length>0) Debug.WriteLine(WindowTitle); */
return true; }
}
}