博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Easy steps to create a System Tray Application with C# z
阅读量:6036 次
发布时间:2019-06-20

本文共 2108 字,大约阅读时间需要 7 分钟。

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 }

转载地址:http://fnohx.baihongyu.com/

你可能感兴趣的文章
Linux signal 那些事儿(2)【转】
查看>>
InfluxDB安装及配置
查看>>
Dynamics CRM Microsoft SQL Server 指定的数据库具有更高的版本号
查看>>
PAT Perfect Sequence (25)
查看>>
java.exe进程来源排查录
查看>>
点滴记录——Ubuntu 14.04中Solr与Tomcat整合安装
查看>>
C++实现KMP模式匹配算法
查看>>
ubuntu linux下建立stm32开发环境: GCC安装以及工程Makefile建立
查看>>
记录锁
查看>>
JSONObject与JSONArray的使用
查看>>
[SQL Server] 数据库日志文件自动增长导致连接超时的分析
查看>>
【常见Web应用安全问题】---6、Script source code disclosure
查看>>
<html:form>标签
查看>>
除了《一无所有》,我一无所有
查看>>
每日英语:China Seeks to Calm Anxiety Over Rice
查看>>
C++中struct和class的区别 [转]
查看>>
C++ ofstream和ifstream详细用法
查看>>
【G-BLASTN 1.0正式发布】
查看>>
Mysql 连接查询 Mysql支持的连接查询有哪些
查看>>
《ASP.NET1200例》<asp:DataList>分页显示图片
查看>>