串口编程

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace ShowCrossSight
{
    public partial class Form1 : Form
    {
        #region
        public Form1()
        {
            //SetStyle(ControlStyles.SupportsTransparentBackColor, true);
            //this.BackColor = Color.Transparent;
            InitializeComponent();
            //HotKey.RegisterHotKey(Handle, 100,HotKey.KeyModifiers.Ctrl, Keys.S);
           // HotKey.RegisterHotKey(Handle, 101,HotKey.KeyModifiers.Ctrl, Keys.X);
            HotKey.RegisterHotKey(Handle, 102, HotKey.KeyModifiers.None, Keys.F10);
            this.Initializenotifyicon();
        }
        #endregion

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            //注销快捷键
            //注销Id号为100的热键设定
            //HotKey.UnregisterHotKey(Handle, 100);
            //注销Id号为101的热键设定
           // HotKey.UnregisterHotKey(Handle, 101);
            //注销Id号为102的热键设定
            HotKey.UnregisterHotKey(Handle, 102);
        }

        

        ///接着重载WndProc方法,用于实现热键响应
        /// 监视Windows消息
        /// 重载WndProc方法,用于实现热键响应
        ///
        ///
        protected override void WndProc(ref Message m)
        {
            const int WM_HOTKEY = 0x0312;
            //按快捷键
            switch (m.Msg)
            {
                case WM_HOTKEY:
                    switch (m.WParam.ToInt32())
                    {
                        //case 100:      //按下的是Ctrl+S
                            //this.timer1.Start();
                        //    break;
                        //case 101:      //按下的是Ctrl+X

                            //此处填写快捷键响应代码
                            //Application.Exit();
                        //    break;
                        case 102:
                            if (this.Visible == false)
                            {
                                this.Visible = true;
                            }
                            else if (this.Visible == true)
                                this.Visible = false;
                            break;
                    }
                    break;
            }
            base.WndProc(ref m);
        }




        private void Initializenotifyicon()
        {
            //设定托盘程序的各个属性 
            //NotifyIcon TrayIcon = new NotifyIcon();
            //TrayIcon.Icon = mNetTrayIcon;
            //TrayIcon.Text = "用Visual C#做托盘程序" + "\n" + "作者:马金虎于2001.12.08";
            //TrayIcon.Visible = true;
            //TrayIcon.Click += new System.EventHandler(this.click);

            //定义一个MenuItem数组,并把此数组同时赋值给ContextMenu对象 
            MenuItem[] mnuItms = new MenuItem[3];
            mnuItms[0] = new MenuItem();
            mnuItms[0].Text = "显示准星";
            mnuItms[0].Click += new EventHandler(ShowCrossSight_Click);



            mnuItms[1] = new MenuItem("-");

            mnuItms[2] = new MenuItem();
            mnuItms[2].Text = "退出系统";
            mnuItms[2].Click += new System.EventHandler(this.ExitSelect);
            mnuItms[2].DefaultItem = true;


            ContextMenu notifyiconMnu = new ContextMenu(mnuItms);
            this.notifyIcon1.ContextMenu = notifyiconMnu;
            //为托盘程序加入设定好的ContextMenu对象 
        }

        private void ShowCrossSight_Click(object sender, EventArgs e)
        {
            if (this.Visible == false)
            {
                this.Visible = true;
            }
        }


        private void ExitSelect(object sender, System.EventArgs e)
        {
            //隐藏托盘程序中的图标 
            //TrayIcon.Visible = false;
            //关闭系统 
            this.Close();
        } 



    }




    class HotKey
    {
        //如果函数执行成功,返回值不为0。
        //如果函数执行失败,返回值为0。要得到扩展错误信息,调用GetLastError。
        [DllImport("user32.dll", SetLastError = true)]
        public static extern bool RegisterHotKey(
            IntPtr hWnd,                  //要定义热键的窗口的句柄
            int id,                       //定义热键ID(不能与其它ID重复)          
            KeyModifiers fsModifiers,     //标识热键是否在按Alt、Ctrl、Shift、Windows等键时才会生效
            Keys vk                       //定义热键的内容
            );

        [DllImport("user32.dll", SetLastError = true)]
        public static extern bool UnregisterHotKey(
            IntPtr hWnd,                  //要取消热键的窗口的句柄
            int id                        //要取消热键的ID
            );

        //定义了辅助键的名称(将数字转变为字符以便于记忆,也可去除此枚举而直接使用数值)
        [Flags()]
        public enum KeyModifiers
        {
            None = 0,
            Alt = 1,
            Ctrl = 2,
            Shift = 4,
            WindowsKey = 8
        }


    }


}

你可能感兴趣的:(编程,C++,c,windows,LINQ)