public partial class WebForm2 : System.Web.UI.Page
{
[DllImport("user32.dll", EntryPoint = "PostMessage")]
public static extern int PostMessage(IntPtr hwnd, int wMsg, int wParam, int lParam);
[DllImport("User32.dll ", EntryPoint = "FindWindow")]
private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);//关键方法
[DllImport("user32.dll", SetLastError = true)]
private static extern int SendMessage(IntPtr HWnd, uint Msg, int WParam, int LParam);
public const int WM_SYSCOMMAND = 0x112;
public const int SC_MINIMIZE = 0xF020;
public const int SC_MAXIMIZE = 0xF030;
protected void Page_Load(object sender, EventArgs e)
{
MiniMizeAppication("yodaodict");
}
private void MiniMizeAppication(string processName)
{
Process[] processs = Process.GetProcessesByName(processName);
if (processs != null)
{
foreach (Process p in processs)
{
IntPtr handle = FindWindow(null,p.MainWindowTitle);
//IntPtr handle = FindWindow("YodaoMainWndClass",null);
PostMessage(handle, WM_SYSCOMMAND, SC_MINIMIZE, 0);
}
}
}
}
FindWindow可以用来查找窗口的句柄。PostMessage和SendMessage都可以向应用程序发送消息。
FindWindow中两个参数可以通过SPY++来获取。一个是注册的类名,一个是窗体的标题。
上面这个简单的demo就是最小化有道词典的例子