日期:2014-05-17  浏览次数:21029 次

user32.dll接口 捕捉windows信息窗口,和关闭信息窗口

 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>
        /// 关闭所有不必要的窗口 (如果你的程序调用其它程序时弹窗口,你弹出的窗口捕捉到,用此方法自动关闭它)
        ///