Beispiel #1
0
        public SystemTray(IComputer computer, PersistentSettings settings, UnitManager unitManager)
        {
            _computer                 = computer;
            _settings                 = settings;
            _unitManager              = unitManager;
            computer.HardwareAdded   += HardwareAdded;
            computer.HardwareRemoved += HardwareRemoved;

            _mainIcon = new NotifyIconAdv();

            ContextMenu contextMenu  = new ContextMenu();
            MenuItem    hideShowItem = new MenuItem("Hide/Show");

            hideShowItem.Click += delegate
            {
                SendHideShowCommand();
            };
            contextMenu.MenuItems.Add(hideShowItem);
            contextMenu.MenuItems.Add(new MenuItem("-"));
            MenuItem exitItem = new MenuItem("Exit");

            exitItem.Click += delegate
            {
                SendExitCommand();
            };
            contextMenu.MenuItems.Add(exitItem);
            _mainIcon.ContextMenu  = contextMenu;
            _mainIcon.DoubleClick += delegate
            {
                SendHideShowCommand();
            };
            _mainIcon.Icon = EmbeddedResources.GetIcon("smallicon.ico");
            _mainIcon.Text = "Libre Hardware Monitor";
        }
Beispiel #2
0
        public SensorNotifyIcon(SystemTray sensorSystemTray, ISensor sensor, PersistentSettings settings, UnitManager unitManager)
        {
            _unitManager = unitManager;
            Sensor       = sensor;
            _notifyIcon  = new NotifyIconAdv();

            Color defaultColor = Color.White;

            if (sensor.SensorType == SensorType.Load || sensor.SensorType == SensorType.Control || sensor.SensorType == SensorType.Level)
            {
                defaultColor = Color.FromArgb(0xff, 0x70, 0x8c, 0xf1);
            }

            Color = settings.GetValue(new Identifier(sensor.Identifier, "traycolor").ToString(), defaultColor);

            _pen = new Pen(Color.FromArgb(96, Color.Black));
            ContextMenu contextMenu  = new ContextMenu();
            MenuItem    hideShowItem = new MenuItem("Hide/Show");

            hideShowItem.Click += delegate
            {
                sensorSystemTray.SendHideShowCommand();
            };
            contextMenu.MenuItems.Add(hideShowItem);
            contextMenu.MenuItems.Add(new MenuItem("-"));
            MenuItem removeItem = new MenuItem("Remove Sensor");

            removeItem.Click += delegate
            {
                sensorSystemTray.Remove(Sensor);
            };
            contextMenu.MenuItems.Add(removeItem);
            MenuItem colorItem = new MenuItem("Change Color...");

            colorItem.Click += delegate
            {
                ColorDialog dialog = new ColorDialog {
                    Color = Color
                };
                if (dialog.ShowDialog() == DialogResult.OK)
                {
                    Color = dialog.Color;
                    settings.SetValue(new Identifier(sensor.Identifier,
                                                     "traycolor").ToString(), Color);
                }
            };
            contextMenu.MenuItems.Add(colorItem);
            contextMenu.MenuItems.Add(new MenuItem("-"));
            MenuItem exitItem = new MenuItem("Exit");

            exitItem.Click += delegate
            {
                sensorSystemTray.SendExitCommand();
            };
            contextMenu.MenuItems.Add(exitItem);
            _notifyIcon.ContextMenu  = contextMenu;
            _notifyIcon.DoubleClick += delegate
            {
                sensorSystemTray.SendHideShowCommand();
            };

            // get the default dpi to create an icon with the correct size
            float dpiX, dpiY;

            using (Bitmap b = new Bitmap(1, 1, PixelFormat.Format32bppArgb))
            {
                dpiX = b.HorizontalResolution;
                dpiY = b.VerticalResolution;
            }

            // adjust the size of the icon to current dpi (default is 16x16 at 96 dpi)
            int width  = (int)Math.Round(16 * dpiX / 96);
            int height = (int)Math.Round(16 * dpiY / 96);

            // make sure it does never get smaller than 16x16
            width  = width < 16 ? 16 : width;
            height = height < 16 ? 16 : height;

            // adjust the font size to the icon size
            FontFamily family = SystemFonts.MessageBoxFont.FontFamily;
            float      baseSize;

            switch (family.Name)
            {
            case "Segoe UI": baseSize = 12; break;

            case "Tahoma": baseSize = 11; break;

            default: baseSize = 12; break;
            }

            _font      = new Font(family, baseSize * width / 16.0f, GraphicsUnit.Pixel);
            _smallFont = new Font(family, 0.75f * baseSize * width / 16.0f, GraphicsUnit.Pixel);

            _bitmap   = new Bitmap(width, height, PixelFormat.Format32bppArgb);
            _graphics = Graphics.FromImage(_bitmap);
            if (Environment.OSVersion.Version.Major > 5)
            {
                _graphics.TextRenderingHint = TextRenderingHint.ClearTypeGridFit;
                _graphics.SmoothingMode     = SmoothingMode.HighQuality;
            }
        }