winfrom 关闭别的应用程序的窗体或者弹出框(winform 关闭句柄)

在word转换成html的时候,由于系统版本不一样,office总是抛出异常,Microsoft Word停止工作,下面有三个按钮,关闭程序等等,但是我的转换工作需要自动的,每当抛出异常的时候我的程序就会停止工作,对此就需要自动关闭这个异常,需要以下的这个代码!

    --相应的引用
       [DllImport("User32.dll", EntryPoint = "FindWindow")]
        private static extern int FindWindow(string lpClassName, string lpWindowName);
        [DllImport("User32.dll", EntryPoint = "FindWindowEx")]
        private static extern int FindWindowEx(IntPtr lpClassName, IntPtr lpWindowName, string isnull, string anniu); 
        [DllImport("user32.dll", EntryPoint = "SendMessageA")]
        private static extern int SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam);  
        private void test()
  {
            const int WM_CLOSE = 0x10;   //关闭

            const uint WM_DESTROY = 0x02;

            const uint WM_QUIT = 0x12;


            const int BM_CLICK = 0xF5;  //单击
            IntPtr Window_Handle = (IntPtr)FindWindow(null, "Microsoft Word");//查找所有的窗体,看看想查找的句柄是否存在,Microsoft Word  句柄              //
            if (Window_Handle ==IntPtr.Zero)   //如果没有查找到相应的句柄
            {
               
                MessageBox.Show("没有找到窗体");
            }
            else    //查找到相应的句柄
            {
                SendMessage(Window_Handle, WM_CLOSE, 0, 0);   //关闭窗体

              //  IntPtr childHwnd = (IntPtr)FindWindowEx(Window_Handle, IntPtr.Zero, null, "点击");//查找句柄中相应的按钮
              //  if (childHwnd == IntPtr.Zero)
              //  {
              //      MessageBox.Show("没有找到按钮");
              //  }
              //  else
              //  {
              //      SendMessage(childHwnd, BM_CLICK, 0, 0);   //单击这个句柄中的按钮
              //  }
            }


        }
如果您认为这篇文章还不错或者有所收获,您可以点击文章下面的【推荐】按钮精神支持,因为这种支持是我继续写作,分享的最大动力!
声明:原创博客请在转载时保留原文链接或者在文章开头加上本人博客地址,如发现错误,欢迎批评指正。凡是转载于本人的文章,不能设置打赏功能,如有特殊需求请与本人联系!
原文地址:https://www.cnblogs.com/gskstudy/p/4223797.html