Visual C++ .Net Controls: Image Lists


Introduction

An image list is a group of images intended to solve a common issue. Such images can be used to represent buttons of a toolbar or items of list-based control, etc.

  1. Start Microsoft Visual Studio .NET
  2. On the Start Page, click New Project (alternatively, on the main menu, you can click File -> New -> Project...)
  3. On the New Project dialog box, in the Project Types tree list, click Visual C++ Projects
  4. In the Templates list, click Managed C++ Empty Project
  5. In the Name edit box, replace the <Enter name> content with ImageList and Nothing Else
  6. In the Location combo box, accept the suggestion or type your own. If you don't have any, type C:\Programs\MSVC.NET
  7. Click OK
  8. On the main menu, click Project -> Add New Item...
  9. In the Add New Item dialog box, in the Templates list, click C++ File
  10. In the Name box, replace <Enter name> with Main and click OK
  11. Replace the contents of the empty file with the following:
     
    #using <mscorlib.dll>
    #using <System.dll>
    #using <System.Windows.Forms.dll>
    
    using namespace System;
    using namespace System::Windows::Forms;
    
    __gc class SimpleForm : public Form
    {
    public:
    	SimpleForm();
    };
    
    SimpleForm::SimpleForm()
    {
    	// The caption of the form
    	this->Text = S"Image Lists and Nothing Else";
    }
    
    int __stdcall WinMain()
    {
    	SimpleForm *SF = new SimpleForm();
    	Application::Run(SF);
    
    	return 0;
    }
  12. Test the application and return to MSVC

Creating an Image List

To create an image list, you can use the ImageList control. Each image is added to a list. Once the list is created, the group is treated as a pseudo-control but usually, each image can still be accessed as its own object.

The following exercise only shows how to create an image list. It doesn't actually use the image list.
  1. Using the lesson on Bitmap Design, design the following three bitmaps and save them respectively as Belgium, Diamond, and Feather:
     
  2. To create an image list, change the file as follows:
     
    #using <mscorlib.dll>
    #using <System.dll>
    #using <System.Drawing.dll>
    #using <System.Windows.Forms.dll>
    
    using namespace System;
    using namespace System::Drawing;
    using namespace System::Windows::Forms;
    
    __gc class SimpleForm : public Form
    {
    public:
    	SimpleForm();
    private:
    	// Declare a pointer to the ImageList class
    	ImageList	*icoImages;
    };
    
    SimpleForm::SimpleForm()
    {
    	// The caption of the form
    	this->Text = S"Image Lists and Nothing Else";
    	
    	// Use the declared pointer to initialize the ImageList object
    	icoImages = new ImageList;
    	// Add each image, as a bitmap or an icon, to the list of images
    	icoImages->Images->Add(Image::FromFile("Belgium.bmp"));
    	icoImages->Images->Add(Image::FromFile("Diamond.bmp"));
    	icoImages->Images->Add(Image::FromFile("Feather.bmp"));
    }
    
    int __stdcall WinMain()
    {
    	SimpleForm *SF = new SimpleForm();
    	Application::Run(SF);
    
    	return 0;
    }
  3. Save the application
 

Home Copyright © 2002 FunctionX, Inc.