Click here to Skip to main content
15,881,380 members
Articles / Programming Languages / C#

Enumerate and Host Control Panel Applets using C#.

Rate me:
Please Sign up or sign in to vote.
4.90/5 (18 votes)
15 Feb 200424 min read 98.8K   2.5K   56  
Demonstrates how to enumerate and host Windows Control Panel Applets using C# and unmanaged C++.
using System;
using System.IO;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using AppletEngine;

namespace AppletEngine
{
	/// <summary>
	/// Summary description for AppletViewerWindow.
	/// </summary>
	public class AppletViewerWindow : System.Windows.Forms.Form
	{
		private AppletEngine _appletEngine;
		private ArrayList _appletLibraries;
		private System.Windows.Forms.ColumnHeader columnHeader1;
		private System.Windows.Forms.ColumnHeader columnHeader2;
		private System.Windows.Forms.ListView listView;
		private System.Windows.Forms.ImageList smallImageList;
		private System.Windows.Forms.ImageList largeImageList;
		private System.Windows.Forms.MainMenu mainMenu1;
		private System.Windows.Forms.MenuItem menuItemView;
		private System.ComponentModel.IContainer components;

		public AppletViewerWindow()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();
			
			this.Closed += new EventHandler(AppletViewerWindow_Closed);

			this.LoadViewMenuItems();
			this.CheckMenuItemAccordingToView(this.listView.View);
			this.LoadApplets();						
			this.DisplayApplets();			
		}

		/// <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.components = new System.ComponentModel.Container();
			this.listView = new System.Windows.Forms.ListView();
			this.columnHeader1 = new System.Windows.Forms.ColumnHeader();
			this.columnHeader2 = new System.Windows.Forms.ColumnHeader();
			this.largeImageList = new System.Windows.Forms.ImageList(this.components);
			this.smallImageList = new System.Windows.Forms.ImageList(this.components);
			this.mainMenu1 = new System.Windows.Forms.MainMenu();
			this.menuItemView = new System.Windows.Forms.MenuItem();
			this.SuspendLayout();
			// 
			// listView
			// 
			this.listView.BackColor = System.Drawing.Color.SteelBlue;
			this.listView.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {
																					   this.columnHeader1,
																					   this.columnHeader2});
			this.listView.Dock = System.Windows.Forms.DockStyle.Fill;
			this.listView.ForeColor = System.Drawing.Color.White;
			this.listView.FullRowSelect = true;
			this.listView.LargeImageList = this.largeImageList;
			this.listView.Location = new System.Drawing.Point(0, 0);
			this.listView.Name = "listView";
			this.listView.Size = new System.Drawing.Size(592, 366);
			this.listView.SmallImageList = this.smallImageList;
			this.listView.Sorting = System.Windows.Forms.SortOrder.Ascending;
			this.listView.TabIndex = 0;
			this.listView.DoubleClick += new System.EventHandler(this.listView1_DoubleClick);
			this.listView.SelectedIndexChanged += new System.EventHandler(this.listView1_SelectedIndexChanged);
			// 
			// columnHeader1
			// 
			this.columnHeader1.Text = "Name";
			this.columnHeader1.Width = 100;
			// 
			// columnHeader2
			// 
			this.columnHeader2.Text = "Description";
			this.columnHeader2.Width = 403;
			// 
			// largeImageList
			// 
			this.largeImageList.ColorDepth = System.Windows.Forms.ColorDepth.Depth32Bit;
			this.largeImageList.ImageSize = new System.Drawing.Size(32, 32);
			this.largeImageList.TransparentColor = System.Drawing.Color.Magenta;
			// 
			// smallImageList
			// 
			this.smallImageList.ColorDepth = System.Windows.Forms.ColorDepth.Depth32Bit;
			this.smallImageList.ImageSize = new System.Drawing.Size(16, 16);
			this.smallImageList.TransparentColor = System.Drawing.Color.Magenta;
			// 
			// mainMenu1
			// 
			this.mainMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
																					  this.menuItemView});
			// 
			// menuItemView
			// 
			this.menuItemView.Index = 0;
			this.menuItemView.Text = "View";
			// 
			// AppletViewerWindow
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(592, 366);
			this.Controls.Add(this.listView);
			this.Menu = this.mainMenu1;
			this.Name = "AppletViewerWindow";
			this.Text = "Available Control Panel Applets";
			this.ResumeLayout(false);

		}
		#endregion

		private void DisplayApplets()
		{
			foreach(AppletLibrary appletLibrary in _appletLibraries)
			{
				foreach(Applet applet in appletLibrary.Applets)
				{
					ListViewItem item = new ListViewItem();
					item.Text = applet.Name;
					item.SubItems.Add(applet.Description);
					item.Tag = applet;					
					
					item.ImageIndex = this.smallImageList.Images.Count;

					if (applet.SmallImage != null)
					{						
						this.smallImageList.Images.Add(applet.SmallImage);
					}

					if (applet.LargeImage != null)
					{
						this.largeImageList.Images.Add(applet.LargeImage);
					}

					this.listView.Items.Add(item);
				}
			}
		}

		private void AppletViewerWindow_Closed(object sender, EventArgs e)
		{
			foreach(AppletLibrary appletLibrary in _appletLibraries)
				appletLibrary.Dispose();
		}

		private void listView1_SelectedIndexChanged(object sender, System.EventArgs e)
		{
		
		}

		private void listView1_DoubleClick(object sender, System.EventArgs e)
		{
			try
			{
				ListViewItem item = this.listView.SelectedItems[0];
				if (item != null)
				{
					Applet applet = item.Tag as Applet;
					applet.Open();
				}
			}
			catch(System.Exception systemException)
			{
				System.Diagnostics.Trace.WriteLine(systemException);
			}
		}

		private void LoadApplets()
		{
			_appletEngine = new AppletEngine();
			FileInfo[] libraries = _appletEngine.FindAppletLibraries(System.Environment.SystemDirectory);
			_appletLibraries = _appletEngine.CreateApplets(libraries, this.Handle);
		}

		private void LoadViewMenuItems()
		{
			Array views = Enum.GetValues(typeof(System.Windows.Forms.View));
			foreach(System.Windows.Forms.View view in views)
			{
				MenuItem mi = new MenuItem(view.ToString(), new EventHandler(this.OnMenuItemClicked));
				mi.RadioCheck = true;
				this.menuItemView.MenuItems.Add(mi);			
			}
		}

		private void CheckMenuItemAccordingToView(View view)
		{
			foreach(MenuItem mi in this.menuItemView.MenuItems)
				if (mi.Text == view.ToString())
					mi.Checked = true;
				else
					mi.Checked = false;
		}

		private void OnMenuItemClicked(object sender, System.EventArgs e)
		{
			MenuItem mi = sender as MenuItem;
			if (mi != null)
			{
				Array views = Enum.GetValues(typeof(System.Windows.Forms.View));
				foreach(System.Windows.Forms.View view in views)
				{
					if (view.ToString() == mi.Text)
					{
						this.listView.View = view;
						this.CheckMenuItemAccordingToView(view);
					}
				}
			}		
						
		}


	}
}

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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Software Developer (Senior)
United States United States
Senior Application Developer specializing in Windows desktop and network development.

Professional Experience
- B.S. of Computer Science (Graduated 2001 - PSU)
- Senior Application Developer (8+ yrs)
- Microsoft Certified Professional

Primary Interests
- C#, C++, HTML, Javascript
- XML, ASP.NET, Web Services, SOAP, UDDI
- Socket programming and anything network related
- Reflection, Serialization, and Plugin Frameworks
- Owner-drawn controls and GDI+ goodness

Comments and Discussions