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);   //單擊這個句柄中的按鈕
              //  }
            }


        }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM