using System.Runtime.InteropServices;
using System.Text;
//1,定义
//[DllImport("User32.dll", EntryPoint = "FindWindow")]
//public extern static IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll", EntryPoint = "ShowWindow", CharSet = CharSet.Auto)]
public static extern int ShowWindow(IntPtr hwnd, int nCmdShow);
//[DllImport("user32.dll")]
//public static extern int GetWindowTextLength(IntPtr hWnd);
[DllImport("User32.dll", CharSet = CharSet.Auto)]
public static extern int GetWindowText(IntPtr hWnd, StringBuilder text, int nMaxCount);
//[DllImport("user32.dll")]
//private static extern int GetClassName(IntPtr hWnd, StringBuilder lpString, int nMaxCount);
// Callback Declaration
public delegate bool EnumWindowsCallback(IntPtr hwnd, int lParam);
[DllImport("user32.dll")]
private static extern int EnumWindows(EnumWindowsCallback callPtr, int lParam);
private EnumWindowsCallback callBackFn = new EnumWindowsCallback(ReportWindow);
private static StringBuilder title = new StringBuilder(256);
//2,回调方法的具体实现
public static bool ReportWindow(IntPtr hwnd, int lParam)
{
GetWindowText(hwnd, title, title.Capacity);
if ("TestWindow".Equals(title.ToString()))
{
//MessageBox.Show("找到了!"); //窗口的title: TestWindow
ShowWindow(hwnd, 8);
}
return true;
}
//3,调用回调方法
EnumWindows(callBackFn, 0);