diff --git a/HyperVSwitch/MainForm.Designer.cs b/HyperVSwitch/MainForm.Designer.cs index 5d467c3..c245f5e 100644 --- a/HyperVSwitch/MainForm.Designer.cs +++ b/HyperVSwitch/MainForm.Designer.cs @@ -33,6 +33,8 @@ private void InitializeComponent() this.infoLabel = new System.Windows.Forms.LinkLabel(); this.label1 = new System.Windows.Forms.Label(); this.panel1 = new System.Windows.Forms.Panel(); + this.refreshButton = new System.Windows.Forms.Button(); + this.toggleButton = new System.Windows.Forms.Button(); this.panel1.SuspendLayout(); this.SuspendLayout(); // @@ -84,18 +86,45 @@ private void InitializeComponent() this.panel1.BackColor = System.Drawing.SystemColors.Control; this.panel1.Controls.Add(this.label1); this.panel1.Controls.Add(this.infoLabel); - this.panel1.Location = new System.Drawing.Point(0, 113); + this.panel1.Dock = System.Windows.Forms.DockStyle.Bottom; + this.panel1.Location = new System.Drawing.Point(0, 190); this.panel1.Margin = new System.Windows.Forms.Padding(0); this.panel1.Name = "panel1"; this.panel1.Size = new System.Drawing.Size(379, 44); this.panel1.TabIndex = 5; // + // refreshButton + // + this.refreshButton.FlatStyle = System.Windows.Forms.FlatStyle.System; + this.refreshButton.Location = new System.Drawing.Point(44, 135); + this.refreshButton.Margin = new System.Windows.Forms.Padding(35, 3, 35, 3); + this.refreshButton.Name = "refreshButton"; + this.refreshButton.Size = new System.Drawing.Size(291, 31); + this.refreshButton.TabIndex = 6; + this.refreshButton.Text = "refreshButton"; + this.refreshButton.UseVisualStyleBackColor = true; + this.refreshButton.Click += new System.EventHandler(this.refreshButton_Click); + // + // toggleButton + // + this.toggleButton.FlatStyle = System.Windows.Forms.FlatStyle.System; + this.toggleButton.Location = new System.Drawing.Point(44, 98); + this.toggleButton.Margin = new System.Windows.Forms.Padding(35, 3, 35, 3); + this.toggleButton.Name = "toggleButton"; + this.toggleButton.Size = new System.Drawing.Size(291, 31); + this.toggleButton.TabIndex = 7; + this.toggleButton.Text = "toggleButton"; + this.toggleButton.UseVisualStyleBackColor = true; + this.toggleButton.Click += new System.EventHandler(this.toggleButton_Click); + // // MainForm // this.AutoScaleDimensions = new System.Drawing.SizeF(96F, 96F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Dpi; this.BackColor = System.Drawing.SystemColors.Window; - this.ClientSize = new System.Drawing.Size(379, 157); + this.ClientSize = new System.Drawing.Size(379, 234); + this.Controls.Add(this.toggleButton); + this.Controls.Add(this.refreshButton); this.Controls.Add(this.panel1); this.Controls.Add(this.actionButton); this.Controls.Add(this.statusLabel); @@ -121,6 +150,8 @@ private void InitializeComponent() private System.Windows.Forms.LinkLabel infoLabel; private System.Windows.Forms.Label label1; private System.Windows.Forms.Panel panel1; + private System.Windows.Forms.Button refreshButton; + private System.Windows.Forms.Button toggleButton; } } diff --git a/HyperVSwitch/MainForm.cs b/HyperVSwitch/MainForm.cs index f0d418d..da02e89 100644 --- a/HyperVSwitch/MainForm.cs +++ b/HyperVSwitch/MainForm.cs @@ -27,6 +27,11 @@ public MainForm() statusLabel.Text = "Detecting current state…\nThis may take a few seconds."; statusLabel.ForeColor = Color.Gray; actionButton.Visible = false; + + toggleButton.Visible = false; + refreshButton.Visible = false; + refreshButton.Text = "Refresh current state"; + infoLabel.Text = "© 2016 Yves Goergen, GNU GPL v3"; } @@ -39,6 +44,9 @@ private async void MainForm_Load(object sender, EventArgs args) isHyperVActive = await GetHyperVStatus(); isHyperVRunning = GetHyperVRunning(); + refreshButton.Visible = true; + toggleButton.Visible = true; + actionButton.Visible = true; actionButton.Focus(); @@ -47,6 +55,7 @@ private async void MainForm_Load(object sender, EventArgs args) statusLabel.Text = "Hyper-V is ACTIVE."; statusLabel.ForeColor = Color.ForestGreen; actionButton.Text = "Deactivate Hyper-V and restart computer"; + toggleButton.Text = "Deactivate only (no restart)"; if (isHyperVRunning == false) { statusLabel.Text += " However, it is currently NOT running, so a restart may be pending."; @@ -59,6 +68,7 @@ private async void MainForm_Load(object sender, EventArgs args) statusLabel.Text = "Hyper-V is DEACTIVATED."; statusLabel.ForeColor = Color.Firebrick; actionButton.Text = "Activate Hyper-V and restart computer"; + toggleButton.Text = "Activate only (no restart)"; if (isHyperVRunning == true) { statusLabel.Text += " However, it is currently running, so a restart may be pending."; @@ -72,6 +82,8 @@ private async void MainForm_Load(object sender, EventArgs args) statusLabel.ForeColor = SystemColors.WindowText; actionButton.Text = "No action available"; actionButton.Enabled = false; + toggleButton.Visible = false; + toggleButton.Enabled = false; } } @@ -84,11 +96,13 @@ private void MainForm_KeyDown(object sender, KeyEventArgs args) if (args.KeyCode == Keys.F1 && args.Modifiers == 0) { string message = - "Hyper-V Switch allows you to enable or disable permanent virtualisation with Hyper-V without uninstalling it so that you can use Hyper-V and other virtualisation solutions like VMware or VirtualBox easily. This setting is stored in the boot configuration so that the computer must be restarted to apply the new setting.\n\n" + + "Hyper-V Switch allows you to enable or disable permanent virtualisation with Hyper-V without uninstalling it so that you can use Hyper-V " + + "and other virtualisation solutions like VMware or VirtualBox easily. This setting is stored in the boot configuration (bcdedit > hypervisorlaunchtype) " + + "so that the computer must be restarted to apply the new setting.\n\n" + "For more information please click on the link to open the website.\n\n" + "Available keyboard shortcuts:\n\n" + "Escape: Close program\n" + - "Shift+Click: Change state but skip restart (you need to restart manually)"; + "Shift+Click(" + actionButton.Text + "): Change state but skip restart (you need to restart manually)"; MessageBox.Show(message, "Info", MessageBoxButtons.OK, MessageBoxIcon.Information); } } @@ -101,6 +115,8 @@ private async void ActionButton_Click(object sender, EventArgs args) { bool shiftKeyPressed = ModifierKeys == Keys.Shift; actionButton.Enabled = false; + refreshButton.Enabled = false; + toggleButton.Enabled = false; try { if (!justRestart) @@ -146,6 +162,8 @@ private async void ActionButton_Click(object sender, EventArgs args) finally { actionButton.Enabled = true; + refreshButton.Enabled = true; + toggleButton.Enabled = true; } // System is restarted. Prevent further actions @@ -157,6 +175,54 @@ private void InfoLabel_LinkClicked(object sender, LinkLabelLinkClickedEventArgs Process.Start("http://unclassified.software/"); } + private void refreshButton_Click(object sender, EventArgs e) + { + actionButton.Enabled = false; + refreshButton.Enabled = false; + toggleButton.Enabled = false; + MainForm_Load(sender, e); + actionButton.Enabled = true; + refreshButton.Enabled = true; + toggleButton.Enabled = true; + } + + private async void toggleButton_Click(object sender, EventArgs e) + { + actionButton.Enabled = false; + refreshButton.Enabled = false; + toggleButton.Enabled = false; + try + { + if (isHyperVActive == true) + { + if (!await SetHyperVStatus(false)) + { + MessageBox.Show("Deactivating Hyper-V failed.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + } + else if (isHyperVActive == false) + { + if (!await SetHyperVStatus(true)) + { + MessageBox.Show("Activating Hyper-V failed.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + } + else + { + return; // Should not happen + } + } + finally + { + MainForm_Load(sender, e); + actionButton.Enabled = true; + refreshButton.Enabled = true; + toggleButton.Enabled = true; + } + } + #endregion Control event handlers #region Hyper-V support methods @@ -208,5 +274,6 @@ private async Task SetHyperVStatus(bool active) } #endregion Hyper-V support methods + } }