Exemplo n.º 1
0
        public Settings()
        {
            //RegistryKey hklm = Registry.CurrentUser;
            //RegistryKey lgn0 = hklm.OpenSubKey(@"Software\BatteryIcon", true);
            //if (lgn0 == null)   //初始化
            //{
            //    RegistryKey lgn = hklm.OpenSubKey(@"Software", true).CreateSubKey("BatteryIcon");
            //    lgn.SetValue("xoffset", "0", RegistryValueKind.String);
            //    lgn.SetValue("yoffset", "0", RegistryValueKind.String);
            //    lgn.SetValue("fontsize", "28", RegistryValueKind.String);
            //    lgn.SetValue("normalColor", "255, 255, 255", RegistryValueKind.String);
            //    lgn.SetValue("chargingColor", "254, 190, 4", RegistryValueKind.String);
            //    lgn.SetValue("lowColor", "254, 97, 82", RegistryValueKind.String);
            //    lgn.SetValue("autoHide", "false", RegistryValueKind.String); // 电池电量充满后是否自动隐藏
            //}
            //RegistryKey lgn1 = hklm.OpenSubKey(@"Software\BatteryIcon", true);
            //// 从注册表获取数据
            //string fontsize = lgn1.GetValue("fontsize").ToString();
            //string xoffset = lgn1.GetValue("xoffset").ToString();
            //string yoffset = lgn1.GetValue("yoffset").ToString();
            //string normalColor = lgn1.GetValue("normalColor").ToString();
            //string chargingColor = lgn1.GetValue("chargingColor").ToString();
            //string lowColor = lgn1.GetValue("lowColor").ToString();


            PTConfig cfg = new PTConfig();

            cfg.Load();
            string fontsize      = cfg.FontSize;
            string xoffset       = cfg.XOffset;
            string yoffset       = cfg.YOffset;
            string normalColor   = cfg.NormalColor;
            string chargingColor = cfg.ChargingColor;
            string lowColor      = cfg.LowColor;

            InitializeComponent(); //绘制控件
            textBox1.Text         = fontsize;
            textBox3.Text         = xoffset;
            textBox4.Text         = yoffset;
            pictureBox1.BackColor = (Color) new ColorConverter().ConvertFromInvariantString(normalColor);   // 从字符串获取颜色
            pictureBox2.BackColor = (Color) new ColorConverter().ConvertFromInvariantString(chargingColor);
            pictureBox3.BackColor = (Color) new ColorConverter().ConvertFromInvariantString(lowColor);

            // 解决textbox1自动聚焦的问题
            textBox2.Visible = false;
            textBox2.Enabled = false;
            ActiveControl    = textBox2;
        }
Exemplo n.º 2
0
        private void textBox3_TextChanged(object sender, EventArgs e)
        {
            try
            {
                TrayIcon.xoffset = Convert.ToInt32(textBox3.Text);
                //RegistryKey hklm = Registry.CurrentUser;
                //RegistryKey lgn = hklm.OpenSubKey(@"Software", true).CreateSubKey("BatteryIcon");
                //lgn.SetValue("xoffset", textBox3.Text, RegistryValueKind.String);

                PTConfig cfg = new PTConfig();
                cfg.Load();
                cfg.XOffset = textBox3.Text;
                cfg.Update();
            }
            catch
            {
                // do nothing
            }
        }
Exemplo n.º 3
0
        private void pictureBox3_Click(object sender, EventArgs e)
        {
            ColorDialog loColorForm = new ColorDialog();

            if (loColorForm.ShowDialog() == DialogResult.OK)
            {
                pictureBox3.BackColor = loColorForm.Color;
                TrayIcon.lowColor     = loColorForm.Color;      // 更新颜色
                                                                // 将设置的颜色保存到注册表中
                                                                //RegistryKey hklm = Registry.CurrentUser;
                                                                //RegistryKey lgn = hklm.OpenSubKey(@"Software", true).CreateSubKey("BatteryIcon");
                                                                //lgn.SetValue("lowColor", new ColorConverter().ConvertToString(TrayIcon.lowColor), RegistryValueKind.String);

                PTConfig cfg = new PTConfig();
                cfg.Load();
                cfg.LowColor = new ColorConverter().ConvertToString(TrayIcon.lowColor);
                cfg.Update();
            }
        }
Exemplo n.º 4
0
        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            try
            {
                TrayIcon.iconFontSize = Convert.ToInt32(textBox1.Text);
                // 将设置的字号保存到注册表中
                //RegistryKey hklm = Registry.CurrentUser;
                //RegistryKey lgn = hklm.OpenSubKey(@"Software", true).CreateSubKey("BatteryIcon");
                //lgn.SetValue("fontsize", textBox1.Text, RegistryValueKind.String);

                PTConfig cfg = new PTConfig();
                cfg.Load();
                cfg.FontSize = textBox1.Text;
                cfg.Update();
            }
            catch
            {
                // do nothing
            }
        }
Exemplo n.º 5
0
        // 电池电量充满后是否自动隐藏电池图标
        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            //RegistryKey hklm = Registry.CurrentUser;
            //RegistryKey lgn = hklm.OpenSubKey(@"Software\BatteryIcon", true);

            PTConfig cfg = new PTConfig();

            cfg.Load();

            if (checkBox1.Checked == true)
            {
                TrayIcon.autoHide = "true";
                //lgn.SetValue("autoHide", "true", RegistryValueKind.String);
                cfg.AutoHide = "true";
            }
            else
            {
                TrayIcon.autoHide = "false";
                //lgn.SetValue("autoHide", "false", RegistryValueKind.String);
                cfg.AutoHide = "false";
            }
            cfg.Update();
        }
Exemplo n.º 6
0
        private Color batteryColor = Color.Green;   // 电池数字颜色

        public TrayIcon()
        {
            //// 从注册表加载颜色
            //RegistryKey hklm = Registry.CurrentUser;
            //RegistryKey lgn0 = hklm.OpenSubKey(@"Software\BatteryIcon", true);
            //if (lgn0 != null)   //当该注册表存在则应用注册表中的值
            //{
            //    RegistryKey lgn = hklm.OpenSubKey(@"Software\BatteryIcon", true);
            //    string fontsize = lgn.GetValue("fontsize").ToString();
            //    string xoff = lgn.GetValue("xoffset").ToString();
            //    string yoff = lgn.GetValue("yoffset").ToString();
            //    string normal = lgn.GetValue("normalColor").ToString();
            //    string charging = lgn.GetValue("chargingColor").ToString();
            //    string low = lgn.GetValue("lowColor").ToString();
            //    autoHide = lgn.GetValue("autoHide").ToString();
            //    try
            //    {
            //        iconFontSize = Convert.ToInt32(fontsize);   // 从字符串获取字体大小
            //        xoffset = Convert.ToInt32(xoff);
            //        yoffset = Convert.ToInt32(yoff);
            //    } catch
            //    {
            //        // do nothing
            //    }
            //    normalColor = (Color)new ColorConverter().ConvertFromString(normal);   // 从字符串获取颜色
            //    chargingColor = (Color)new ColorConverter().ConvertFromString(charging);   // 从字符串获取颜色
            //    lowColor = (Color)new ColorConverter().ConvertFromString(low);   // 从字符串获取颜色
            //}


            PTConfig cfg = new PTConfig();

            cfg.Load();
            string fontsize = cfg.FontSize;
            string xoff     = cfg.XOffset;
            string yoff     = cfg.YOffset;
            string normal   = cfg.NormalColor;
            string charging = cfg.ChargingColor;
            string low      = cfg.LowColor;

            autoHide = cfg.AutoHide;
            try
            {
                iconFontSize = Convert.ToInt32(fontsize);   // 从字符串获取字体大小
            }
            catch
            {
                iconFontSize = 15;
            }
            try
            {
                xoffset = Convert.ToInt32(xoff);
            }
            catch
            {
                xoffset = 0;
            }
            try
            {
                yoffset = Convert.ToInt32(yoff);
            }
            catch
            {
                yoffset = 0;
            }
            normalColor   = (Color) new ColorConverter().ConvertFromString(normal);   // 从字符串获取颜色
            chargingColor = (Color) new ColorConverter().ConvertFromString(charging); // 从字符串获取颜色
            lowColor      = (Color) new ColorConverter().ConvertFromString(low);      // 从字符串获取颜色



            ContextMenu contextMenu = new ContextMenu();
            MenuItem    menuItem1   = new MenuItem(); //设置按钮
            MenuItem    menuItem2   = new MenuItem(); //退出按钮

            notifyIcon = new NotifyIcon();

            // 初始化上下文菜单
            contextMenu.MenuItems.AddRange(new MenuItem[] { menuItem1, menuItem2 });

            // 初始化上下文菜单项
            // 初始化上下文菜单项
            menuItem1.Index  = 0;
            menuItem1.Text   = "设置";
            menuItem1.Click += new EventHandler(settingButton_Click);     // 注册上下文菜单点击事件

            menuItem2.Index  = 1;
            menuItem2.Text   = "退出";
            menuItem2.Click += new EventHandler(exitButton_Click);     // 注册上下文菜单点击事件

            notifyIcon.ContextMenu = contextMenu;

            batteryPercentage = "?";

            notifyIcon.Visible = true;

            // 设定计时器
            Timer timer = new Timer();

            timer.Tick    += new EventHandler(timer_Tick); // 注册计时器事件(用于更新电池百分比数字)
            timer.Interval = 1000;                         // 计时器频率:1s (也就是电池百分比的更新频率)
            timer.Start();
        }
Exemplo n.º 7
0
        /**
         * 电池百分比更新事件
         */
        private void timer_Tick(object sender, EventArgs e)
        {
            PowerStatus powerStatus = SystemInformation.PowerStatus;

            batteryPercentage = (powerStatus.BatteryLifePercent * 100).ToString();

            // 从注册表加载偏移信息
            //RegistryKey hklm = Registry.CurrentUser;
            //RegistryKey lgn = hklm.OpenSubKey(@"Software\BatteryIcon", true);
            //iconFontSize = Convert.ToInt32(lgn.GetValue("fontsize").ToString()); // 更新信息
            //xoffset = Convert.ToInt32(lgn.GetValue("xoffset").ToString());
            //yoffset = Convert.ToInt32(lgn.GetValue("yoffset").ToString());

            PTConfig cfg = new PTConfig();

            cfg.Load();
            iconFontSize = Convert.ToInt32(cfg.FontSize);
            xoffset      = Convert.ToInt32(cfg.XOffset);
            yoffset      = Convert.ToInt32(cfg.YOffset);

            // 如果电量充满则不显示
            if (batteryPercentage == "100" && autoHide == "true")
            {
                notifyIcon.Visible = false;
            }
            else
            {
                if (batteryPercentage == "100")
                {
                    iconFontSize      = 23;
                    xoffset           = -10;
                    yoffset           = 5;
                    batteryPercentage = "100";
                }
                notifyIcon.Visible = true;
            }

            // 如果电池正在充电,则将数字颜色改为金黄色
            if (powerStatus.BatteryChargeStatus.ToString().Contains(BatteryChargeStatus.Charging.ToString()))
            {
                batteryColor = chargingColor;
            }
            else
            {
                if (powerStatus.BatteryChargeStatus.ToString().Contains(BatteryChargeStatus.Low.ToString()))
                {
                    batteryColor = lowColor;
                }
                else
                {
                    batteryColor = normalColor;
                }
            }                                                                                                                                  // 渲染字体内容
            using (Bitmap bitmap = new Bitmap(DrawText(batteryPercentage, new Font(iconFont, iconFontSize), batteryColor, Color.Transparent))) // 背景色透明
            {
                IntPtr intPtr = bitmap.GetHicon();
                try
                {
                    using (Icon icon = Icon.FromHandle(intPtr))
                    {
                        notifyIcon.Icon = icon;
                        notifyIcon.Text = batteryPercentage + "%";
                    }
                }
                finally
                {
                    DestroyIcon(intPtr);
                }
            }
        }