컴퓨터 이야기/c#

Tray로 Form을 Icon화 하기

golfin 2018. 6. 6. 13:40


Tray로 Form을 Icon화 하기



Form애  notifyIcon1 추가

private void Form1_Load(object sender, EventArgs e)
        {
            ContextMenu ctx = new ContextMenu();
            ctx.MenuItems.Add(new MenuItem("Open",
                new EventHandler((s, ex) => AppearingMainForm())));
            ctx.MenuItems.Add("-");
            ctx.MenuItems.Add(new MenuItem("Exit", new EventHandler((s, ex) => this.Close())));
            notifyIcon1.ContextMenu = ctx;

}

       private void Form1_MinimumSizeChanged(object sender, EventArgs e)
        {
            ShownNotifyIcon();
        }

        private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            AppearingMainForm();
        }

  private void ShownNotifyIcon()
        {
            WindowState = FormWindowState.Minimized;
            notifyIcon1.Visible = true;
        //    notifyIcon1.Icon = SystemIcons.Application;
        }

        private void AppearingMainForm()
        {
            notifyIcon1.Visible = false;
            this.Show();
            this.WindowState = FormWindowState.Normal;
        }

        private void Form1_Resize(object sender, EventArgs e)
        {
            if (FormWindowState.Minimized == this.WindowState)
            {
                ShownNotifyIcon();
            }
            else if (FormWindowState.Normal == this.WindowState)
            {
                AppearingMainForm();
            }
        }

        private void notifyIcon1_MouseDoubleClick_1(object sender, MouseEventArgs e)
        {
            // 아이콘을 더블클릭하면 폼 화면을 보여줌
            AppearingMainForm();
        }