zoukankan      html  css  js  c++  java
  • winfrom实现开机和托盘启动(转)

    功能:

    • (1)、在窗口上点击关闭按钮或者最小化时将托盘显示;
    • (2)、双击托盘图标显示窗口;
    • (3)、右键点击托盘图标提供三个菜单选项,“退出”、“隐藏”、“显示”;
    • (4)、程序可以设置开机启动,隐藏任务栏显示。就这四个小功能。

     

    1、建一个WinForm程序—TestIconForm,将其属性ShowInTaskbar改为false,这样程序将不会在任务栏中显示;将MaximizeBox属性设置为false,屏蔽掉最大化按钮;把StartPosition属性改为CerternScreen,这样程序运行后,窗口将会居中显示。

    2、在工具栏中的公共控件里,拖入NotifyIcon控件—testNotifyIcon,这个是程序运行任务栏右侧通知区域图标显示控件。

    3、在工具栏中的菜单和工具栏里,拖入ContextMenuStrip—testContextMenuStrip,这个控件是右击时关联菜单。

    4、右键testNotifyIcon选择属性,将其属性ContextMenuStrip改加为testContextMenuStrip,这个时候1和2两个步骤的两个控件就关联了,用于完成上面(3)功能。

    5、右键testContextMenuStrip选择属性,进入Items,然后点击“添加”,这里添加三个菜单选项:exitMenuItem、hideMenuItem、showMenuItem,同时分别将其Text属性改为:退出、隐藏和显示。

    准备工作就这些,下面是大致代码:

    1)、双击TestIconForm,即添加Load事件然后

       1:  private void Form1_Load(object sender, EventArgs e)
       2:          {
       3:              testNotifyIcon.Icon = new Icon("e:\MyPicture\testIcon.ico");
       4:              //取得程序路径
       5:              string startup = Application.ExecutablePath;
       6:   
       7:              //class Micosoft.Win32.RegistryKey. 表示Window注册表中项级节点,此类是注册表装
       8:              RegistryKey rKey = Registry.LocalMachine;
       9:              RegistryKey autoRun = rKey.CreateSubKey(@"SOFTWAREMicrosoftWindowsCurrentVersionRun");
      10:              try
      11:              {
      12:                  autoRun.SetValue("BookServer", startup);
      13:                  rKey.Close();
      14:              }
      15:              catch (Exception exp)
      16:              {
      17:                  MessageBox.Show(exp.Message.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
      18:              }
      19:          }

    添加Form_Closing,SizeChanged事件

       1:  private void Form1_FormClosing(object sender, FormClosingEventArgs e)  //关闭按钮事件
       2:          {
       3:              e.Cancel = true;
       4:              this.Hide();
       5:          }
       6:   
       7:          private void Form1_SizeChanged(object sender, EventArgs e)     //最小化事件按钮
       8:          {
       9:              this.Hide();
      10:          }
     
     
     
    2)、给testNotifyIcon添加MouseDoubleClick事件
       1:  private void testNotifyIcon_MouseDoubleClick(object sender, MouseEventArgs e)            // 左键双击,显示
       2:          {
       3:              if (e.Button == MouseButtons.Left)
       4:              {
       5:                  this.Show();
       6:                  this.WindowState = FormWindowState.Normal;
       7:                  this.Activate();
       8:              }
       9:          }

    3)、进入TestIconForm单击testContextMenuStrip,然后可以看到“退出”、“隐藏”、“显示”,分别双击,添加相应的事件

       1:  private void exitMenuItem_Click(object sender, EventArgs e)
       2:          {
       3:              if (MessageBox.Show("你确定要退出终端服务程序吗?", "确认", MessageBoxButtons.OKCancel,
       4:                  MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.OK)
       5:              {
       6:                  testNotifyIcon.Visible = false;
       7:                  this.Close();
       8:                  this.Dispose();
       9:                  Application.Exit();
      10:              }        
      11:          }
      12:   
      13:          private void showMenuItem_Click(object sender, EventArgs e)
      14:          {
      15:              this.Hide();        
      16:          }
      17:   
      18:          private void hideMenuItem_Click(object sender, EventArgs e)
      19:          {
      20:              this.Show();
      21:              this.WindowState = FormWindowState.Normal;
      22:              this.Activate();
      23:          }
  • 相关阅读:
    shell 统计行数
    sqlldr errors
    sqlldr 远程数据库
    load Properties
    查看shell 版本
    linux中的网络通信指令
    给EditText的drawableRight属性的图片设置点击事件
    p2p网贷3种运营模式
    p2p网贷3种运营模式
    linux常用的压缩与解压缩命令
  • 原文地址:https://www.cnblogs.com/ilooking/p/3635026.html
Copyright © 2011-2022 走看看