시스템 트레이에서만 실행되는 .NET Windows Forms 응용 프로그램을 만들려면 어떻게해야합니까?
시스템 트레이에서 Windows Forms 응용 프로그램을 실행 하려면 어떻게해야 합니까?
트레이를 최소화 할 수있는 응용 프로그램은 아니지만 아이콘, 도구 설명 및 "오른쪽 클릭"메뉴 외에 트레이에만있는 응용 프로그램.
NotifyIcon을 사용하는 기본적인 대답은 정확하지만 많은 .NET과 마찬가지로 올바르게 수행하는 데 많은 미묘한 점이 있습니다 . Brad가 언급 한 튜토리얼은 기본적인 내용을 잘 설명하지만 다음 중 어느 것도 다루지 않습니다.
- 시스템 트레이에서 응용 프로그램을 닫으면 열려있는 자식 양식이 올바르게 닫히나요?
- 응용 프로그램이 하나의 인스턴스 만 실행되도록 강제 실행합니까 (모두 트레이 응용 프로그램이 아닌 대부분의 응용 프로그램에 적용 가능)?
- 원하는 경우 WPF 자식 창과 WinForms 자식 창을 여는 방법
- 동적 상황에 맞는 메뉴를 지원하는 방법
- 표준 NotifyIcon은 WinForms 공간에 존재합니다. 순수한 WPF 솔루션을 할 수 있습니까? (그래 넌 할수있어!)
방금 Simple-Talk.com에 실린 기사를 통해 이러한 요점을 자세히 설명하고 즉시 사용할 수있는 트레이 응용 프로그램 프레임 워크와 실제의 모든 것을 보여주는 완전한 실제 예제 응용 프로그램을 제공합니다. 2010 년 11 월 발행 된 .NET : 실용 안내서의 트레이 응용 프로그램 만들기를 참조하십시오 .
코드 프로젝트 기사 Tasktray 응용 프로그램 만들기 는 시스템 트레이에만 존재하는 응용 프로그램을 만드는 간단한 설명과 예를 제공합니다.
기본적으로 Application.Run(new Form1());
라인을 변경하여 에서 Program.cs
상속되는 클래스를 시작하고 해당 클래스 ApplicationContext
의 생성자가NotifyIcon
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new MyCustomApplicationContext());
}
}
public class MyCustomApplicationContext : ApplicationContext
{
private NotifyIcon trayIcon;
public MyCustomApplicationContext ()
{
// Initialize Tray Icon
trayIcon = new NotifyIcon()
{
Icon = Resources.AppIcon,
ContextMenu = new ContextMenu(new MenuItem[] {
new MenuItem("Exit", Exit)
}),
Visible = true
};
}
void Exit(object sender, EventArgs e)
{
// Hide tray icon, otherwise it will remain shown until user mouses over it
trayIcon.Visible = false;
Application.Exit();
}
}
mat1t가 말했듯이-응용 프로그램에 NotifyIcon을 추가 한 다음 다음 코드와 같은 것을 사용하여 툴팁과 상황에 맞는 메뉴를 설정해야합니다.
this.notifyIcon.Text = "This is the tooltip";
this.notifyIcon.ContextMenu = new ContextMenu();
this.notifyIcon.ContextMenu.MenuItems.Add(new MenuItem("Option 1", new EventHandler(handler_method)));
이 코드는 시스템 트레이의 아이콘 만 보여줍니다.
this.notifyIcon.Visible = true; // Shows the notify icon in the system tray
어떤 이유에서든 양식이있는 경우 다음이 필요합니다.
this.ShowInTaskbar = false; // Removes the application from the taskbar
Hide();
상황에 맞는 메뉴를 가져 오려면 마우스 오른쪽 버튼을 클릭하면 자동으로 처리되지만 왼쪽 클릭에 대해 작업을 수행하려면 클릭 처리기를 추가해야합니다.
private void notifyIcon_Click(object sender, EventArgs e)
{
var eventArgs = e as MouseEventArgs;
switch (eventArgs.Button)
{
// Left click to reactivate
case MouseButtons.Left:
// Do your stuff
break;
}
}
I've wrote a traybar app with .NET 1.1 and I didn't need a form.
First of all, set the startup object of the project as a Sub Main
, defined in a module.
Then create programmatically the components: the NotifyIcon
and ContextMenu
.
Be sure to include a MenuItem
"Quit" or similar.
Bind the ContextMenu
to the NotifyIcon
.
Invoke Application.Run()
.
In the event handler for the Quit MenuItem
be sure to call set NotifyIcon.Visible = False
, then Application.Exit()
. Add what you need to the ContextMenu
and handle properly :)
- Create a new Windows Application with the wizard.
- Delete
Form1
from the code. - Remove the code in Program.cs starting up the
Form1
. - Use the
NotifyIcon
class to create your system tray icon (assign an icon to it). - Add a contextmenu to it.
- Or react to
NotifyIcon
's mouseclick and differenciate between Right and Left click, setting your contextmenu and showing it for which ever button (right/left) was pressed. Application.Run()
to keep the app running withApplication.Exit()
to quit. Or abool bRunning = true; while(bRunning){Application.DoEvents(); Thread.Sleep(10);}
. Then setbRunning = false;
to exit the app.
"System tray" application is just a regular win forms application, only difference is that it creates a icon in windows system tray area. In order to create sys.tray icon use NotifyIcon component , you can find it in Toolbox(Common controls), and modify it's properties: Icon, tool tip. Also it enables you to handle mouse click and double click messages.
And One more thing , in order to achieve look and feels or standard tray app. add followinf lines on your main form show event:
private void MainForm_Shown(object sender, EventArgs e)
{
WindowState = FormWindowState.Minimized;
Hide();
}
As far as I'm aware you have to still write the application using a form, but have no controls on the form and never set it visible. Use the NotifyIcon (an MSDN sample of which can be found here) to write your application.
Here is how I did it with Visual Studio 2010, .NET 4
- Create a Windows Forms Application, set 'Make single instance application' in properties
- Add a ContextMenuStrip
- Add some entries to the context menu strip, double click on them to get the handlers, for example, 'exit' (double click) -> handler -> me.Close()
- Add a NotifyIcon, in the designer set contextMenuStrip to the one you just created, pick an icon (you can find some in the VisualStudio folder under 'common7...')
- Set properties for the form in the designer: FormBorderStyle:none, ShowIcon:false, ShowInTaskbar:false, Opacity:0%, WindowState:Minimized
- Add Me.Visible=false at the end of Form1_Load, this will hide the icon when using Ctrl + Tab
- Run and adjust as needed.
It is very friendly framework for Notification Area Application... it is enough to add NotificationIcon to base form and change auto-generated code to code below:
public partial class Form1 : Form
{
private bool hidden = false;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
this.ShowInTaskbar = false;
//this.WindowState = FormWindowState.Minimized;
this.Hide();
hidden = true;
}
private void notifyIcon1_Click(object sender, EventArgs e)
{
if (hidden) // this.WindowState == FormWindowState.Minimized)
{
// this.WindowState = FormWindowState.Normal;
this.Show();
hidden = false;
}
else
{
// this.WindowState = FormWindowState.Minimized;
this.Hide();
hidden = true;
}
}
}
Simply add
this.WindowState = FormWindowState.Minimized;
this.ShowInTaskbar = false;
to your form object. You will see only an icon at system tray.
'IT story' 카테고리의 다른 글
새로운 자동 참조 계산 메커니즘은 어떻게 작동합니까? (0) | 2020.05.06 |
---|---|
JavaScript에서 setAttribute vs.attribute =를 사용하는시기 (0) | 2020.05.06 |
범프 버전은 무엇을 의미합니까? (0) | 2020.05.06 |
JavaScript : alert () 재정의 (0) | 2020.05.06 |
<% $, <% @, <% =, <% #… 거래는 무엇입니까? (0) | 2020.05.06 |