Click here to Skip to main content
15,884,298 members
Articles / Programming Languages / C#

Custom ComboBoxes with Advanced Drop-down Features

Rate me:
Please Sign up or sign in to vote.
4.95/5 (55 votes)
22 Apr 2003CPOL4 min read 313.5K   12.1K   125  
Contains several ComboBoxes which uses Windows themes and contains ComboBoxes with CheckBoxed lists and TreeViews
using System;
using System.IO;
using System.Drawing;
using System.Reflection;
using System.Windows.Forms;
using System.Resources;

namespace UtilityLibrary.General
{
	public class ResourceUtil
	{
		public static Icon LoadIconStream(Type assemblyType, string iconName)
		{
			// Get the assembly that contains the bitmap resource
			Assembly myAssembly = Assembly.GetAssembly(assemblyType);

			// Get the resource stream containing the images
			Stream iconStream = myAssembly.GetManifestResourceStream(iconName);

			// Load the Icon from the stream
			return new Icon(iconStream);
		}

		public static Icon LoadIconStream(Type assemblyType, string iconName, Size iconSize)
		{
			// Load the entire Icon requested (may include several different Icon sizes)
			Icon rawIcon = LoadIconStream(assemblyType, iconName);
			
			// Create and return a new Icon that only contains the requested size
			return new Icon(rawIcon, iconSize); 
		}

		public static Bitmap LoadBitmapStream(Type assemblyType, string imageName)
		{
			return LoadBitmapStream(assemblyType, imageName, false, new Point(0,0));
		}

		public static Bitmap LoadBitmapStream(Type assemblyType, string imageName, Point transparentPixel)
		{
			return LoadBitmapStream(assemblyType, imageName, true, transparentPixel);
		}

		public static ImageList LoadImageListStream(Type assemblyType, 
												string imageName, 
												Size imageSize)
		{
			return LoadImageListStream(assemblyType, imageName, imageSize, false, new Point(0,0));
		}

		public static ImageList LoadImageListStream(Type assemblyType, 
												string imageName, 
												Size imageSize,
												Point transparentPixel)
		{
			return LoadImageListStream(assemblyType, imageName, imageSize, true, transparentPixel);
		}

		protected static Bitmap LoadBitmapStream(Type assemblyType, string imageName, 
										   bool makeTransparent, Point transparentPixel)
		{
			// Get the assembly that contains the bitmap resource
			Assembly myAssembly = Assembly.GetAssembly(assemblyType);

			// Get the resource stream containing the images
			Stream imageStream = myAssembly.GetManifestResourceStream(imageName);

			// Load the bitmap from stream
			Bitmap image = new Bitmap(imageStream);

			if (makeTransparent)
			{
				Color backColor = image.GetPixel(transparentPixel.X, transparentPixel.Y);
    
				// Make backColor transparent for Bitmap
				image.MakeTransparent(backColor);
			}
			    
			return image;
		}

		public static ImageList LoadImageListStream(Type assemblyType, 
												   string imageName, 
												   Size imageSize,
												   bool makeTransparent,
												   Point transparentPixel)
		{
			// Create storage for bitmap strip
			ImageList images = new ImageList();

			// Define the size of images we supply
			images.ImageSize = imageSize;

			// Get the assembly that contains the bitmap resource
			Assembly myAssembly = Assembly.GetAssembly(assemblyType);

			// Get the resource stream containing the images
			Stream imageStream = myAssembly.GetManifestResourceStream(imageName);

			// Load the bitmap strip from resource
			Bitmap pics = new Bitmap(imageStream);

			if (makeTransparent)
			{
				Color backColor = pics.GetPixel(transparentPixel.X, transparentPixel.Y);
    
				// Make backColor transparent for Bitmap
				pics.MakeTransparent(backColor);
			}
			    
			// Load them all !
			images.Images.AddStrip(pics);

			return images;
		}
        
		// The difference between the "LoadXStream" and "LoadXResource" functions is that
		// the load stream functions will load a embedded resource -- a file that you choose
		// to "embed as resource" while the load resource functions work with resource files
		// that have structure (.resX and .resources) and thus can hold several different 
		// resource items.

		public static Icon LoadIconResource(Type assemblyType, string resourceHolder, string imageName) 
		{
			// Get the assembly that contains the bitmap resource
			Assembly thisAssembly = Assembly.GetAssembly(assemblyType);
			ResourceManager rm = new ResourceManager(resourceHolder, thisAssembly);
			Icon icon = (Icon)rm.GetObject(imageName);
			return icon;
		}
		
		public static Bitmap LoadBitmapResource(Type assemblyType, string resourceHolder, string imageName) 
		{
			// Get the assembly that contains the bitmap resource
			Assembly thisAssembly = Assembly.GetAssembly(assemblyType);
			ResourceManager rm = new ResourceManager(resourceHolder, thisAssembly);
			Bitmap bitmap = (Bitmap)rm.GetObject(imageName);
			return bitmap;
		}


		public static ImageList LoadImageListResource(Type assemblyType, string resourceHolder,
																string imageName, Size imageSize)
		
		{
			return LoadImageListResource(assemblyType, resourceHolder, imageName, imageSize, false, new Point(0,0));
		}

		public static ImageList LoadImageListResource(Type assemblyType, string resourceHolder,
			string imageName, 
			Size imageSize,
			bool makeTransparent,
			Point transparentPixel)
		{
			// Create storage for bitmap strip
			ImageList images = new ImageList();

			// Define the size of images we supply
			images.ImageSize = imageSize;

			// Get the assembly that contains the bitmap resource
			Assembly thisAssembly = Assembly.GetAssembly(assemblyType);
			ResourceManager rm = new ResourceManager(resourceHolder, thisAssembly);
			Bitmap pics = (Bitmap)rm.GetObject(imageName);

			if (makeTransparent)
			{
				Color backColor = pics.GetPixel(transparentPixel.X, transparentPixel.Y);
    
				// Make backColor transparent for Bitmap
				pics.MakeTransparent(backColor);
			}
			    
			// Load the image
			images.Images.AddStrip(pics);

			return images;
		}




	}
}

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)


Written By
CEO ArtfulBits Inc.
Ukraine Ukraine
Name:Kucherenko Oleksandr

Born:September 20, 1979

Platforms: Win32, Linux; - well known and MS-DOS; Win16; OS/2 - old time not touched;

Hardware: IBM PC

Programming Languages: Assembler (for Intel 80386); Borland C/C++; Borland Pascal; Object Pascal; Borland C++Builder; Delphi; Perl; Java; Visual C++; Visual J++; UML; XML/XSL; C#; VB.NET; T-SQL; PL/SQL; and etc.

Development Environments: MS Visual Studio 2001-2008; MS Visual C++; Borland Delphi; Borland C++Builder; C/C++ any; Rational Rose; GDPro; Together and etc.

Libraries: STL, ATL, WTL, MFC, NuMega Driver Works, VCL; .NET 1.0, 1.1, 2.0, 3.5; and etc.

Technologies: Client/Server; COM; DirectX; DirectX Media; BDE; HTML/DHTML; ActiveX; Java Servlets; DCOM; COM+; ADO; CORBA; .NET; Windows Forms; GDI/GDI+; and etc.

Application Skills: Databases - design and maintain, support, programming; GUI Design; System Programming, Security; Business Software Development. Win/Web Services development and etc.

Comments and Discussions