Adding items to the list control is quite simple if we are not writing a time-critical application. If there is a great number of thumbnails to load, the user will see just a moving scroll bar flashing on the screen while the images are being loaded from disk. Instead we can use a simple thread mechanism to add the list items while another thread just loads the images, and all this while the user continues to interact with the application.
The member function that loads items into the list control will be something like:
Collapse
// structure used to pass parameters to a image adder thread
struct threadParamImage
{
CString folder; // the folder to be scanned for thumbnails
CThumbnailView* pView; // the view that accomodates them
HANDLE readPipe; // the pipe used to pass thumbnail filenames to the
// JPEG loader thread
};
// structure used to pass parameters to a JPEG image loader thread
struct threadParam
{
CThumbnailView* pView; // the view that shows thumbnails
HANDLE readPipe; // the pipe used to pass thumbnail filenames to the
// JPEG loader thread
};
HANDLE hThread = NULL; // handle to the JPEG image loader thread
HANDLE readPipe = NULL; // read and write ends of the communication pipe
HANDLE writePipe = NULL;
HANDLE skipImages = NULL; // handle to the semaphore that signals the pipe
//does no longer hold consistent data
HANDLE imageFiller = NULL; // handle to the thumbnail adder thread
HANDLE imageFillerSemaphore = NULL; // thread termination flag (when this semaphore
The code uses SDK semaphores, pipes and threads because they are easier to handle and much straightforward than MFC threads and synchronization mechanisms.
The code is quite easy to follow and change to meet your needs, but if you need assistance, contact me. Also please send me bugs or updates, to keep this solution up-to-date. For more details on the sample application, contact me.
The solution presented in this article uses a JPEG reader class to read JPEG images and display them into a CListCtrl. The idea is to create an image list that holds the icons created from the JPEG thumbails.
The first problem arises with the fact that MFC class CImageList does not support higher color depths than 16 colors (4 bits per pixel). Another interesting issue is that image loading takes quite some time. This article addresses both these issues.
1. Creating an image list with higher color depth.
The less-known SDK macro ImageList_Create meets this problem.
// Create the image list with 100*100 icons and 32 bpp color depth