Click here to Skip to main content
13,291,084 members (66,677 online)
Click here to Skip to main content
Add your own
alternative version


51 bookmarked
Posted 23 Oct 2002

Thumbnail Picture Control

, 23 Oct 2002
Rate this:
Please Sign up or sign in to vote.
A MFC thumbnail picture control
<!-- Download Links --> <!-- Article image -->

Sample Image - ThumbNailCtrl.png

<!-- Add the rest of your HTML here -->


Recently, I was asked to write a thumbnail control to preview a certain numbers of pictures from a database. At the beginning, I was just lazy and want to get one over the net. After a few days, unluckily, I still got nothing. With my boss pressure, I finally have to write it once. Thank you to my boss if you find this piece of code is useful.


You are suggested to know a little bit about the use of custom control. I would recommend you to try the MFC Grid Control by Chris Maunder and his great articles at beginner level about "Creating Custom Controls".

Using the code

Although I have recommended you to have basic knowledge from previous articles, I will still provide step-by-step instructions to use the control.

Step 1: Create a Dialog resource with a custom control on it. <pStep 1
Type "CThumbNailControl" in class and "IDC_FRAME_THUMB" in ID

Step 2: Create a class and associate it to the preivous created dialog. Go to the header file of the class. Insert a include and a variable like below:

#include "ThumbNailControl.h"

class CThumbNailDlg : public CDialog
    <code>CThumbNailControl	m_cThumbFrame</code>;

Step 3: And then go to the cpp file to add the line "DDX_Control(pDX, IDC_FRAME_THUMB, m_cThumbFrame);"

void CThumbNailDlg::DoDataExchange(CDataExchange* pDX)
  DDX_Text(pDX, IDC_MSG, m_sMsg);
  <code>DDX_Control(pDX, IDC_FRAME_THUMB, m_cThumbFrame);</code>

Step 4: Now, go to OnInitDialog. An initialization is required here. The two integers are the width and heigh of a thumbnail button. You must call the "m_cThumbFrame.InitializeVariables" once. The default size is width=100 & height=75.

BOOL CThumbNailDlg::OnInitDialog()

  // TODO: Add extra initialization here
  m_cThumbFrame.InitializeVariables( 200, 150 );

  m_cThumbFrame.Add( "fish.bmp" );
  m_cThumbFrame.Add( "pinboard.bmp" );
  m_cThumbFrame.Add( "chess.bmp" );
  m_cThumbFrame.Add( "box.bmp" );
  return TRUE;  // return TRUE  unless you set the focus to a control

That's ALL.

Callback for L/R Mouse Clicks

Now, the control is ready to use. You only have to add a few more codes to response to the left and right mouse clicks (L/R MouseDown). In the header file of your dialog, add the following lines.

afx_msg LRESULT OnTnbLClicked( WPARAM wParam, LPARAM lParam );
afx_msg LRESULT OnTnbRClicked( WPARAM wParam, LPARAM lParam );

In the Message Map section of the dialog cpp file, add...


Now, you can add two functions to response to the 2 events.

LRESULT CThumbNailDlg::OnTnbLClicked( WPARAM wParam, LPARAM /* lParam  */ )
LRESULT CThumbNailDlg::OnTnbRClicked( WPARAM wParam, LPARAM /* lParam */ )

Some Known Issues

A smart reader like you may question for the supportted image formats. I am sorry that the current version could only support the bitmap file. Perhaps in the later version, I will add the code to support other formats. Interested readers could could refer to the article "Simple class for drawing pictures (jpg, tif, gif, etc...)" by Peter Hendrix or any other to integrate the codes together. I would be grateful if you spend the efforts to help me. ^_^

Another issue is about the calling of AfxMessageBox with mouse clicked action. You have to call ResetTrackFlag immediately after the popup of message box. I don't have time to deeply resolve it. Just write one more function to reset the flag. You may refer to and try modify the CThumbNailDlg::OnTnbRClicked in demo project to see the problem.


If you would like to give me comments and any suggestions, leave me a message or an email.


2002/10/25 Version 1.0 First relese.


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


About the Author

Rex Fong
Software Developer
Hong Kong Hong Kong
Software developer in realtime, hi-freq trading app.

You may also be interested in...


Comments and Discussions

GeneralNot pass the Mouse Left/Right button click message to it's Parent window. Pin
gawadepd22-Mar-11 3:04
membergawadepd22-Mar-11 3:04 
GeneralWant Jpg, Gif and other formats, here is the code.... Pin
Habeeballah Hasnoddin19-Jul-08 0:36
memberHabeeballah Hasnoddin19-Jul-08 0:36 
GeneralCustom Control, List Control and DoModal Pin
stanleyhxywork8-Mar-07 20:42
memberstanleyhxywork8-Mar-07 20:42 
Hi Rex,

Thank you for providing this thumbnail control class. It is very useful.
While I used your CThumbnailControl class and associated it with a List Control instead of Custom Control, the scroll bar (both vertical and horizontal) cannot be displayed. Can you shed some light on why it is so?

Another question is if I use a custom control on a dialog and call DoModal(), the dialog creation will fail. I have searched many forum but couldn't find a working solution. Do you have any idea how to fix this?

appreciate any help provided.



The man of tomorrow is shaped by his battles today.

GeneralRe: Custom Control, List Control and DoModal Pin
kp200320-Oct-07 21:17
memberkp200320-Oct-07 21:17 
GeneralOnMouseWheel() not active Pin
safety_ruk8-Aug-06 22:54
membersafety_ruk8-Aug-06 22:54 
QuestionIs it possible to resize the preview size at runtime ? Pin
Mehta Jigar13-Apr-06 12:08
memberMehta Jigar13-Apr-06 12:08 
GeneralRemove image Pin
Larsson28-Oct-04 2:16
memberLarsson28-Oct-04 2:16 
GeneralRefresh Pin
L$D12-Aug-04 7:12
memberL$D12-Aug-04 7:12 
GeneralRe: Refresh Pin
leven31-Mar-06 9:22
memberleven31-Mar-06 9:22 
QuestionCan this code be modified to display JPEQ file? Pin
techguyatwork3-May-04 8:45
membertechguyatwork3-May-04 8:45 
AnswerRe: Can this code be modified to display JPEQ file? Pin
techguyatwork3-May-04 12:15
membertechguyatwork3-May-04 12:15 
GeneralRe: Can this code be modified to display JPEQ file? Pin
Rex Fong3-May-04 15:45
memberRex Fong3-May-04 15:45 
GeneralI can't download patched source code ! Pin
sigtrans6-Feb-04 16:41
membersigtrans6-Feb-04 16:41 
GeneralRe: I can't download patched source code ! Pin
Rex Fong6-Feb-04 16:56
memberRex Fong6-Feb-04 16:56 
GeneralEndless loop Pin
Filip Geens17-Sep-03 3:57
memberFilip Geens17-Sep-03 3:57 
GeneralRe: Endless loop Pin
Rex Fong17-Sep-03 16:02
memberRex Fong17-Sep-03 16:02 
GeneralUse with CFormView, horizontal scrolling, Pin
ManojP11-Apr-03 0:50
memberManojP11-Apr-03 0:50 
GeneralRe: Use with CFormView, horizontal scrolling, Pin
gawadepd22-Mar-11 4:18
membergawadepd22-Mar-11 4:18 
GeneralSorting The Images By Drag and Drop Pin
pavankvk30-Mar-03 18:31
memberpavankvk30-Mar-03 18:31 
GeneralRe: Sorting The Images By Drag and Drop Pin
Rex Fong30-Mar-03 22:41
memberRex Fong30-Mar-03 22:41 
QuestionNeed some help ... how can i clear the control? Pin
E310-Dec-02 4:48
memberE310-Dec-02 4:48 
GeneralThanks goto it working Pin
E37-Dec-02 10:33
memberE37-Dec-02 10:33 
GeneralRe: Thanks goto it working Pin
ManojP11-Apr-03 0:46
memberManojP11-Apr-03 0:46 
Questionvery nice! what about list control and thumbnails? Pin
hvibh7-Dec-02 3:11
memberhvibh7-Dec-02 3:11 
GeneralPutting into SDI or MDI project Pin
E36-Dec-02 10:52
memberE36-Dec-02 10:52 
Generalvery nice Rex! Pin
Otyug26-Oct-02 14:27
memberOtyug26-Oct-02 14:27 
GeneralRe: very nice Rex! Pin
Rex Fong27-Oct-02 3:16
memberRex Fong27-Oct-02 3:16 
GeneralThis is a nice idea... Pin
Larry Antram24-Oct-02 14:11
memberLarry Antram24-Oct-02 14:11 
GeneralRe: This is a nice idea... Pin
Rex Fong24-Oct-02 15:43
memberRex Fong24-Oct-02 15:43 
GeneralThanks Pin
Pete Toms24-Oct-02 11:20
sussPete Toms24-Oct-02 11:20 
GeneralNice work Pin
mgama24-Oct-02 9:27
membermgama24-Oct-02 9:27 
GeneralRe: Nice work Pin
Rex Fong24-Oct-02 17:45
memberRex Fong24-Oct-02 17:45 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.171207.1 | Last Updated 24 Oct 2002
Article Copyright 2002 by Rex Fong
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid