Easy steps to create a System Tray Application with C# z
2014-05-29 09:30
302 查看
Hiding the C# application to the system tray is quiet straight forward. With a few line of codes in C# and you can accomplish this. First you will need to create the context menu for the system tray, then after that you need to create the notify icon. The next step is just enable the system icon. Here is the sample code below.
1 using System;
2 using System.Collections.Generic;
3 using System.ComponentModel;
4 using System.Data;
5 using System.Drawing;
6 using System.Linq;
7 using System.Text;
8 using System.Windows.Forms;
9
10 namespace AdsenseDisabler
11 {
12 public partial class Form1 : Form
13 {
14 private NotifyIcon sysTrayIcon;
15 private ContextMenu sysTrayMenu;
16
17 [STAThread]
18 static void Main()
19 {
20 Application.EnableVisualStyles();
21 Application.SetCompatibleTextRenderingDefault(false);
22 Application.Run(new Form1());
23 }
24
25 public Form1()
26 {
27 InitializeComponent();
28
29 // Create a context menu for th systray.
30
31 sysTrayMenu = new ContextMenu();
32 sysTrayMenu.MenuItems.Add("Enable Adsense", OnEnabled);
33 sysTrayMenu.MenuItems.Add("Disable AdsenseDisabler", OnDisabled);
34 sysTrayMenu.MenuItems.Add("Show Panel", OnShowed);
35 sysTrayMenu.MenuItems.Add("Exit", OnExit);
36
37 // create and intialise the tray notify icon.
38 // This example uses the standard icon but can be replaced
39 // with your own custom icon.
40 sysTrayIcon = new NotifyIcon();
41 sysTrayIcon.Text = "Adsense Disabler";
42 sysTrayIcon.Icon = new Icon(SystemIcons.Shield, 40, 40);
43
44 // Add menu to tray icon and show it.
45 sysTrayIcon.ContextMenu = sysTrayMenu;
46 sysTrayIcon.Visible = true;
47 }
48
49 protected override void OnLoad(EventArgs e)
50 {
51 Visible = true; // Hide form window.
52 ShowInTaskbar = false; // Remove from taskbar.
53
54 base.OnLoad(e);
55 }
56
57 private void OnExit(object sender, EventArgs e)
58 {
59 Application.Exit();
60 }
61
62 private void OnEnabled(object sender, EventArgs e)
63 {
64
65 }
66
67 private void OnDisabled(object sender, EventArgs e)
68 {
69
70 }
71
72 private void OnShowed(object sender, EventArgs e)
73 {
74
75 }
76
77 }
78 }
1 using System;
2 using System.Collections.Generic;
3 using System.ComponentModel;
4 using System.Data;
5 using System.Drawing;
6 using System.Linq;
7 using System.Text;
8 using System.Windows.Forms;
9
10 namespace AdsenseDisabler
11 {
12 public partial class Form1 : Form
13 {
14 private NotifyIcon sysTrayIcon;
15 private ContextMenu sysTrayMenu;
16
17 [STAThread]
18 static void Main()
19 {
20 Application.EnableVisualStyles();
21 Application.SetCompatibleTextRenderingDefault(false);
22 Application.Run(new Form1());
23 }
24
25 public Form1()
26 {
27 InitializeComponent();
28
29 // Create a context menu for th systray.
30
31 sysTrayMenu = new ContextMenu();
32 sysTrayMenu.MenuItems.Add("Enable Adsense", OnEnabled);
33 sysTrayMenu.MenuItems.Add("Disable AdsenseDisabler", OnDisabled);
34 sysTrayMenu.MenuItems.Add("Show Panel", OnShowed);
35 sysTrayMenu.MenuItems.Add("Exit", OnExit);
36
37 // create and intialise the tray notify icon.
38 // This example uses the standard icon but can be replaced
39 // with your own custom icon.
40 sysTrayIcon = new NotifyIcon();
41 sysTrayIcon.Text = "Adsense Disabler";
42 sysTrayIcon.Icon = new Icon(SystemIcons.Shield, 40, 40);
43
44 // Add menu to tray icon and show it.
45 sysTrayIcon.ContextMenu = sysTrayMenu;
46 sysTrayIcon.Visible = true;
47 }
48
49 protected override void OnLoad(EventArgs e)
50 {
51 Visible = true; // Hide form window.
52 ShowInTaskbar = false; // Remove from taskbar.
53
54 base.OnLoad(e);
55 }
56
57 private void OnExit(object sender, EventArgs e)
58 {
59 Application.Exit();
60 }
61
62 private void OnEnabled(object sender, EventArgs e)
63 {
64
65 }
66
67 private void OnDisabled(object sender, EventArgs e)
68 {
69
70 }
71
72 private void OnShowed(object sender, EventArgs e)
73 {
74
75 }
76
77 }
78 }
相关文章推荐
- Summarize the steps how to create a .NET application[摘自Beginning Visual C# 2008]
- 错误:created a ThreadLocal with key of type ……but failed to remove it when the web application was stopped. This is very likely to create a memory leak.
- How to create a DLL library in C and then use it with C#
- Easy Steps to Create Storm Project ( Twitter Support) in Eclipse
- C# Tip: Placing Your C# Application in the System Tray(把应用程序最小化到系统托架中)(zz)
- 6 Easy Steps to Learn Naive Bayes Algorithm (with code in Python)
- 6 Easy Steps to Learn Naive Bayes Algorithm (with code in Python)
- 7 Easy Steps to Learn C#: Silverlight C# Compiler and Loading DLLs from a server at runtime in Silverlight
- How to create a PictureMarkerSymbol (Picture Symbol)in ArcGIS server with C#
- http://www.mkyong.com/maven/how-to-create-a-web-application-project-with-maven/
- How To Manipulate Icons in the System Tray with Visual Basic
- How to create a DLL library in C and then use it with C#
- (C#) System.BadImageFormatException: An attempt was made to load a program with an incorrect format.
- how to create dynamically crystal roporter with C# code
- How to create a PictureMarkerSymbol (Picture Symbol)in ArcGIS server with C#
- Adding Icons to the System Tray
- add a splash to load system is easy:1:add spashWnd,then call it in instance ,when some work ok,close it.
- What 's CATT(Computer Aided Test Tool)?How to deal with the CATT in the SAP System?什么是CATT(计算机附加测试工具)
- PDFToText with ITextSharp--Extract text from PDF in C# (100% .NET)(推荐)
- TFS CMMI 模板试用, Create Alternative Application Partitioning Designs /Design System Architecture and Deployment