user32.dll是Windows用户界面相关应用程序接口,用于包括Windows处理,基本用户界面等特性,如创建窗口和发送消息。
[DllImport("user32.dll", EntryPoint = "FindWindow")]
public extern static IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll", EntryPoint = "FindWindowEx")]
public static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);
[DllImport("user32.dll", EntryPoint = "SendMessage")]
public static extern int SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam);
/// <summary>
/// 是否找到窗口 是true ,否false
/// </summary>
/// <param name="title">窗口标题</param>
/// <param name="btnName">按钮名称</param>
private bool closeWindows_User32(string title,string btnName)
{
bool b = false;
const int BM_CLICK = 0xF5;
const int WM_CLOSE = 0x0010;
IntPtr maindHwnd = DataAccess.FindWindow(null, title); //程序句柄
if (maindHwnd != IntPtr.Zero)
{
IntPtr childHwnd = DataAccess.FindWindowEx(maindHwnd, IntPtr.Zero, null, btnName); //获得按钮的句柄
if (childHwnd != IntPtr.Zero)
{
DataAccess.SendMessage(childHwnd, BM_CLICK, 0, 0); //发送点击按钮的消息
b = true;
}
else
{
b = false;
}
}
else
{
b = false;
}
return b;
}
/// <summary>
/// 关闭所有不必要的窗口 (如果你的程序调用其它程序时弹窗口,你弹出的窗口捕捉到,用此方法自动关闭它)
/// </summary>
/// <returns></returns>
private bool CloseUser32()
{
bool b = false;
while (closeWindows_User32("Macromedia FlashPaper", "确定"))
{
closeWindows_User32("Macromedia FlashPaper", "确定");
}
while (closeWindows_User32("Microsoft Office Word", "是(&Y)"))
{
closeWindows_User32("Microsoft Office Word", "是(&Y)");
}
while (closeWindows_User32("Microsoft Office Word", "确定"))
{
closeWindows_User32("Microsoft Office Word", "确定");
}
while (closeWindows_User32("文件正在使用", "确定"))
{
closeWindows_User32("文件正在使用", "确定");
}
while (closeWindows_User32("FlashPaper打印机错误。", "确定"))
{
closeWindows_User32("FlashPaper打印机错误。", "确定");
}
while (closeWindows_User32("另存为", "取消"))
{
closeWindows_User32("另存为", "取消");
}
while (closeWindows_User32("Microsoft Office Word", "打开(&O)"))
{
closeWindows_User32("Microsoft Office Word", "打开(&O)");
}
//if (closeWindows_User32("FlashPaper打印机错误。", "确定") == false && closeWindows_User32("Microsoft Office Word", "是(&Y)") == false && closeWindows_User32("文件正在使用", "确定") == false)
//{
// b = true;
//}
return b;
}