65.9K
CodeProject 正在变化。 阅读更多。
Home

一个简单的应用程序,用于通知大写锁定键状态

starIconstarIconstarIcon
emptyStarIcon
starIcon
emptyStarIcon

3.20/5 (8投票s)

2013年6月25日

CPOL

1分钟阅读

viewsIcon

22916

downloadIcon

11

此应用程序允许您在每次切换大写锁定键状态时了解其状态。

引言

我们都曾经使用过没有大写锁定、数字锁定等LED指示灯的键盘。 大写锁定尤其成问题,因为它会导致我们多次输入错误的密码。 此应用程序在每次更改大写锁定状态时提供一个指示器,显示大写锁定是开启还是关闭。

Using the Code

首先需要说明的是,我使用了此处TaskbarNotifier,用于显示弹出窗口。 任何使用TaskbarNotifier所需的说明都可以在上述链接中找到。 我使用了一个定时器来定期检查大写锁定键的状态,并在检测到更改时显示状态。 在var中声明的静态值用于检查状态是否已更改。

// public static class vars
        {
            public static string prevstate = "off";
            public static string curstate = "off";
        }
        private void timer1_Tick(object sender, EventArgs e)
        {
            
            if (Control.IsKeyLocked(Keys.CapsLock))
            {
                vars.prevstate = vars.curstate;
                vars.curstate = "on";
                if(vars.prevstate != vars.curstate)
                {
                TaskbarNotifier taskbarNotifier1;
                taskbarNotifier1 = new TaskbarNotifier();
                taskbarNotifier1.SetBackgroundBitmap(new Bitmap(GetType(), 
                "skin.bmp"), Color.FromArgb(255, 0, 255));
                taskbarNotifier1.SetCloseBitmap(new Bitmap(GetType(), 
                "close.bmp"), Color.FromArgb(255, 0, 255), new Point(127, 8));
                taskbarNotifier1.TitleRectangle = new Rectangle(40, 9, 100, 25);
                taskbarNotifier1.ContentRectangle = new Rectangle(8, 41, 150, 68);
                taskbarNotifier1.Show("Caps Lock status", 
                "Caps Lock key is ON", 500, 500, 500);
                }
            }
            else if (!(Control.IsKeyLocked(Keys.CapsLock)))
            {
                vars.prevstate = vars.curstate;
                vars.curstate = "off";
                if (vars.prevstate != vars.curstate)
                {
                    TaskbarNotifier taskbarNotifier1;
                    taskbarNotifier1 = new TaskbarNotifier();
                    taskbarNotifier1.SetBackgroundBitmap(new Bitmap(GetType(), 
                    "skin.bmp"), Color.FromArgb(255, 0, 255));
                    taskbarNotifier1.SetCloseBitmap(new Bitmap(GetType(), 
                    "close.bmp"), Color.FromArgb(255, 0, 255), new Point(127, 8));
                    taskbarNotifier1.TitleRectangle = new Rectangle(40, 9, 100, 25);
                    taskbarNotifier1.ContentRectangle = new Rectangle(8, 41, 150, 68);
                    taskbarNotifier1.Show("Caps Lock status", 
                    "Caps Lock key is OFF", 500, 500, 500);
                }
            }
        }

此外,在窗体加载事件中,我确保应用程序在任务栏上运行,并且不会通过显示任何窗口来干扰。

 if (FormWindowState.Minimized == this.WindowState)
            {
                mynotifyicon.BalloonTipText = 
                	"This app shows a notification whenever caps key is pressed ";
                
                mynotifyicon.BalloonTipTitle = "Caps Lock Indicator";
                 
                mynotifyicon.Visible = true;
                mynotifyicon.ShowBalloonTip(500);
                this.Hide();
            }
            else if (FormWindowState.Normal == this.WindowState)
            {
                mynotifyicon.Visible = false;  
            } 

此外,在窗体加载事件中,我通过设置窗体的“始终以最小化模式启动”属性,确保应用程序在任务栏上运行,并且不会通过显示任何窗口来干扰。 将可执行文件添加到启动文件夹,以确保应用程序在每次用户登录时运行。 最后,我想感谢John O'Byrne编写并向我们提供TaskbarNotifier,这使得编写此应用程序的代码变得轻而易举。

© . All rights reserved.