Click here to Skip to main content
Click here to Skip to main content
Articles » Languages » C# » Windows Forms » Downloads
 
Add your own
alternative version

Window Hiding with C#

, 26 Mar 2012 CPOL
A Window Hider program that demonstrates many C# features
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;

namespace WindowHider
{
	/// <summary>
	/// Summary description for Settings.
	/// </summary>
	public class Settings : System.Windows.Forms.Form
	{
		private System.Windows.Forms.Button OkButton;
		private System.Windows.Forms.Label label1;
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;
		private System.Windows.Forms.TrackBar OpacityBar;
		private System.Windows.Forms.Label label3;
		private System.Windows.Forms.CheckedListBox ModifierList;
		private System.Windows.Forms.ComboBox ToggleCombo;
		private System.Windows.Forms.ComboBox MainCombo;
		private System.Windows.Forms.Panel panel1;
		private System.Windows.Forms.Label label2;
		private System.Windows.Forms.Label label4;

		public Settings()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();
			this.ModifierList.Validating += new CancelEventHandler(ValidateModifiers);

			foreach (KeyCodes.VirtualKey x in KeyCodes.VirtualKey.GetValues(typeof(KeyCodes.VirtualKey)))
			{
				MainCombo.Items.Add(x);
				ToggleCombo.Items.Add(x);
			}
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.OpacityBar = new System.Windows.Forms.TrackBar();
			this.ModifierList = new System.Windows.Forms.CheckedListBox();
			this.ToggleCombo = new System.Windows.Forms.ComboBox();
			this.MainCombo = new System.Windows.Forms.ComboBox();
			this.panel1 = new System.Windows.Forms.Panel();
			this.label2 = new System.Windows.Forms.Label();
			this.label4 = new System.Windows.Forms.Label();
			this.OkButton = new System.Windows.Forms.Button();
			this.label1 = new System.Windows.Forms.Label();
			this.label3 = new System.Windows.Forms.Label();
			((System.ComponentModel.ISupportInitialize)(this.OpacityBar)).BeginInit();
			this.panel1.SuspendLayout();
			this.SuspendLayout();
			// 
			// OpacityBar
			// 
			this.OpacityBar.Location = new System.Drawing.Point(16, 136);
			this.OpacityBar.Maximum = 100;
			this.OpacityBar.Name = "OpacityBar";
			this.OpacityBar.Size = new System.Drawing.Size(328, 45);
			this.OpacityBar.TabIndex = 0;
			this.OpacityBar.TickFrequency = 5;
			// 
			// ModifierList
			// 
			this.ModifierList.CheckOnClick = true;
			this.ModifierList.Items.AddRange(new object[] {
															  "Alt",
															  "Control",
															  "Shift",
															  "Windows Key"});
			this.ModifierList.Location = new System.Drawing.Point(16, 16);
			this.ModifierList.Name = "ModifierList";
			this.ModifierList.Size = new System.Drawing.Size(120, 64);
			this.ModifierList.TabIndex = 8;
			// 
			// ToggleCombo
			// 
			this.ToggleCombo.DropDownWidth = 40;
			this.ToggleCombo.Location = new System.Drawing.Point(144, 56);
			this.ToggleCombo.Name = "ToggleCombo";
			this.ToggleCombo.Size = new System.Drawing.Size(56, 21);
			this.ToggleCombo.TabIndex = 9;
			// 
			// MainCombo
			// 
			this.MainCombo.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
			this.MainCombo.DropDownWidth = 40;
			this.MainCombo.Location = new System.Drawing.Point(144, 24);
			this.MainCombo.Name = "MainCombo";
			this.MainCombo.Size = new System.Drawing.Size(56, 21);
			this.MainCombo.TabIndex = 9;
			// 
			// panel1
			// 
			this.panel1.Controls.AddRange(new System.Windows.Forms.Control[] {
																				 this.MainCombo,
																				 this.ModifierList,
																				 this.label2,
																				 this.ToggleCombo,
																				 this.label4});
			this.panel1.Location = new System.Drawing.Point(8, 16);
			this.panel1.Name = "panel1";
			this.panel1.Size = new System.Drawing.Size(344, 96);
			this.panel1.TabIndex = 9;
			// 
			// label2
			// 
			this.label2.Location = new System.Drawing.Point(200, 24);
			this.label2.Name = "label2";
			this.label2.Size = new System.Drawing.Size(96, 16);
			this.label2.TabIndex = 6;
			this.label2.Text = "hides main form.";
			this.label2.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
			// 
			// label4
			// 
			this.label4.Location = new System.Drawing.Point(200, 56);
			this.label4.Name = "label4";
			this.label4.Size = new System.Drawing.Size(144, 16);
			this.label4.TabIndex = 6;
			this.label4.Text = "hides all selected windows";
			this.label4.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
			// 
			// OkButton
			// 
			this.OkButton.DialogResult = System.Windows.Forms.DialogResult.OK;
			this.OkButton.Location = new System.Drawing.Point(272, 192);
			this.OkButton.Name = "OkButton";
			this.OkButton.TabIndex = 1;
			this.OkButton.Text = "Accept";
			// 
			// label1
			// 
			this.label1.Location = new System.Drawing.Point(24, 120);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(48, 16);
			this.label1.TabIndex = 2;
			this.label1.Text = "Opacity:";
			// 
			// label3
			// 
			this.label3.Location = new System.Drawing.Point(24, 16);
			this.label3.Name = "label3";
			this.label3.Size = new System.Drawing.Size(56, 16);
			this.label3.TabIndex = 6;
			this.label3.Text = "Hot Keys:";
			// 
			// Settings
			// 
			this.AcceptButton = this.OkButton;
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(368, 242);
			this.Controls.AddRange(new System.Windows.Forms.Control[] {
																		  this.label1,
																		  this.OkButton,
																		  this.OpacityBar,
																		  this.label3,
																		  this.panel1});
			this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedToolWindow;
			this.MaximizeBox = false;
			this.MinimizeBox = false;
			this.Name = "Settings";
			this.Text = "Settings";
			((System.ComponentModel.ISupportInitialize)(this.OpacityBar)).EndInit();
			this.panel1.ResumeLayout(false);
			this.ResumeLayout(false);

		}
		#endregion

		private void ValidateModifiers(object sender, CancelEventArgs e)
		{
			if (ModifierList.CheckedItems.Count == 0)
			{
				e.Cancel = true;
				MessageBox.Show("You must select at least one Hot Key Modifier","Hot Key Modifiers");
			}
		}
		public KeyCodes.VirtualKey HideKey
		{
			get
			{
				return (KeyCodes.VirtualKey)MainCombo.SelectedItem;
			}
			set
			{
				MainCombo.SelectedItem = value;
			}
		}
		public KeyCodes.VirtualKey ToggleKey
		{
			get
			{
				return (KeyCodes.VirtualKey)ToggleCombo.SelectedItem;
			}
			set
			{
				ToggleCombo.SelectedItem = value;
			}
		}
		new public double Opacity
		{
			get
			{
				return Convert.ToDouble(OpacityBar.Value)/100;
			}
			set
			{
				OpacityBar.Value = (int)(value*100);
				base.Opacity = value;
			}
		}

		public KeyCodes.Modifier Modifiers
		{
			get
			{
				KeyCodes.Modifier mods = 0;

				foreach(string modKey in ModifierList.CheckedItems)
				{
					switch(modKey)
					{
						case "Alt":
							mods = mods + (uint)KeyCodes.Modifier.MOD_ALT;
							break;
						case "Shift":
							mods = mods + (uint)KeyCodes.Modifier.MOD_SHIFT;
							break;
						case "Control":
							mods = mods + (uint)KeyCodes.Modifier.MOD_CONTROL;
							break;
						case "Windows Key":
							mods = mods + (uint)KeyCodes.Modifier.MOD_WIN;
							break;
					}
				}

				return mods;
			}
			set
			{
				uint x = (uint)value;
				while (x != 0)
				{
					if (x >= (uint)KeyCodes.Modifier.MOD_WIN)
					{
						this.ModifierList.SetItemChecked(3,true);
						x = x - (uint)KeyCodes.Modifier.MOD_WIN;
					}
					else if (x >= (uint)KeyCodes.Modifier.MOD_SHIFT)
					{
						this.ModifierList.SetItemChecked(2,true);
						x = x - (uint)KeyCodes.Modifier.MOD_SHIFT;
					}
					else if (x >= (uint)KeyCodes.Modifier.MOD_CONTROL)
					{
						this.ModifierList.SetItemChecked(1,true);
						x = x - (uint)KeyCodes.Modifier.MOD_CONTROL;
					}
					else if (x >= (uint)KeyCodes.Modifier.MOD_ALT)
					{
						this.ModifierList.SetItemChecked(0,true);
						x = x - (uint)KeyCodes.Modifier.MOD_ALT;
					}
				}
			}
		}
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Taylor Wood
Web Developer
United States United States
I write c#

| Advertise | Privacy | Mobile
Web01 | 2.8.141022.2 | Last Updated 26 Mar 2012
Article Copyright 2002 by Taylor Wood
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid