关于托盘运行时,系统无法关机的方法

    技术2022-05-11  49

    用C#创建一个windows 应用程序,当程序以托盘的形式运行时,若关闭系统,若不首先关闭该程序,系统无法关机,

    解决方法如下:

    首先 声明 全局变量

    private const int WM_QUERYENDSESSION = 0x0011; 

    重写Form类方法WndProc

            /// <summary>        /// get windows message to close application        /// </summary>        /// <param name="m"></param>        protected override void WndProc(ref Message m)        {            switch (m.Msg)            {

                    case WM_QUERYENDSESSION:                    CloseForm();                    break;                default:                    break;            }            base.WndProc(ref m);        }

     只打开一个进程

            [STAThread]        static void Main()        {            bool Result;            //if the application does not start  then start            Mutex mutex = new Mutex(true, Application.ProductName, out Result);            if (Result)            {                Application.EnableVisualStyles();                Application.SetCompatibleTextRenderingDefault(false);                Application.Run(new SKSIndex());                mutex.ReleaseMutex();            }        }


    最新回复(0)