一个程序关闭另外一个程序

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.Threading;
using System.Runtime.InteropServices ;

 
namespace test
{
    
    class Program
    { 
        [DllImport ("User32.dll")  ]

        private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
       
        [DllImport("user32.dll")]
        [return: MarshalAs(UnmanagedType.Bool)]
        static extern Boolean SetForegroundWindow(IntPtr hWnd);

        [DllImport("user32.dll")]
       static extern IntPtr SendMessage(IntPtr hWnd, UInt32 Msg, IntPtr wParam, [MarshalAs(UnmanagedType.LPStr)] string lParam);


        [DllImport("user32.dll", EntryPoint = "SendMessage")]
        private static extern int SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam);

        [DllImport("user32.dll", CharSet = CharSet.Auto)]

        static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);

        [return: MarshalAs(UnmanagedType.Bool)]

        [DllImport("user32.dll", SetLastError = true)]
        static extern bool PostMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);
       public   Alert hehe;

    static    IntPtr he;
        int scantime;
     //   void loadconfig(string data);
        private const int WM_ACTIVATEAPP = 0x001C;
        private bool appActive = true;
      
        const  int WM_CLOSE = 0x10;

        const uint WM_DESTROY = 0x02;

        const uint WM_QUIT = 0x12;

        static void Main(string[] args)
        {
            systest conf;

           //conf=new systest() ;
          // conf.loadconfig("config.xml");

          // Console.WriteLine(conf.pptdir+conf.pptdir );
           he = FindWindow(null, "yuan");
           Console.WriteLine(he.ToString());
           Console.WriteLine(he.ToInt64());

         //  PostMessage(he, WM_QUIT, IntPtr.Zero, IntPtr.Zero);
        //  SetForegroundWindow(he);
           SendMessage(he, WM_QUIT, IntPtr.Zero, IntPtr.Zero);
       //  SendMessage(he, WM_QUIT, 0, 0);
           Thread.Sleep(20000);

        }

      public   void backthread()
        {
            hehe = new Alert();
            Form f = new Form();
            Application.Run(f);
            f.Show();
            hehe.ShowDialog();

        }

    }

    class systest
    {
      public   XmlDocument doc;
      public   string pptexe, pptdir, scantime;


     public   void loadconfig(string data)
        {
            doc = new XmlDocument();
            doc.Load(data);
            XmlNode pptdirnode, pptexenode, timenode;
            pptexenode = doc.SelectSingleNode("/root/PPTExe");
            pptdirnode = doc.SelectSingleNode("/root/PPTDir");
            timenode = doc.SelectSingleNode("/root/ScanTime");
            pptexe = pptexenode.InnerText.Trim();
            pptdir = pptdirnode.InnerText.Trim();
           //scantime = Convert.ToInt32(timenode.InnerText.Trim());
           // MessageBox.Show(pptdir);
            Console.WriteLine(Thread.CurrentThread.ManagedThreadId);
           
        }

    }

}



在一个GUI程序运行环境下,我们通过得到窗体的句柄,然后便可以向该句柄发送消息,WndProc(Window Procedure)函数会处理相应的事件。其中WM_CLOSE相当于用户点击关闭按钮,使用PostMessage将WM_CLOSE发送至主窗体等价于.NET中Process类的CloseMainWindow方法,当接收到WM_CLOSE消息时,应用程序是可以选择是否真正结束程序的,如果继续结束程序而不取消。接着WM_DESTROY消息会发送,这个消息代表着窗体开始真正关闭,此时可以进行一些资源的清理。最后当前线程接收到WM_QUIT消息,线程的消息循环会被终止。

 

因此向窗体发送这3个消息,只有WM_CLOSE会引发Closing事件,属于正常窗体退出逻辑,其他两个中消息会直接强行关闭窗体。

注意WM_QUIT消息只能用PostMessage将其送至消息队列尾部,使用SendMessage立即发送在WPF应用程序上运行后程序没有任何反应。






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Farmwang

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值