none
如何利用 VC# 寫一支常駐程式去 監督 keyboard ? RRS feed

  • 問題

  • Dear All,

     

    想請問如何寫一支常駐程式去 監督 Keyboard.

     

    當程式執行後 , 右下角會出現 Tray icon , 可以關閉程式. (有可能連右下角的 tray icon 都看不見嗎?)

    當常駐程式存在的情況下, 如果有 使用者 按下 "a" , 程式就會 pop 出一個 message.

     

    目前我已經可以寫出一個右下角有 Tray icon 了, 只是如何開一個 Thread 去監督 keyboard 不會.

    可以請大家幫幫忙嗎? 謝謝

     

    BR,

    Wiles

     

    //===============================

    // 以下目前可以動的程式

    //===============================

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

    namespace WindowsApplication1
    {
        public partial class Form1 : Form
        {
            private Icon mNetTrayIcon = new Icon("Tray.ico");
            private NotifyIcon TrayIcon;
            private ContextMenu notifyiconMnu;


            public Form1()
            {
                InitializeComponent();

                Initializenotifyicon();

            }

            private void Form1_Load(object sender, EventArgs e)
            {
                this.MaximizeBox = false;
                this.MinimizeBox = false;
                this.WindowState = System.Windows.Forms.FormWindowState.Minimized;

            }

            private void Form1_KeyDown(object sender, KeyEventArgs e)
            {
                if (e.KeyCode == Keys.A)
                {
                    MessageBox.Show("Get Key-A", "TSR");
                }
            }

            private void Initializenotifyicon()
            {
                TrayIcon = new NotifyIcon();
                TrayIcon.Icon = mNetTrayIcon;
                TrayIcon.Text = "TSR-1";
                TrayIcon.Visible = true;
                TrayIcon.Click += new System.EventHandler(this.click);

                MenuItem[] mnuItms = new MenuItem[3];
                mnuItms[0] = new MenuItem();
                mnuItms[0].Text = "TSR Item-1";
                mnuItms[0].Click += new System.EventHandler(this.showmessage);

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

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

                notifyiconMnu = new ContextMenu(mnuItms);
                TrayIcon.ContextMenu = notifyiconMnu;           
            }

            public void click(object sender, System.EventArgs e)
            {
                MessageBox.Show("Item Clicked");
            }

            public void showmessage(object sender, System.EventArgs e)
            {
                MessageBox.Show("Item Selected");
            }

            public void ExitSelect(object sender, System.EventArgs e)
            {

                TrayIcon.Visible = false;
                this.Close();
            }


        }
    }

    2008年3月11日 上午 08:42

解答

所有回覆