窗体最小化到右下脚托盘中

下面是在网上看到的,ContextMenu  这个控件 在工具箱中没有找到,用了contextMenuStrip 这个控件代替.

可以通过设置 this.notifyIcon1.Visible = false;  (设置托盘中图表的显示和隐藏)

比如在From_Load事件中,设置隐藏,在Form1_Resize中显示,类推...

窗体最小化到右下脚托盘中 - xyq - xyq

There are many cases when it's advantageous to place an application's icon in the System Tray. For example, firewall/antivirus and instant messaging applications do this so as to run in the background and still be accessible to the user without crowding up the task bar.

In this week's installment of my .NET Tips and Techniques series, I'll show you the steps involved in specifying that an application is to be minimized to the Tray, how to allow the user to restore the application by double-clicking the icon, and how to create and respond to a System Tray icon's context menu.

  1. To get started, open an existing C# Windows form (or create a new one).
  2. Open the Visual Studio Toolbox.
  3. Drag a NotifyIcon control onto the form. The control will named notifyIcon1 by default and placed below the form because it has no visual representation on the form itself.
  4. Set the NotifyIcon control's Text property to the name you want to appear when the user pauses the mouse over the application's icon. For example, this value could be "KillerApp 1.0".
  5. Set the control's Icon property to the icon that you want to appear in the System Tray.
    Tip: If you have a BMP file that you want to convert to an icon file, I highly recommend the QTam Bitmap to Icon 3.5 application.
  6. Add an event handler for the form's Resize event that will hide the application when it's minimized. That way, it won't appear on the task bar.
    private void Form1_Resize(object sender, System.EventArgs e)  {     if (FormWindowState.Minimized == WindowState)        Hide();  }  
  7. Add an event handler for the NotifyIcon.DoubleClick event and code it as follows so that the application will be restored when the icon is double-clicked.
    private void notifyIcon1_DoubleClick(object sender,                                       System.EventArgs e)  {      Show();      WindowState = FormWindowState.Normal;  }  

At this point, your application will fuction perfectly in terms of an icon appearing in the System Tray when the application is run (see Figure 1), the application not appearing on the task bar when minimized and the application restoring itself when the Tray icon is double-clicked.

窗体最小化到右下脚托盘中 - xyq - xyq

Figure 1

Now, let's see the steps involved with adding a context menu to the icon.

  1. From the Visual Studio Toolbox, drag a ContextMenu control onto the form.
  2. Right-click the ContextMenu control and select the Edit Menu.option.
  3. Type in the options that you want to appear in your context menu. For example, you can add options such as Restore and Close Application.
  4. As with any menu, double-click the menu item to create and code each item's handler. As an example, you could copy the code from the form's DoubleClick handler into the context menu's Restore handler and for the Close Application menu item; simply call the form's Close method.
  5. Finally, set the NotifyIcon control's ContextMenu property to the new context menu you just created by selecting the menu from the drop-down list. Figure 2 shows a simple Tray context menu.

窗体最小化到右下脚托盘中 - xyq - xyq

Figure 2

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值