写一个server端 无窗体 只有小窗右击功能

代码:

 internal static class Program
    {
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            //Application.Run(new Form1());
            Application.Run(new RtApplication());
        }
    }

将原来的窗体注释,用自己自定义类RtApplication:

namespace WindowsFormsApp1
{
    internal class RtApplication : ApplicationContext
    {
        private NotifyIcon _notifyIcon = new NotifyIcon();
        public RtApplication()
        {
            MenuItem configMenuItem = new MenuItem(string.Format("Login  RT Server(&L)"), new EventHandler(ConfirmLogin1));
            MenuItem exitMenuItem = new MenuItem(string.Format("Exit  RT Server(&Q)"), new EventHandler(ConfirmLogin2));
            _notifyIcon.ContextMenu = new ContextMenu(new MenuItem[] { configMenuItem, exitMenuItem });
            _notifyIcon.Visible = true;
            _notifyIcon.Text = "test";
            _notifyIcon.Icon = WindowsFormsApp1.Properties.Resources.logo;//一定要有,不然不显示
        }

        private void ConfirmLogin1(object sender, EventArgs e)
        {
            MessageBox.Show("1");
        }

        private void ConfirmLogin2(object sender, EventArgs e)
        {
            MessageBox.Show("2");
        }
    }
}

 

posted @ 2022-07-25 17:09  当年小清新  阅读(21)  评论(0编辑  收藏  举报