Пример #1
0
        private ContextMenu CreateMenu()
        {
            ContextMenu menu = new ContextMenu();

            MenuItem stackedAxesMenuItem = new MenuItem("Stacked Axes");

            stackedAxes = new UserOption("stackedAxes", true,
                                         stackedAxesMenuItem, settings);
            stackedAxes.Changed += (sender, e) => {
                UpdateAxesPosition();
                InvalidatePlot();
            };
            menu.MenuItems.Add(stackedAxesMenuItem);

            MenuItem axisLabelsMenuItem = new MenuItem("Axis Labels");

            axisLabels = new UserOption("axisLabels", true,
                                        axisLabelsMenuItem, settings);
            axisLabels.Changed += (sender, e) => {
                model.PlotMargins = ((UserOption)sender).Value ? new OxyThickness(double.NaN) : new OxyThickness(0);
            };
            menu.MenuItems.Add(axisLabelsMenuItem);

            MenuItem timeWindow = new MenuItem("Time Window");

            MenuItem[] timeWindowMenuItems =
            { new MenuItem("Auto",
                           (s,                                                            e) =>  { timeAxis.Zoom(0, double.NaN); InvalidatePlot();    }),
              new MenuItem("5 min",
                           (s,                                                                e) =>  { timeAxis.Zoom(0,        5 * 60); InvalidatePlot(); }),
              new MenuItem("10 min",
                           (s,                                                                e) =>  { timeAxis.Zoom(0,       10 * 60); InvalidatePlot(); }),
              new MenuItem("20 min",
                           (s,                                                                e) =>  { timeAxis.Zoom(0,       20 * 60); InvalidatePlot(); }),
              new MenuItem("30 min",
                           (s,                                                                e) =>  { timeAxis.Zoom(0,       30 * 60); InvalidatePlot(); }),
              new MenuItem("45 min",
                           (s,                                                                e) =>  { timeAxis.Zoom(0,       45 * 60); InvalidatePlot(); }),
              new MenuItem("1 h",
                           (s,                                                                e) =>  { timeAxis.Zoom(0,       60 * 60); InvalidatePlot(); }),
              new MenuItem("1.5 h",
                           (s,                                                                e) =>  { timeAxis.Zoom(0, 1.5 * 60 * 60); InvalidatePlot(); }),
              new MenuItem("2 h",
                           (s,                                                                e) =>  { timeAxis.Zoom(0,   2 * 60 * 60); InvalidatePlot(); }),
              new MenuItem("3 h",
                           (s,                                                                e) =>  { timeAxis.Zoom(0,   3 * 60 * 60); InvalidatePlot(); }),
              new MenuItem("6 h",
                           (s,                                                                e) =>  { timeAxis.Zoom(0,   6 * 60 * 60); InvalidatePlot(); }),
              new MenuItem("12 h",
                           (s,                                                                e) =>  { timeAxis.Zoom(0,  12 * 60 * 60); InvalidatePlot(); }),
              new MenuItem("24 h",
                           (s,                                                                e) =>  { timeAxis.Zoom(0,  24 * 60 * 60); InvalidatePlot(); }) };
            foreach (MenuItem mi in timeWindowMenuItems)
            {
                timeWindow.MenuItems.Add(mi);
            }
            menu.MenuItems.Add(timeWindow);

            return(menu);
        }
Пример #2
0
        private ContextMenu CreateMenu()
        {
            ContextMenu menu = new ContextMenu();

            MenuItem stackedAxesMenuItem = new MenuItem("堆叠轴");

            stackedAxes          = new UserOption("stackedAxes", true, stackedAxesMenuItem, settings);
            stackedAxes.Changed += (sender, e) =>
            {
                UpdateAxesPosition();
                InvalidatePlot();
            };
            menu.MenuItems.Add(stackedAxesMenuItem);

            MenuItem timeWindow = new MenuItem("时间窗");

            MenuItem[] timeWindowMenuItems =
            { new MenuItem("Auto",
                           (s,                                                               e) =>  { timeAxis.Zoom(0, double.NaN); InvalidatePlot();    }),
              new MenuItem("5 分钟",
                           (s,                                                               e) =>  { timeAxis.Zoom(0,        5 * 60); InvalidatePlot(); }),
              new MenuItem("10 分钟",
                           (s,                                                               e) =>  { timeAxis.Zoom(0,       10 * 60); InvalidatePlot(); }),
              new MenuItem("20 分钟",
                           (s,                                                               e) =>  { timeAxis.Zoom(0,       20 * 60); InvalidatePlot(); }),
              new MenuItem("30 分钟",
                           (s,                                                               e) =>  { timeAxis.Zoom(0,       30 * 60); InvalidatePlot(); }),
              new MenuItem("45 分钟",
                           (s,                                                               e) =>  { timeAxis.Zoom(0,       45 * 60); InvalidatePlot(); }),
              new MenuItem("1 小时",
                           (s,                                                               e) =>  { timeAxis.Zoom(0,       60 * 60); InvalidatePlot(); }),
              new MenuItem("1.5 小时",
                           (s,                                                               e) =>  { timeAxis.Zoom(0, 1.5 * 60 * 60); InvalidatePlot(); }),
              new MenuItem("2 小时",
                           (s,                                                               e) =>  { timeAxis.Zoom(0,   2 * 60 * 60); InvalidatePlot(); }),
              new MenuItem("3 小时",
                           (s,                                                               e) =>  { timeAxis.Zoom(0,   3 * 60 * 60); InvalidatePlot(); }),
              new MenuItem("6 小时",
                           (s,                                                               e) =>  { timeAxis.Zoom(0,   6 * 60 * 60); InvalidatePlot(); }),
              new MenuItem("12 小时",
                           (s,                                                               e) =>  { timeAxis.Zoom(0,  12 * 60 * 60); InvalidatePlot(); }),
              new MenuItem("24 小时",
                           (s,                                                               e) =>  { timeAxis.Zoom(0,  24 * 60 * 60); InvalidatePlot(); }) };
            foreach (MenuItem mi in timeWindowMenuItems)
            {
                timeWindow.MenuItems.Add(mi);
            }
            menu.MenuItems.Add(timeWindow);

            return(menu);
        }
Пример #3
0
        private ContextMenuStrip CreateMenu()
        {
            ContextMenuStrip menu = new ContextMenuStrip();

            ToolStripMenuItem stackedAxesMenuItem = new ToolStripMenuItem("Stacked Axes");

            stackedAxes = new UserOption("stackedAxes", true,
                                         stackedAxesMenuItem, settings);
            stackedAxes.Changed += (sender, e) => {
                UpdateAxesPosition();
                InvalidatePlot();
            };
            menu.Items.Add(stackedAxesMenuItem);

            ToolStripMenuItem timeWindow = new ToolStripMenuItem("Time Window");

            ToolStripMenuItem[] timeWindowMenuItems =
            { new ToolStripMenuItem("Auto",                                                         null,
                                    (s,                                                             e) => { timeAxis.Zoom(0,double.NaN); InvalidatePlot();      }),
              new ToolStripMenuItem("5 min",                                                            null,
                                    (s,                                                                 e) => { timeAxis.Zoom(0, 5 * 60); InvalidatePlot(); }),
              new ToolStripMenuItem("10 min",                                                           null,
                                    (s,                                                                 e) => { timeAxis.Zoom(0,10 * 60); InvalidatePlot(); }),
              new ToolStripMenuItem("20 min",                                                           null,
                                    (s,                                                                 e) => { timeAxis.Zoom(0,20 * 60); InvalidatePlot(); }),
              new ToolStripMenuItem("30 min",                                                           null,
                                    (s,                                                                 e) => { timeAxis.Zoom(0,30 * 60); InvalidatePlot(); }),
              new ToolStripMenuItem("45 min",                                                           null,
                                    (s,                                                                 e) => { timeAxis.Zoom(0,45 * 60); InvalidatePlot(); }),
              new ToolStripMenuItem("1 h",                                                              null,
                                    (s,                                                                 e) => { timeAxis.Zoom(0,60 * 60); InvalidatePlot(); }),
              new ToolStripMenuItem("1.5 h",                                                            null,
                                    (s,                                                                 e) => { timeAxis.Zoom(0,1.5 * 60 * 60); InvalidatePlot(); }),
              new ToolStripMenuItem("2 h",                                                              null,
                                    (s,                                                                 e) => { timeAxis.Zoom(0,2 * 60 * 60); InvalidatePlot(); }),
              new ToolStripMenuItem("3 h",                                                              null,
                                    (s,                                                                 e) => { timeAxis.Zoom(0,3 * 60 * 60); InvalidatePlot(); }),
              new ToolStripMenuItem("6 h",                                                              null,
                                    (s,                                                                 e) => { timeAxis.Zoom(0,6 * 60 * 60); InvalidatePlot(); }),
              new ToolStripMenuItem("12 h",                                                             null,
                                    (s,                                                                 e) => { timeAxis.Zoom(0,12 * 60 * 60); InvalidatePlot(); }),
              new ToolStripMenuItem("24 h",                                                             null,
                                    (s,                                                                 e) => { timeAxis.Zoom(0,24 * 60 * 60); InvalidatePlot(); }) };
            foreach (ToolStripMenuItem mi in timeWindowMenuItems)
            {
                timeWindow.DropDownItems.Add(mi);
            }
            menu.Items.Add(timeWindow);

            return(menu);
        }
    private ContextMenu CreateMenu() {
      ContextMenu menu = new ContextMenu();

      MenuItem stackedAxesMenuItem = new MenuItem("Stacked Axes");
      stackedAxes = new UserOption("stackedAxes", true,
        stackedAxesMenuItem, settings);
      stackedAxes.Changed += (sender, e) => {
        UpdateAxesPosition();
        InvalidatePlot();
      };
      menu.MenuItems.Add(stackedAxesMenuItem);

      MenuItem timeWindow = new MenuItem("Time Window");
      MenuItem[] timeWindowMenuItems =
        { new MenuItem("Auto", 
            (s, e) => { timeAxis.Zoom(0, double.NaN); InvalidatePlot(); }),
          new MenuItem("5 min", 
            (s, e) => { timeAxis.Zoom(0, 5 * 60); InvalidatePlot(); }),
          new MenuItem("10 min", 
            (s, e) => { timeAxis.Zoom(0, 10 * 60); InvalidatePlot(); }),
          new MenuItem("20 min", 
            (s, e) => { timeAxis.Zoom(0, 20 * 60); InvalidatePlot(); }),
          new MenuItem("30 min", 
            (s, e) => { timeAxis.Zoom(0, 30 * 60); InvalidatePlot(); }),
          new MenuItem("45 min", 
            (s, e) => { timeAxis.Zoom(0, 45 * 60); InvalidatePlot(); }),
          new MenuItem("1 h", 
            (s, e) => { timeAxis.Zoom(0, 60 * 60); InvalidatePlot(); }),
          new MenuItem("1.5 h", 
            (s, e) => { timeAxis.Zoom(0, 1.5 * 60 * 60); InvalidatePlot(); }),
          new MenuItem("2 h", 
            (s, e) => { timeAxis.Zoom(0, 2 * 60 * 60); InvalidatePlot(); }),
          new MenuItem("3 h", 
            (s, e) => { timeAxis.Zoom(0, 3 * 60 * 60); InvalidatePlot(); }),
          new MenuItem("6 h", 
            (s, e) => { timeAxis.Zoom(0, 6 * 60 * 60); InvalidatePlot(); }),
          new MenuItem("12 h", 
            (s, e) => { timeAxis.Zoom(0, 12 * 60 * 60); InvalidatePlot(); }),
          new MenuItem("24 h", 
            (s, e) => { timeAxis.Zoom(0, 24 * 60 * 60); InvalidatePlot(); }) };
      foreach (MenuItem mi in timeWindowMenuItems)
        timeWindow.MenuItems.Add(mi);
      menu.MenuItems.Add(timeWindow);

      return menu;
    }
Пример #5
0
        private void timer_Tick(object sender, EventArgs e)
        {
            computer.Accept(updateVisitor);
            treeView.Invalidate();
            plotPanel.InvalidatePlot();
            systemTray.Redraw();
            if (gadget != null)
            {
                gadget.Redraw();
            }

            if (wmiProvider != null)
            {
                wmiProvider.Update();
            }


            if (logSensors != null)
            {
                if (logSensors.Value && delayCount >= 4)
                {
                    logger.Log();
                }

                if (delayCount < 4)
                {
                    delayCount++;
                }
            }
            else
            {
                logSensors = new UserOption("logSensorsMenuItem", false, logSensorsMenuItem,
                                            settings);

                if (logSensors.Value && delayCount >= 4)
                {
                    logger.Log();
                }

                if (delayCount < 4)
                {
                    delayCount++;
                }
            }
        }
Пример #6
0
        public MainForm()
        {
            InitializeComponent();

            // check if the OpenHardwareMonitorLib assembly has the correct version
            if (Assembly.GetAssembly(typeof(Computer)).GetName().Version !=
                Assembly.GetExecutingAssembly().GetName().Version)
            {
                MessageBox.Show(
                    "The version of the file OpenHardwareMonitorLib.dll is incompatible.",
                    "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                Environment.Exit(0);
            }

            this.settings = new PersistentSettings();
            this.settings.Load(Path.ChangeExtension(
                                   Application.ExecutablePath, ".config"));

            this.unitManager = new UnitManager(settings);

            // make sure the buffers used for double buffering are not disposed
            // after each draw call
            BufferedGraphicsManager.Current.MaximumBuffer =
                Screen.PrimaryScreen.Bounds.Size;

            // set the DockStyle here, to avoid conflicts with the MainMenu
            this.splitContainer.Dock = DockStyle.Fill;

            this.Font     = SystemFonts.MessageBoxFont;
            treeView.Font = SystemFonts.MessageBoxFont;

            // Set the bounds immediately, so that our child components can be
            // properly placed.
            this.Bounds = new Rectangle {
                X      = settings.GetValue("mainForm.Location.X", Location.X),
                Y      = settings.GetValue("mainForm.Location.Y", Location.Y),
                Width  = settings.GetValue("mainForm.Width", 470),
                Height = settings.GetValue("mainForm.Height", 640)
            };

            plotPanel      = new PlotPanel(settings, unitManager);
            plotPanel.Font = SystemFonts.MessageBoxFont;
            plotPanel.Dock = DockStyle.Fill;

            nodeCheckBox.IsVisibleValueNeeded += nodeCheckBox_IsVisibleValueNeeded;
            nodeTextBoxText.DrawText          += nodeTextBoxText_DrawText;
            nodeTextBoxValue.DrawText         += nodeTextBoxText_DrawText;
            nodeTextBoxMin.DrawText           += nodeTextBoxText_DrawText;
            nodeTextBoxMax.DrawText           += nodeTextBoxText_DrawText;
            nodeTextBoxText.EditorShowing     += nodeTextBoxText_EditorShowing;

            foreach (TreeColumn column in treeView.Columns)
            {
                column.Width = Math.Max(20, Math.Min(400,
                                                     settings.GetValue("treeView.Columns." + column.Header + ".Width",
                                                                       column.Width)));
            }

            treeModel  = new TreeModel();
            root       = new Node(System.Environment.MachineName);
            root.Image = Utilities.EmbeddedResources.GetImage("computer.png");

            treeModel.Nodes.Add(root);
            treeView.Model = treeModel;

            this.computer = new Computer(settings);

            systemTray = new SystemTray(computer, settings, unitManager);
            systemTray.HideShowCommand += hideShowClick;
            systemTray.ExitCommand     += exitClick;

            int p = (int)Environment.OSVersion.Platform;

            if ((p == 4) || (p == 128)) // Unix
            {
                treeView.RowHeight           = Math.Max(treeView.RowHeight, 18);
                splitContainer.BorderStyle   = BorderStyle.None;
                splitContainer.Border3DStyle = Border3DStyle.Adjust;
                splitContainer.SplitterWidth = 4;
                treeView.BorderStyle         = BorderStyle.Fixed3D;
                plotPanel.BorderStyle        = BorderStyle.Fixed3D;
                gadgetMenuItem.Visible       = false;
                minCloseMenuItem.Visible     = false;
                minTrayMenuItem.Visible      = false;
                startMinMenuItem.Visible     = false;
            }
            else // Windows
            {
                treeView.RowHeight = Math.Max(treeView.Font.Height + 1, 18);

                gadget = new SensorGadget(computer, settings, unitManager);
                gadget.HideShowCommand += hideShowClick;

                wmiProvider = new WmiProvider(computer);
            }

            logger = new Logger(computer);

            plotColorPalette     = new Color[13];
            plotColorPalette[0]  = Color.Blue;
            plotColorPalette[1]  = Color.OrangeRed;
            plotColorPalette[2]  = Color.Green;
            plotColorPalette[3]  = Color.LightSeaGreen;
            plotColorPalette[4]  = Color.Goldenrod;
            plotColorPalette[5]  = Color.DarkViolet;
            plotColorPalette[6]  = Color.YellowGreen;
            plotColorPalette[7]  = Color.SaddleBrown;
            plotColorPalette[8]  = Color.RoyalBlue;
            plotColorPalette[9]  = Color.DeepPink;
            plotColorPalette[10] = Color.MediumSeaGreen;
            plotColorPalette[11] = Color.Olive;
            plotColorPalette[12] = Color.Firebrick;

            computer.HardwareAdded   += new HardwareEventHandler(HardwareAdded);
            computer.HardwareRemoved += new HardwareEventHandler(HardwareRemoved);

            computer.Open();

            timer.Enabled = true;

            showHiddenSensors = new UserOption("hiddenMenuItem", false,
                                               hiddenMenuItem, settings);
            showHiddenSensors.Changed += delegate(object sender, EventArgs e) {
                treeModel.ForceVisible = showHiddenSensors.Value;
            };

            showValue = new UserOption("valueMenuItem", true, valueMenuItem,
                                       settings);
            showValue.Changed += delegate(object sender, EventArgs e) {
                treeView.Columns[1].IsVisible = showValue.Value;
            };

            showMin          = new UserOption("minMenuItem", false, minMenuItem, settings);
            showMin.Changed += delegate(object sender, EventArgs e) {
                treeView.Columns[2].IsVisible = showMin.Value;
            };

            showMax          = new UserOption("maxMenuItem", true, maxMenuItem, settings);
            showMax.Changed += delegate(object sender, EventArgs e) {
                treeView.Columns[3].IsVisible = showMax.Value;
            };

            startMinimized = new UserOption("startMinMenuItem", false,
                                            startMinMenuItem, settings);

            minimizeToTray = new UserOption("minTrayMenuItem", true,
                                            minTrayMenuItem, settings);
            minimizeToTray.Changed += delegate(object sender, EventArgs e) {
                systemTray.IsMainIconEnabled = minimizeToTray.Value;
            };

            minimizeOnClose = new UserOption("minCloseMenuItem", false,
                                             minCloseMenuItem, settings);

            autoStart = new UserOption(null, startupManager.Startup,
                                       startupMenuItem, settings);
            autoStart.Changed += delegate(object sender, EventArgs e) {
                try {
                    startupManager.Startup = autoStart.Value;
                } catch (InvalidOperationException) {
                    MessageBox.Show("Updating the auto-startup option failed.", "Error",
                                    MessageBoxButtons.OK, MessageBoxIcon.Error);
                    autoStart.Value = startupManager.Startup;
                }
            };

            readMainboardSensors = new UserOption("mainboardMenuItem", true,
                                                  mainboardMenuItem, settings);
            readMainboardSensors.Changed += delegate(object sender, EventArgs e) {
                computer.MainboardEnabled = readMainboardSensors.Value;
            };

            readCpuSensors = new UserOption("cpuMenuItem", true,
                                            cpuMenuItem, settings);
            readCpuSensors.Changed += delegate(object sender, EventArgs e) {
                computer.CPUEnabled = readCpuSensors.Value;
            };

            readRamSensors = new UserOption("ramMenuItem", true,
                                            ramMenuItem, settings);
            readRamSensors.Changed += delegate(object sender, EventArgs e) {
                computer.RAMEnabled = readRamSensors.Value;
            };

            readGpuSensors = new UserOption("gpuMenuItem", true,
                                            gpuMenuItem, settings);
            readGpuSensors.Changed += delegate(object sender, EventArgs e) {
                computer.GPUEnabled = readGpuSensors.Value;
            };

            readFanControllersSensors = new UserOption("fanControllerMenuItem", true,
                                                       fanControllerMenuItem, settings);
            readFanControllersSensors.Changed += delegate(object sender, EventArgs e) {
                computer.FanControllerEnabled = readFanControllersSensors.Value;
            };

            readHddSensors = new UserOption("hddMenuItem", true, hddMenuItem,
                                            settings);
            readHddSensors.Changed += delegate(object sender, EventArgs e) {
                computer.HDDEnabled = readHddSensors.Value;
            };

            readNicSensors = new UserOption("nicMenuItem", true, nicMenuItem,
                                            settings);
            readNicSensors.Changed += delegate(object sender, EventArgs e) {
                computer.NICEnabled = readNicSensors.Value;
            };

            showGadget = new UserOption("gadgetMenuItem", false, gadgetMenuItem,
                                        settings);
            showGadget.Changed += delegate(object sender, EventArgs e) {
                if (gadget != null)
                {
                    gadget.Visible = showGadget.Value;
                }
            };

            celsiusMenuItem.Checked =
                unitManager.TemperatureUnit == TemperatureUnit.Celsius;
            fahrenheitMenuItem.Checked = !celsiusMenuItem.Checked;

            server = new HttpServer(root, this.settings.GetValue("listenerPort", 8085));
            if (server.PlatformNotSupported)
            {
                webMenuItemSeparator.Visible = false;
                webMenuItem.Visible          = false;
            }

            runWebServer = new UserOption("runWebServerMenuItem", false,
                                          runWebServerMenuItem, settings);
            runWebServer.Changed += delegate(object sender, EventArgs e) {
                if (runWebServer.Value)
                {
                    server.StartHTTPListener();
                }
                else
                {
                    server.StopHTTPListener();
                }
            };

            logSensors = new UserOption("logSensorsMenuItem", false, logSensorsMenuItem,
                                        settings);

            loggingInterval = new UserRadioGroup("loggingInterval", 0,
                                                 new[] { log1sMenuItem, log2sMenuItem, log5sMenuItem, log10sMenuItem,
                                                         log30sMenuItem, log1minMenuItem, log2minMenuItem, log5minMenuItem,
                                                         log10minMenuItem, log30minMenuItem, log1hMenuItem, log2hMenuItem,
                                                         log6hMenuItem },
                                                 settings);
            loggingInterval.Changed += (sender, e) => {
                switch (loggingInterval.Value)
                {
                case 0: logger.LoggingInterval = new TimeSpan(0, 0, 1); break;

                case 1: logger.LoggingInterval = new TimeSpan(0, 0, 2); break;

                case 2: logger.LoggingInterval = new TimeSpan(0, 0, 5); break;

                case 3: logger.LoggingInterval = new TimeSpan(0, 0, 10); break;

                case 4: logger.LoggingInterval = new TimeSpan(0, 0, 30); break;

                case 5: logger.LoggingInterval = new TimeSpan(0, 1, 0); break;

                case 6: logger.LoggingInterval = new TimeSpan(0, 2, 0); break;

                case 7: logger.LoggingInterval = new TimeSpan(0, 5, 0); break;

                case 8: logger.LoggingInterval = new TimeSpan(0, 10, 0); break;

                case 9: logger.LoggingInterval = new TimeSpan(0, 30, 0); break;

                case 10: logger.LoggingInterval = new TimeSpan(1, 0, 0); break;

                case 11: logger.LoggingInterval = new TimeSpan(2, 0, 0); break;

                case 12: logger.LoggingInterval = new TimeSpan(6, 0, 0); break;
                }
            };

            sensorValuesTimeWindow = new UserRadioGroup("sensorValuesTimeWindow", 10,
                                                        new[] { timeWindow30sMenuItem, timeWindow1minMenuItem, timeWindow2minMenuItem,
                                                                timeWindow5minMenuItem, timeWindow10minMenuItem, timeWindow30minMenuItem,
                                                                timeWindow1hMenuItem, timeWindow2hMenuItem, timeWindow6hMenuItem,
                                                                timeWindow12hMenuItem, timeWindow24hMenuItem },
                                                        settings);
            sensorValuesTimeWindow.Changed += (sender, e) => {
                TimeSpan timeWindow = TimeSpan.Zero;
                switch (sensorValuesTimeWindow.Value)
                {
                case 0: timeWindow = new TimeSpan(0, 0, 30); break;

                case 1: timeWindow = new TimeSpan(0, 1, 0); break;

                case 2: timeWindow = new TimeSpan(0, 2, 0); break;

                case 3: timeWindow = new TimeSpan(0, 5, 0); break;

                case 4: timeWindow = new TimeSpan(0, 10, 0); break;

                case 5: timeWindow = new TimeSpan(0, 30, 0); break;

                case 6: timeWindow = new TimeSpan(1, 0, 0); break;

                case 7: timeWindow = new TimeSpan(2, 0, 0); break;

                case 8: timeWindow = new TimeSpan(6, 0, 0); break;

                case 9: timeWindow = new TimeSpan(12, 0, 0); break;

                case 10: timeWindow = new TimeSpan(24, 0, 0); break;
                }

                computer.Accept(new SensorVisitor(delegate(ISensor sensor) {
                    sensor.ValuesTimeWindow = timeWindow;
                }));
            };

            InitializePlotForm();
            InitializeSplitter();

            startupMenuItem.Visible = startupManager.IsAvailable;

            if (startMinMenuItem.Checked)
            {
                if (!minTrayMenuItem.Checked)
                {
                    WindowState = FormWindowState.Minimized;
                    Show();
                }
            }
            else
            {
                Show();
            }

            // Create a handle, otherwise calling Close() does not fire FormClosed
            IntPtr handle = Handle;

            // Make sure the settings are saved when the user logs off
            Microsoft.Win32.SystemEvents.SessionEnded += delegate {
                computer.Close();
                SaveConfiguration();
                if (runWebServer.Value)
                {
                    server.Quit();
                }
            };
        }
Пример #7
0
        private void InitializePlotForm()
        {
            plotForm = new Form();
            plotForm.FormBorderStyle = FormBorderStyle.SizableToolWindow;
            plotForm.ShowInTaskbar   = false;
            plotForm.StartPosition   = FormStartPosition.Manual;
            this.AddOwnedForm(plotForm);
            plotForm.Bounds = new Rectangle {
                X      = settings.GetValue("plotForm.Location.X", -100000),
                Y      = settings.GetValue("plotForm.Location.Y", 100),
                Width  = settings.GetValue("plotForm.Width", 600),
                Height = settings.GetValue("plotForm.Height", 400)
            };

            showPlot     = new UserOption("plotMenuItem", false, plotMenuItem, settings);
            plotLocation = new UserRadioGroup("plotLocation", 0,
                                              new[] { plotWindowMenuItem, plotBottomMenuItem, plotRightMenuItem },
                                              settings);

            showPlot.Changed += delegate(object sender, EventArgs e) {
                if (plotLocation.Value == 0)
                {
                    if (showPlot.Value && this.Visible)
                    {
                        plotForm.Show();
                    }
                    else
                    {
                        plotForm.Hide();
                    }
                }
                else
                {
                    splitContainer.Panel2Collapsed = !showPlot.Value;
                }
                treeView.Invalidate();
            };
            plotLocation.Changed += delegate(object sender, EventArgs e) {
                switch (plotLocation.Value)
                {
                case 0:
                    splitContainer.Panel2.Controls.Clear();
                    splitContainer.Panel2Collapsed = true;
                    plotForm.Controls.Add(plotPanel);
                    if (showPlot.Value && this.Visible)
                    {
                        plotForm.Show();
                    }
                    break;

                case 1:
                    plotForm.Controls.Clear();
                    plotForm.Hide();
                    splitContainer.Orientation = Orientation.Horizontal;
                    splitContainer.Panel2.Controls.Add(plotPanel);
                    splitContainer.Panel2Collapsed = !showPlot.Value;
                    break;

                case 2:
                    plotForm.Controls.Clear();
                    plotForm.Hide();
                    splitContainer.Orientation = Orientation.Vertical;
                    splitContainer.Panel2.Controls.Add(plotPanel);
                    splitContainer.Panel2Collapsed = !showPlot.Value;
                    break;
                }
            };

            plotForm.FormClosing += delegate(object sender, FormClosingEventArgs e) {
                if (e.CloseReason == CloseReason.UserClosing)
                {
                    // just switch off the plotting when the user closes the form
                    if (plotLocation.Value == 0)
                    {
                        showPlot.Value = false;
                    }
                    e.Cancel = true;
                }
            };

            EventHandler moveOrResizePlotForm = delegate(object sender, EventArgs e) {
                if (plotForm.WindowState != FormWindowState.Minimized)
                {
                    settings.SetValue("plotForm.Location.X", plotForm.Bounds.X);
                    settings.SetValue("plotForm.Location.Y", plotForm.Bounds.Y);
                    settings.SetValue("plotForm.Width", plotForm.Bounds.Width);
                    settings.SetValue("plotForm.Height", plotForm.Bounds.Height);
                }
            };

            plotForm.Move   += moveOrResizePlotForm;
            plotForm.Resize += moveOrResizePlotForm;

            plotForm.VisibleChanged += delegate(object sender, EventArgs e) {
                Rectangle bounds       = new Rectangle(plotForm.Location, plotForm.Size);
                Screen    screen       = Screen.FromRectangle(bounds);
                Rectangle intersection =
                    Rectangle.Intersect(screen.WorkingArea, bounds);
                if (intersection.Width < Math.Min(16, bounds.Width) ||
                    intersection.Height < Math.Min(16, bounds.Height))
                {
                    plotForm.Location = new Point(
                        screen.WorkingArea.Width / 2 - bounds.Width / 2,
                        screen.WorkingArea.Height / 2 - bounds.Height / 2);
                }
            };

            this.VisibleChanged += delegate(object sender, EventArgs e) {
                if (this.Visible && showPlot.Value && plotLocation.Value == 0)
                {
                    plotForm.Show();
                }
                else
                {
                    plotForm.Hide();
                }
            };
        }
Пример #8
0
        public MainForm()
        {
            InitializeComponent();

              // check if the OpenHardwareMonitorLib assembly has the correct version
              if (Assembly.GetAssembly(typeof(Computer)).GetName().Version !=
            Assembly.GetExecutingAssembly().GetName().Version) {
            MessageBox.Show(
              "The version of the file OpenHardwareMonitorLib.dll is incompatible.",
              "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            Environment.Exit(0);
              }

              this.settings = new PersistentSettings();
              this.settings.Load(Path.ChangeExtension(
            Application.ExecutablePath, ".config"));

              this.unitManager = new UnitManager(settings);

              // make sure the buffers used for double buffering are not disposed
              // after each draw call
              BufferedGraphicsManager.Current.MaximumBuffer =
            Screen.PrimaryScreen.Bounds.Size;

              // set the DockStyle here, to avoid conflicts with the MainMenu
              this.splitContainer.Dock = DockStyle.Fill;

              this.Font = SystemFonts.MessageBoxFont;
              treeView.Font = SystemFonts.MessageBoxFont;

              plotPanel = new PlotPanel(settings, unitManager);
              plotPanel.Font = SystemFonts.MessageBoxFont;
              plotPanel.Dock = DockStyle.Fill;

              nodeCheckBox.IsVisibleValueNeeded += nodeCheckBox_IsVisibleValueNeeded;
              nodeTextBoxText.DrawText += nodeTextBoxText_DrawText;
              nodeTextBoxValue.DrawText += nodeTextBoxText_DrawText;
              nodeTextBoxMin.DrawText += nodeTextBoxText_DrawText;
              nodeTextBoxMax.DrawText += nodeTextBoxText_DrawText;
              nodeTextBoxText.EditorShowing += nodeTextBoxText_EditorShowing;

              foreach (TreeColumn column in treeView.Columns)
            column.Width = Math.Max(20, Math.Min(400,
              settings.GetValue("treeView.Columns." + column.Header + ".Width",
              column.Width)));

              treeModel = new TreeModel();
              root = new Node(System.Environment.MachineName);
              root.Image = Utilities.EmbeddedResources.GetImage("computer.png");

              treeModel.Nodes.Add(root);
              treeView.Model = treeModel;

              this.computer = new Computer(settings);

              systemTray = new SystemTray(computer, settings, unitManager);
              systemTray.HideShowCommand += hideShowClick;
              systemTray.ExitCommand += exitClick;

              int p = (int)Environment.OSVersion.Platform;
              if ((p == 4) || (p == 128)) { // Unix
            treeView.RowHeight = Math.Max(treeView.RowHeight, 18);
            splitContainer.BorderStyle = BorderStyle.None;
            splitContainer.Border3DStyle = Border3DStyle.Adjust;
            splitContainer.SplitterWidth = 4;
            treeView.BorderStyle = BorderStyle.Fixed3D;
            plotPanel.BorderStyle = BorderStyle.Fixed3D;
            gadgetMenuItem.Visible = false;
            minCloseMenuItem.Visible = false;
            minTrayMenuItem.Visible = false;
            startMinMenuItem.Visible = false;
              } else { // Windows
            treeView.RowHeight = Math.Max(treeView.Font.Height + 1, 18);

            gadget = new SensorGadget(computer, settings, unitManager);
            gadget.HideShowCommand += hideShowClick;

            wmiProvider = new WmiProvider(computer);
              }

              logger = new Logger(computer);

              plotColorPalette = new Color[13];
              plotColorPalette[0] = Color.Blue;
              plotColorPalette[1] = Color.OrangeRed;
              plotColorPalette[2] = Color.Green;
              plotColorPalette[3] = Color.LightSeaGreen;
              plotColorPalette[4] = Color.Goldenrod;
              plotColorPalette[5] = Color.DarkViolet;
              plotColorPalette[6] = Color.YellowGreen;
              plotColorPalette[7] = Color.SaddleBrown;
              plotColorPalette[8] = Color.RoyalBlue;
              plotColorPalette[9] = Color.DeepPink;
              plotColorPalette[10] = Color.MediumSeaGreen;
              plotColorPalette[11] = Color.Olive;
              plotColorPalette[12] = Color.Firebrick;

              computer.HardwareAdded += new HardwareEventHandler(HardwareAdded);
              computer.HardwareRemoved += new HardwareEventHandler(HardwareRemoved);

              computer.Open();

              timer.Enabled = true;

              showHiddenSensors = new UserOption("hiddenMenuItem", false,
            hiddenMenuItem, settings);
              showHiddenSensors.Changed += delegate(object sender, EventArgs e) {
            treeModel.ForceVisible = showHiddenSensors.Value;
              };

              showValue = new UserOption("valueMenuItem", true, valueMenuItem,
            settings);
              showValue.Changed += delegate(object sender, EventArgs e) {
            treeView.Columns[1].IsVisible = showValue.Value;
              };

              showMin = new UserOption("minMenuItem", false, minMenuItem, settings);
              showMin.Changed += delegate(object sender, EventArgs e) {
            treeView.Columns[2].IsVisible = showMin.Value;
              };

              showMax = new UserOption("maxMenuItem", true, maxMenuItem, settings);
              showMax.Changed += delegate(object sender, EventArgs e) {
            treeView.Columns[3].IsVisible = showMax.Value;
              };

              startMinimized = new UserOption("startMinMenuItem", false,
            startMinMenuItem, settings);

              minimizeToTray = new UserOption("minTrayMenuItem", true,
            minTrayMenuItem, settings);
              minimizeToTray.Changed += delegate(object sender, EventArgs e) {
            systemTray.IsMainIconEnabled = minimizeToTray.Value;
              };

              minimizeOnClose = new UserOption("minCloseMenuItem", false,
            minCloseMenuItem, settings);

              autoStart = new UserOption(null, startupManager.Startup,
            startupMenuItem, settings);
              autoStart.Changed += delegate(object sender, EventArgs e) {
            try {
              startupManager.Startup = autoStart.Value;
            } catch (InvalidOperationException) {
              MessageBox.Show("Updating the auto-startup option failed.", "Error",
            MessageBoxButtons.OK, MessageBoxIcon.Error);
              autoStart.Value = startupManager.Startup;
            }
              };

              readMainboardSensors = new UserOption("mainboardMenuItem", true,
            mainboardMenuItem, settings);
              readMainboardSensors.Changed += delegate(object sender, EventArgs e) {
            computer.MainboardEnabled = readMainboardSensors.Value;
              };

              readCpuSensors = new UserOption("cpuMenuItem", true,
            cpuMenuItem, settings);
              readCpuSensors.Changed += delegate(object sender, EventArgs e) {
            computer.CPUEnabled = readCpuSensors.Value;
              };

              readRamSensors = new UserOption("ramMenuItem", true,
            ramMenuItem, settings);
              readRamSensors.Changed += delegate(object sender, EventArgs e) {
            computer.RAMEnabled = readRamSensors.Value;
              };

              readGpuSensors = new UserOption("gpuMenuItem", true,
            gpuMenuItem, settings);
              readGpuSensors.Changed += delegate(object sender, EventArgs e) {
            computer.GPUEnabled = readGpuSensors.Value;
              };

              readFanControllersSensors = new UserOption("fanControllerMenuItem", true,
            fanControllerMenuItem, settings);
              readFanControllersSensors.Changed += delegate(object sender, EventArgs e) {
            computer.FanControllerEnabled = readFanControllersSensors.Value;
              };

              readHddSensors = new UserOption("hddMenuItem", true, hddMenuItem,
            settings);
              readHddSensors.Changed += delegate(object sender, EventArgs e) {
            computer.HDDEnabled = readHddSensors.Value;
              };

              showGadget = new UserOption("gadgetMenuItem", false, gadgetMenuItem,
            settings);
              showGadget.Changed += delegate(object sender, EventArgs e) {
            if (gadget != null)
              gadget.Visible = showGadget.Value;
              };

              celsiusMenuItem.Checked =
            unitManager.TemperatureUnit == TemperatureUnit.Celsius;
              fahrenheitMenuItem.Checked = !celsiusMenuItem.Checked;

              server = new HttpServer(root, this.settings.GetValue("listenerPort", 8085));
              if (server.PlatformNotSupported) {
            webMenuItemSeparator.Visible = false;
            webMenuItem.Visible = false;
              }

              runWebServer = new UserOption("runWebServerMenuItem", false,
            runWebServerMenuItem, settings);
              runWebServer.Changed += delegate(object sender, EventArgs e) {
            if (runWebServer.Value)
              server.StartHTTPListener();
            else
              server.StopHTTPListener();
              };

              logSensors = new UserOption("logSensorsMenuItem", false, logSensorsMenuItem,
            settings);

              loggingInterval = new UserRadioGroup("loggingInterval", 0,
            new[] { log1sMenuItem, log2sMenuItem, log5sMenuItem, log10sMenuItem,
            log30sMenuItem, log1minMenuItem, log2minMenuItem, log5minMenuItem,
            log10minMenuItem, log30minMenuItem, log1hMenuItem, log2hMenuItem,
            log6hMenuItem},
            settings);
              loggingInterval.Changed += (sender, e) => {
            switch (loggingInterval.Value) {
              case 0: logger.LoggingInterval = new TimeSpan(0, 0, 1); break;
              case 1: logger.LoggingInterval = new TimeSpan(0, 0, 2); break;
              case 2: logger.LoggingInterval = new TimeSpan(0, 0, 5); break;
              case 3: logger.LoggingInterval = new TimeSpan(0, 0, 10); break;
              case 4: logger.LoggingInterval = new TimeSpan(0, 0, 30); break;
              case 5: logger.LoggingInterval = new TimeSpan(0, 1, 0); break;
              case 6: logger.LoggingInterval = new TimeSpan(0, 2, 0); break;
              case 7: logger.LoggingInterval = new TimeSpan(0, 5, 0); break;
              case 8: logger.LoggingInterval = new TimeSpan(0, 10, 0); break;
              case 9: logger.LoggingInterval = new TimeSpan(0, 30, 0); break;
              case 10: logger.LoggingInterval = new TimeSpan(1, 0, 0); break;
              case 11: logger.LoggingInterval = new TimeSpan(2, 0, 0); break;
              case 12: logger.LoggingInterval = new TimeSpan(6, 0, 0); break;
            }
              };

              InitializePlotForm();

              startupMenuItem.Visible = startupManager.IsAvailable;

              if (startMinMenuItem.Checked) {
            if (!minTrayMenuItem.Checked) {
              WindowState = FormWindowState.Minimized;
              Show();
            }
              } else {
            Show();
              }

              // Create a handle, otherwise calling Close() does not fire FormClosed
              IntPtr handle = Handle;

              // Make sure the settings are saved when the user logs off
              Microsoft.Win32.SystemEvents.SessionEnded += delegate {
            computer.Close();
            SaveConfiguration();
            if (runWebServer.Value)
              server.Quit();
              };
        }
Пример #9
0
        private void InitializePlotForm()
        {
            plotForm = new Form();
              plotForm.FormBorderStyle = FormBorderStyle.SizableToolWindow;
              plotForm.ShowInTaskbar = false;
              plotForm.StartPosition = FormStartPosition.Manual;
              this.AddOwnedForm(plotForm);
              plotForm.Bounds = new Rectangle {
            X = settings.GetValue("plotForm.Location.X", -100000),
            Y = settings.GetValue("plotForm.Location.Y", 100),
            Width = settings.GetValue("plotForm.Width", 600),
            Height = settings.GetValue("plotForm.Height", 400)
              };

              showPlot = new UserOption("plotMenuItem", false, plotMenuItem, settings);
              plotLocation = new UserRadioGroup("plotLocation", 0,
            new[] { plotWindowMenuItem, plotBottomMenuItem, plotRightMenuItem },
            settings);

              showPlot.Changed += delegate(object sender, EventArgs e) {
            if (plotLocation.Value == 0) {
              if (showPlot.Value && this.Visible)
            plotForm.Show();
              else
            plotForm.Hide();
            } else {
              splitContainer.Panel2Collapsed = !showPlot.Value;
            }
            treeView.Invalidate();
              };
              plotLocation.Changed += delegate(object sender, EventArgs e) {
            switch (plotLocation.Value) {
              case 0:
            splitContainer.Panel2.Controls.Clear();
            splitContainer.Panel2Collapsed = true;
            plotForm.Controls.Add(plotPanel);
            if (showPlot.Value && this.Visible)
              plotForm.Show();
            break;
              case 1:
            plotForm.Controls.Clear();
            plotForm.Hide();
            splitContainer.Orientation = Orientation.Horizontal;
            splitContainer.Panel2.Controls.Add(plotPanel);
            splitContainer.Panel2Collapsed = !showPlot.Value;
            break;
              case 2:
            plotForm.Controls.Clear();
            plotForm.Hide();
            splitContainer.Orientation = Orientation.Vertical;
            splitContainer.Panel2.Controls.Add(plotPanel);
            splitContainer.Panel2Collapsed = !showPlot.Value;
            break;
            }
              };

              plotForm.FormClosing += delegate(object sender, FormClosingEventArgs e) {
            if (e.CloseReason == CloseReason.UserClosing) {
              // just switch off the plotting when the user closes the form
              if (plotLocation.Value == 0) {
            showPlot.Value = false;
              }
              e.Cancel = true;
            }
              };

              EventHandler moveOrResizePlotForm = delegate(object sender, EventArgs e) {
            if (plotForm.WindowState != FormWindowState.Minimized) {
              settings.SetValue("plotForm.Location.X", plotForm.Bounds.X);
              settings.SetValue("plotForm.Location.Y", plotForm.Bounds.Y);
              settings.SetValue("plotForm.Width", plotForm.Bounds.Width);
              settings.SetValue("plotForm.Height", plotForm.Bounds.Height);
            }
              };
              plotForm.Move += moveOrResizePlotForm;
              plotForm.Resize += moveOrResizePlotForm;

              plotForm.VisibleChanged += delegate(object sender, EventArgs e) {
            Rectangle bounds = new Rectangle(plotForm.Location, plotForm.Size);
            Screen screen = Screen.FromRectangle(bounds);
            Rectangle intersection =
              Rectangle.Intersect(screen.WorkingArea, bounds);
            if (intersection.Width < Math.Min(16, bounds.Width) ||
            intersection.Height < Math.Min(16, bounds.Height)) {
              plotForm.Location = new Point(
            screen.WorkingArea.Width / 2 - bounds.Width / 2,
            screen.WorkingArea.Height / 2 - bounds.Height / 2);
            }
              };

              this.VisibleChanged += delegate(object sender, EventArgs e) {
            if (this.Visible && showPlot.Value && plotLocation.Value == 0)
              plotForm.Show();
            else
              plotForm.Hide();
              };
        }
Пример #10
0
        public SensorGadget(IComputer computer, PersistentSettings settings,
                            UnitManager unitManager)
        {
            this.unitManager          = unitManager;
            this.settings             = settings;
            computer.HardwareAdded   += new HardwareEventHandler(HardwareAdded);
            computer.HardwareRemoved += new HardwareEventHandler(HardwareRemoved);

            this.darkWhite = new SolidBrush(Color.FromArgb(0xF0, 0xF0, 0xF0));

            this.stringFormat             = new StringFormat();
            this.stringFormat.FormatFlags = StringFormatFlags.NoWrap;

            this.trimStringFormat             = new StringFormat();
            this.trimStringFormat.Trimming    = StringTrimming.EllipsisCharacter;
            this.trimStringFormat.FormatFlags = StringFormatFlags.NoWrap;

            this.alignRightStringFormat             = new StringFormat();
            this.alignRightStringFormat.Alignment   = StringAlignment.Far;
            this.alignRightStringFormat.FormatFlags = StringFormatFlags.NoWrap;

            this.Location = new Point(
                settings.GetValue("sensorGadget.Location.X", 100),
                settings.GetValue("sensorGadget.Location.Y", 100));
            LocationChanged += delegate(object sender, EventArgs e) {
                settings.SetValue("sensorGadget.Location.X", Location.X);
                settings.SetValue("sensorGadget.Location.Y", Location.Y);
            };

            SetFontSize(settings.GetValue("sensorGadget.FontSize", 7.5f));
            Resize(settings.GetValue("sensorGadget.Width", Size.Width));

            ContextMenu contextMenu       = new ContextMenu();
            MenuItem    hardwareNamesItem = new MenuItem("Hardware Names");

            contextMenu.MenuItems.Add(hardwareNamesItem);
            MenuItem fontSizeMenu = new MenuItem("Font Size");

            for (int i = 0; i < 4; i++)
            {
                float  size;
                string name;
                switch (i)
                {
                case 0: size = 6.5f; name = "Small"; break;

                case 1: size = 7.5f; name = "Medium"; break;

                case 2: size = 9f; name = "Large"; break;

                case 3: size = 11f; name = "Very Large"; break;

                default: throw new NotImplementedException();
                }
                MenuItem item = new MenuItem(name);
                item.Checked = fontSize == size;
                item.Click  += delegate(object sender, EventArgs e) {
                    SetFontSize(size);
                    settings.SetValue("sensorGadget.FontSize", size);
                    foreach (MenuItem mi in fontSizeMenu.MenuItems)
                    {
                        mi.Checked = mi == item;
                    }
                };
                fontSizeMenu.MenuItems.Add(item);
            }
            contextMenu.MenuItems.Add(fontSizeMenu);
            contextMenu.MenuItems.Add(new MenuItem("-"));
            MenuItem lockItem = new MenuItem("Lock Position and Size");

            contextMenu.MenuItems.Add(lockItem);
            contextMenu.MenuItems.Add(new MenuItem("-"));
            MenuItem alwaysOnTopItem = new MenuItem("Always on Top");

            contextMenu.MenuItems.Add(alwaysOnTopItem);
            MenuItem opacityMenu = new MenuItem("Opacity");

            contextMenu.MenuItems.Add(opacityMenu);
            Opacity = (byte)settings.GetValue("sensorGadget.Opacity", 255);
            for (int i = 0; i < 5; i++)
            {
                MenuItem item = new MenuItem((20 * (i + 1)).ToString() + " %");
                byte     o    = (byte)(51 * (i + 1));
                item.Checked = Opacity == o;
                item.Click  += delegate(object sender, EventArgs e) {
                    Opacity = o;
                    settings.SetValue("sensorGadget.Opacity", Opacity);
                    foreach (MenuItem mi in opacityMenu.MenuItems)
                    {
                        mi.Checked = mi == item;
                    }
                };
                opacityMenu.MenuItems.Add(item);
            }
            this.ContextMenu = contextMenu;

            hardwareNames = new UserOption("sensorGadget.Hardwarenames", true,
                                           hardwareNamesItem, settings);
            hardwareNames.Changed += delegate(object sender, EventArgs e) {
                Resize();
            };

            alwaysOnTop = new UserOption("sensorGadget.AlwaysOnTop", false,
                                         alwaysOnTopItem, settings);
            alwaysOnTop.Changed += delegate(object sender, EventArgs e) {
                this.AlwaysOnTop = alwaysOnTop.Value;
            };
            lockPositionAndSize = new UserOption("sensorGadget.LockPositionAndSize",
                                                 false, lockItem, settings);
            lockPositionAndSize.Changed += delegate(object sender, EventArgs e) {
                this.LockPositionAndSize = lockPositionAndSize.Value;
            };

            HitTest += delegate(object sender, HitTestEventArgs e) {
                if (lockPositionAndSize.Value)
                {
                    return;
                }

                if (e.Location.X < leftBorder)
                {
                    e.HitResult = HitResult.Left;
                    return;
                }
                if (e.Location.X > Size.Width - 1 - rightBorder)
                {
                    e.HitResult = HitResult.Right;
                    return;
                }
            };

            SizeChanged += delegate(object sender, EventArgs e) {
                settings.SetValue("sensorGadget.Width", Size.Width);
                Redraw();
            };
        }
Пример #11
0
        public SensorGadget(IComputer computer, PersistentSettings settings,
                            UnitManager unitManager)
        {
            this.unitManager          = unitManager;
            this.settings             = settings;
            computer.HardwareAdded   += new HardwareEventHandler(HardwareAdded);
            computer.HardwareRemoved += new HardwareEventHandler(HardwareRemoved);

            this.darkWhite = new SolidBrush(Color.FromArgb(0xF0, 0xF0, 0xF0));

            this.stringFormat             = new StringFormat();
            this.stringFormat.FormatFlags = StringFormatFlags.NoWrap;

            this.trimStringFormat             = new StringFormat();
            this.trimStringFormat.Trimming    = StringTrimming.EllipsisCharacter;
            this.trimStringFormat.FormatFlags = StringFormatFlags.NoWrap;

            this.alignRightStringFormat             = new StringFormat();
            this.alignRightStringFormat.Alignment   = StringAlignment.Far;
            this.alignRightStringFormat.FormatFlags = StringFormatFlags.NoWrap;

            if (File.Exists("gadget_background.png"))
            {
                try {
                    Image newBack = new Bitmap("gadget_background.png");
                    back.Dispose();
                    back = newBack;
                } catch { }
            }

            if (File.Exists("gadget_image.png"))
            {
                try {
                    image = new Bitmap("gadget_image.png");
                } catch {}
            }

            if (File.Exists("gadget_foreground.png"))
            {
                try {
                    fore = new Bitmap("gadget_foreground.png");
                } catch { }
            }

            if (File.Exists("gadget_bar_background.png"))
            {
                try {
                    Image newBarBack = new Bitmap("gadget_bar_background.png");
                    barBack.Dispose();
                    barBack = newBarBack;
                } catch { }
            }

            if (File.Exists("gadget_bar_foreground.png"))
            {
                try {
                    Image newBarColor = new Bitmap("gadget_bar_foreground.png");
                    barFore.Dispose();
                    barFore = newBarColor;
                } catch { }
            }

            this.Location = new Point(
                settings.GetValue("sensorGadget.Location.X", 100),
                settings.GetValue("sensorGadget.Location.Y", 100));
            LocationChanged += delegate(object sender, EventArgs e) {
                settings.SetValue("sensorGadget.Location.X", Location.X);
                settings.SetValue("sensorGadget.Location.Y", Location.Y);
            };

            // get the custom to default dpi ratio
            using (Bitmap b = new Bitmap(1, 1)) {
                scale = b.HorizontalResolution / 96.0f;
            }

            progressWidthMult = settings.GetValue("sensorGadget.ProgressWidthMult", 1.0f);
            SetFontSize(settings.GetValue("sensorGadget.FontSize", 7.5f));
            percentageDisplayType = (PercentageDisplayType)settings.GetValue("sensorGadget.PercentageDisplayType", 0);
            Resize(settings.GetValue("sensorGadget.Width", Size.Width));

            ContextMenu contextMenu       = new ContextMenu();
            MenuItem    hardwareNamesItem = new MenuItem("Hardware Names");

            contextMenu.MenuItems.Add(hardwareNamesItem);
            MenuItem fontSizeMenu = new MenuItem("Font Size");

            for (int i = 0; i < 4; i++)
            {
                float  size;
                string name;
                switch (i)
                {
                case 0: size = 6.5f; name = "Small"; break;

                case 1: size = 7.5f; name = "Medium"; break;

                case 2: size = 9f; name = "Large"; break;

                case 3: size = 11f; name = "Very Large"; break;

                default: throw new NotImplementedException();
                }
                MenuItem item = new MenuItem(name);
                item.Checked = fontSize == size;
                item.Click  += delegate(object sender, EventArgs e) {
                    SetFontSize(size);
                    settings.SetValue("sensorGadget.FontSize", size);
                    foreach (MenuItem mi in fontSizeMenu.MenuItems)
                    {
                        mi.Checked = mi == item;
                    }
                };
                fontSizeMenu.MenuItems.Add(item);
            }
            contextMenu.MenuItems.Add(fontSizeMenu);
            MenuItem percentageMenu = new MenuItem("Percentage display");

            for (int i = 0; i < 3; i++)
            {
                PercentageDisplayType type = (PercentageDisplayType)i;
                string name;
                switch (type)
                {
                case PercentageDisplayType.ProgressBar: name = "Progress bar"; break;

                case PercentageDisplayType.Value: name = "Numeric value"; break;

                case PercentageDisplayType.Both: name = "Both"; break;

                default: throw new NotImplementedException();
                }
                MenuItem item = new MenuItem(name);
                item.Checked = percentageDisplayType == type;
                item.Click  += delegate(object sender, EventArgs e) {
                    percentageDisplayType = type;
                    settings.SetValue("sensorGadget.PercentageDisplayType", (int)type);
                    foreach (MenuItem mi in percentageMenu.MenuItems)
                    {
                        mi.Checked = mi == item;
                    }
                };
                percentageMenu.MenuItems.Add(item);
            }
            contextMenu.MenuItems.Add(percentageMenu);
            contextMenu.MenuItems.Add(new MenuItem("-"));
            MenuItem lockItem = new MenuItem("Lock Position and Size");

            contextMenu.MenuItems.Add(lockItem);
            contextMenu.MenuItems.Add(new MenuItem("-"));
            MenuItem alwaysOnTopItem = new MenuItem("Always on Top");

            contextMenu.MenuItems.Add(alwaysOnTopItem);
            MenuItem opacityMenu = new MenuItem("Opacity");

            contextMenu.MenuItems.Add(opacityMenu);
            Opacity = (byte)settings.GetValue("sensorGadget.Opacity", 255);
            for (int i = 0; i < 5; i++)
            {
                MenuItem item = new MenuItem((20 * (i + 1)).ToString() + " %");
                byte     o    = (byte)(51 * (i + 1));
                item.Checked = Opacity == o;
                item.Click  += delegate(object sender, EventArgs e) {
                    Opacity = o;
                    settings.SetValue("sensorGadget.Opacity", Opacity);
                    foreach (MenuItem mi in opacityMenu.MenuItems)
                    {
                        mi.Checked = mi == item;
                    }
                };
                opacityMenu.MenuItems.Add(item);
            }
            this.ContextMenu = contextMenu;

            hardwareNames = new UserOption("sensorGadget.Hardwarenames", true,
                                           hardwareNamesItem, settings);
            hardwareNames.Changed += delegate(object sender, EventArgs e) {
                Resize();
            };

            alwaysOnTop = new UserOption("sensorGadget.AlwaysOnTop", false,
                                         alwaysOnTopItem, settings);
            alwaysOnTop.Changed += delegate(object sender, EventArgs e) {
                this.AlwaysOnTop = alwaysOnTop.Value;
            };
            lockPositionAndSize = new UserOption("sensorGadget.LockPositionAndSize",
                                                 false, lockItem, settings);
            lockPositionAndSize.Changed += delegate(object sender, EventArgs e) {
                this.LockPositionAndSize = lockPositionAndSize.Value;
            };

            HitTest += delegate(object sender, HitTestEventArgs e) {
                if (lockPositionAndSize.Value)
                {
                    return;
                }

                if (e.Location.X < leftBorder)
                {
                    e.HitResult = HitResult.Left;
                    return;
                }
                if (e.Location.X > Size.Width - 1 - rightBorder)
                {
                    e.HitResult = HitResult.Right;
                    return;
                }
            };

            SizeChanged += delegate(object sender, EventArgs e) {
                settings.SetValue("sensorGadget.Width", Size.Width);
                Redraw();
            };

            VisibleChanged += delegate(object sender, EventArgs e) {
                Rectangle bounds       = new Rectangle(Location, Size);
                Screen    screen       = Screen.FromRectangle(bounds);
                Rectangle intersection =
                    Rectangle.Intersect(screen.WorkingArea, bounds);
                if (intersection.Width < Math.Min(16, bounds.Width) ||
                    intersection.Height < Math.Min(16, bounds.Height))
                {
                    Location = new Point(
                        screen.WorkingArea.Width / 2 - bounds.Width / 2,
                        screen.WorkingArea.Height / 2 - bounds.Height / 2);
                }
            };

            MouseDoubleClick += delegate(object obj, MouseEventArgs args) {
                SendHideShowCommand();
            };
        }
Пример #12
0
        public MainForm()
        {
            InitializeComponent();

            this.settings = new PersistentSettings();
            this.settings.Load(Path.ChangeExtension(
                                   Application.ExecutablePath, ".config"));

            this.unitManager = new UnitManager(settings);

            // set the DockStyle here, to avoid conflicts with the MainMenu
            this.splitContainer.Dock = DockStyle.Fill;

            this.Font      = SystemFonts.MessageBoxFont;
            treeView.Font  = SystemFonts.MessageBoxFont;
            plotPanel.Font = SystemFonts.MessageBoxFont;

            nodeCheckBox.IsVisibleValueNeeded += nodeCheckBox_IsVisibleValueNeeded;
            nodeCheckBox.CheckStateChanged    += UpdatePlotSelection;
            nodeTextBoxText.DrawText          += nodeTextBoxText_DrawText;
            nodeTextBoxValue.DrawText         += nodeTextBoxText_DrawText;
            nodeTextBoxMin.DrawText           += nodeTextBoxText_DrawText;
            nodeTextBoxMax.DrawText           += nodeTextBoxText_DrawText;
            nodeTextBoxText.EditorShowing     += nodeTextBoxText_EditorShowing;

            foreach (TreeColumn column in treeView.Columns)
            {
                column.Width = Math.Max(20, Math.Min(400,
                                                     settings.GetValue("treeView.Columns." + column.Header + ".Width",
                                                                       column.Width)));
            }

            treeModel  = new TreeModel();
            root       = new Node(System.Environment.MachineName);
            root.Image = Utilities.EmbeddedResources.GetImage("computer.png");

            treeModel.Nodes.Add(root);
            treeView.Model = treeModel;

            this.computer = new Computer(settings);

            systemTray = new SystemTray(computer, settings);
            systemTray.HideShowCommand += hideShowClick;
            systemTray.ExitCommand     += exitClick;

            int p = (int)Environment.OSVersion.Platform;

            if ((p == 4) || (p == 128)) // Unix
            {
                treeView.RowHeight           = Math.Max(treeView.RowHeight, 17);
                splitContainer.BorderStyle   = BorderStyle.None;
                splitContainer.Border3DStyle = Border3DStyle.Adjust;
                splitContainer.SplitterWidth = 4;
                treeView.BorderStyle         = BorderStyle.Fixed3D;
                plotPanel.BorderStyle        = BorderStyle.Fixed3D;
                gadgetMenuItem.Visible       = false;
                minCloseMenuItem.Visible     = false;
                minTrayMenuItem.Visible      = false;
                startMinMenuItem.Visible     = false;
            }
            else // Windows
            {
                treeView.RowHeight = Math.Max(treeView.Font.Height + 1, 17);

                gadget = new SensorGadget(computer, settings, unitManager);
                gadget.HideShowCommand += hideShowClick;

                wmiProvider = new WmiProvider(computer);
            }

            computer.HardwareAdded   += new HardwareEventHandler(HardwareAdded);
            computer.HardwareRemoved += new HardwareEventHandler(HardwareRemoved);

            computer.Open();

            timer.Enabled = true;

            plotColorPalette     = new Color[13];
            plotColorPalette[0]  = Color.Blue;
            plotColorPalette[1]  = Color.OrangeRed;
            plotColorPalette[2]  = Color.Green;
            plotColorPalette[3]  = Color.LightSeaGreen;
            plotColorPalette[4]  = Color.Goldenrod;
            plotColorPalette[5]  = Color.DarkViolet;
            plotColorPalette[6]  = Color.YellowGreen;
            plotColorPalette[7]  = Color.SaddleBrown;
            plotColorPalette[8]  = Color.RoyalBlue;
            plotColorPalette[9]  = Color.DeepPink;
            plotColorPalette[10] = Color.MediumSeaGreen;
            plotColorPalette[11] = Color.Olive;
            plotColorPalette[12] = Color.Firebrick;

            showHiddenSensors = new UserOption("hiddenMenuItem", false,
                                               hiddenMenuItem, settings);
            showHiddenSensors.Changed += delegate(object sender, EventArgs e) {
                treeModel.ForceVisible = showHiddenSensors.Value;
            };

            showPlot          = new UserOption("plotMenuItem", false, plotMenuItem, settings);
            showPlot.Changed += delegate(object sender, EventArgs e) {
                splitContainer.Panel2Collapsed = !showPlot.Value;
                treeView.Invalidate();
            };

            showValue = new UserOption("valueMenuItem", true, valueMenuItem,
                                       settings);
            showValue.Changed += delegate(object sender, EventArgs e) {
                treeView.Columns[1].IsVisible = showValue.Value;
            };

            showMin          = new UserOption("minMenuItem", false, minMenuItem, settings);
            showMin.Changed += delegate(object sender, EventArgs e) {
                treeView.Columns[2].IsVisible = showMin.Value;
            };

            showMax          = new UserOption("maxMenuItem", true, maxMenuItem, settings);
            showMax.Changed += delegate(object sender, EventArgs e) {
                treeView.Columns[3].IsVisible = showMax.Value;
            };

            startMinimized = new UserOption("startMinMenuItem", false,
                                            startMinMenuItem, settings);

            minimizeToTray = new UserOption("minTrayMenuItem", true,
                                            minTrayMenuItem, settings);
            minimizeToTray.Changed += delegate(object sender, EventArgs e) {
                systemTray.IsMainIconEnabled = minimizeToTray.Value;
            };

            minimizeOnClose = new UserOption("minCloseMenuItem", false,
                                             minCloseMenuItem, settings);

            autoStart = new UserOption(null, startupManager.Startup,
                                       startupMenuItem, settings);
            autoStart.Changed += delegate(object sender, EventArgs e) {
                try {
                    startupManager.Startup = autoStart.Value;
                } catch (InvalidOperationException) {
                    MessageBox.Show("Updating the auto-startup option failed.", "Error",
                                    MessageBoxButtons.OK, MessageBoxIcon.Error);
                    autoStart.Value = startupManager.Startup;
                }
            };

            readHddSensors = new UserOption("hddMenuItem", true, hddMenuItem,
                                            settings);
            readHddSensors.Changed += delegate(object sender, EventArgs e) {
                computer.HDDEnabled = readHddSensors.Value;
                UpdatePlotSelection(null, null);
            };

            showGadget = new UserOption("gadgetMenuItem", false, gadgetMenuItem,
                                        settings);
            showGadget.Changed += delegate(object sender, EventArgs e) {
                if (gadget != null)
                {
                    gadget.Visible = showGadget.Value;
                }
            };

            celciusMenuItem.Checked =
                unitManager.TemperatureUnit == TemperatureUnit.Celcius;
            fahrenheitMenuItem.Checked = !celciusMenuItem.Checked;

            startupMenuItem.Visible = startupManager.IsAvailable;

            if (startMinMenuItem.Checked)
            {
                if (!minTrayMenuItem.Checked)
                {
                    WindowState = FormWindowState.Minimized;
                    Show();
                }
            }
            else
            {
                Show();
            }

            // Create a handle, otherwise calling Close() does not fire FormClosed
            IntPtr handle = Handle;

            // Make sure the settings are saved when the user logs off
            Microsoft.Win32.SystemEvents.SessionEnded += delegate {
                SaveConfiguration();
            };
        }