최소화 버튼을 누르면 현재 폼의 visible가 꺼지고 시스템트레이로 가게 됩니다.
시스템트레이는 보통 시계 옆에 있는 것을 뜻하구요.
시스템트레이로간 아이콘을 오른쪽 버튼을 클릭하게 되면 open과 exit가 있습니다
open을 누르면 다시 폼의 visible이 true가 되고 트레이의 아이콘 visibe이 false가 됩니다.
코드입니다.
Form1.cs |
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
//폼 최상위로 띄우기
[DllImport("user32.dll")]
private static extern IntPtr FindWindow(string SClassName, string SWindowName);
[DllImport("user32.dll")]
private static extern bool SetForegroundWindow(IntPtr findname);
[DllImport("user32.dll")]
private static extern bool ShowWindowAsync(IntPtr findname, int howShow);
private const int showNORMAL = 1;
private const int showMINIMIZED = 2;
private const int showMAXIMIZED = 3;
private void Form1_Shown(object sender, EventArgs e)
{
this.miniMomBtn.Visible = false;
miniMomBtn.ContextMenuStrip = cms;
}
private void Form1_Resize(object sender, EventArgs e)
{
if (this.WindowState == FormWindowState.Minimized)
{
miniMomBtn.Visible = true;
this.Visible = false;
return;
}
}
private void openToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Visible = true;
miniMomBtn.Visible = false;
// 윈도우 타이틀명으로 핸들을 찾는다
IntPtr findname = FindWindow(null, "Form1");
if (!findname.Equals(IntPtr.Zero))
{
// 프로그램이 최소화 되어 있다면 활성화 시킴
ShowWindowAsync(findname, showNORMAL);
// 윈도우에 포커스를 줘서 최상위로 만듬
SetForegroundWindow(findname);
}
}
private void exitToolStripMenuItem_Click(object sender, EventArgs e)
{
Application.Exit();
}
}
}
Form1_Resize 는 폼의 이벤트 (번개모양) 표시된곳에서 설정할 수 있습니다.
이것은 프로그램의 사이즈가 변경이 될때인데 if (this.WindowState == FormWindowState.Minimized)
위 코드문과 같이 if문을주어 최소화 버튼이 눌렸을때 { } 안의 내용을 실행 시켜라 입니다.
Form1_Shown 도 폼의 이벤트 입니다.
이것은 폼이 처음 실행되었을때 입니다. 여기서 miniMomBtn과 CMS는 도구상자의 NotifyIcon, ContextMenuStrip 입니다.
NotifyIcon은 시스템트레이의 아이콘 모양이나 설정등을 할수있고 ContextMenuStrip 오른쪽버튼이나 왼쪽 버튼을 눌렀을때 나오는 open과 exit와 같은 것입니다.
댓글