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

来源:互联网 发布:java集合和数组的区别 编辑:程序博客网 时间:2024/05/07 06:49
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应用程序上运行后程序没有任何反应。






0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 厂家不通过经销商直接发货怎么办 媳妇吵架说我全家有病怎么办 全家都不尊重媳妇怎么办 招商闪电贷有额度不通过怎么办 闪电贷页面登录不进去怎么办 91借钱极速逾期怎么办 论文没过拿不到毕业证怎么办 大学论文不过拿不到毕业证怎么办 转店被黑中介骗了钱怎么办? 被星外转铺骗了怎么办 店铺转了后悔了怎么办 商铺转让不出去怎么办? 和包券密码丢失怎么办 天猫购物卷兑换不了怎么办 淘宝新店每天只有几个访客怎么办 注册淘宝企业店铺需要审核怎么办 淘宝店铺被投诉知识产权怎么办 一般违规扣48分怎么办 金税盘处于报税期不能开票怎么办 小规模税率开错了怎么办 我是代购卖家被买家投诉偷税怎么办 天猫盒子内存不够怎么办 天猫品牌申请不通过怎么办 天猫商家发货发个空包裹怎么办 无限流量怎么办没有4g 海外直邮身份证过期了怎么办 买车的人不过户怎么办 天猫精灵球泡离线怎么办 花呗被骗了2万怎么办 天猫公司变更地址发票怎么办 支付宝自助解限怎么办 支付宝16岁限额怎么办 支付宝提不了现怎么办 支付宝余额受限需要身份证怎么办 微信被骗了6000怎么办 被代运营骗了该怎么办 淘宝店铺过节放假无人打理怎么办 淘宝店太久没打理出现未开店怎么办 淘宝店关了售后怎么办 发货运单号发错了怎么办 天猫积分为零怎么办