Click here to Skip to main content
15,886,362 members
Articles / Desktop Programming / MFC
Article

Thumbnail Picture Control

Rate me:
Please Sign up or sign in to vote.
4.70/5 (17 votes)
23 Oct 20022 min read 167.3K   7.6K   53   33
A MFC thumbnail picture control

Sample Image - ThumbNailCtrl.png

Introduction

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.

Background

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. <p<img src="/KB/miscctrl/thumbnailctrl/step1.png" alt="Step 1" width="678" height="487">
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
  {
  private:
    <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)
{
  CDialog::DoDataExchange(pDX);
  //{{AFX_DATA_MAP(CThumbNailDlg)
  DDX_Text(pDX, IDC_MSG, m_sMsg);
  //}}AFX_DATA_MAP
  <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()
{
  CDialog::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...

BEGIN_MESSAGE_MAP(CThumbNailDlg, CDialog)
  //{{AFX_MSG_MAP(CThumbNailDlg)
  ON_WM_SYSCOMMAND()
  ON_WM_PAINT()
  ON_WM_QUERYDRAGICON()
  //}}AFX_MSG_MAP
  ON_REGISTERED_MESSAGE( UWM_ON_TNB_LCLICKED, OnTnbLClicked )
  ON_REGISTERED_MESSAGE( UWM_ON_TNB_RCLICKED, OnTnbRClicked )
END_MESSAGE_MAP()

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.

Comments

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

History

2002/10/25 Version 1.0 First relese.

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
Hong Kong Hong Kong
Software developer in realtime, hi-freq trading app.

Comments and Discussions

 
QuestionOpen Source Licensing Pin
Shimon Shore3-Jun-21 23:35
Shimon Shore3-Jun-21 23:35 
GeneralNot pass the Mouse Left/Right button click message to it's Parent window. Pin
gawadepd22-Mar-11 2:04
gawadepd22-Mar-11 2:04 
Hi All,

The mouse Left/Right button click message not pass to it's Parent window from CThumbnailButton.

Please help me if you can,

I have a class which have derived from CFormView where the custom ''Thumbnail' control is placed. I have added the code as per the suggestion given in Article as follows:


<=============================================================================================>
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...

BEGIN_MESSAGE_MAP(CThumbNailDlg, CFormView)
//{{AFX_MSG_MAP(CThumbNailDlg)

//}}AFX_MSG_MAP

ON_REGISTERED_MESSAGE( UWM_ON_TNB_LCLICKED, OnTnbLClicked )
ON_REGISTERED_MESSAGE( UWM_ON_TNB_RCLICKED, OnTnbRClicked )
END_MESSAGE_MAP()

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 */ )

<=============================================================================================>


I have not received any message from CThumbnailButton in CThumbNailDlg class.

Regards,
Pravin
GeneralWant Jpg, Gif and other formats, here is the code.... Pin
Habeeballah Hasnoddin18-Jul-08 23:36
Habeeballah Hasnoddin18-Jul-08 23:36 
GeneralCustom Control, List Control and DoModal Pin
stanleyhxywork8-Mar-07 19:42
stanleyhxywork8-Mar-07 19:42 
GeneralRe: Custom Control, List Control and DoModal Pin
kp200320-Oct-07 20:17
kp200320-Oct-07 20:17 
GeneralOnMouseWheel() not active Pin
safety_ruk8-Aug-06 21:54
safety_ruk8-Aug-06 21:54 
QuestionIs it possible to resize the preview size at runtime ? Pin
Mehta Jigar13-Apr-06 11:08
Mehta Jigar13-Apr-06 11:08 
GeneralRemove image Pin
Larsson28-Oct-04 1:16
Larsson28-Oct-04 1:16 
GeneralRefresh Pin
L$D12-Aug-04 6:12
L$D12-Aug-04 6:12 
GeneralRe: Refresh Pin
leven31-Mar-06 8:22
leven31-Mar-06 8:22 
QuestionCan this code be modified to display JPEQ file? Pin
ashleynguyen20063-May-04 7:45
ashleynguyen20063-May-04 7:45 
AnswerRe: Can this code be modified to display JPEQ file? Pin
ashleynguyen20063-May-04 11:15
ashleynguyen20063-May-04 11:15 
GeneralRe: Can this code be modified to display JPEQ file? Pin
Rex Fong3-May-04 14:45
Rex Fong3-May-04 14:45 
GeneralI can't download patched source code ! Pin
sigtrans6-Feb-04 15:41
sigtrans6-Feb-04 15:41 
GeneralRe: I can't download patched source code ! Pin
Rex Fong6-Feb-04 15:56
Rex Fong6-Feb-04 15:56 
GeneralEndless loop Pin
netflup17-Sep-03 2:57
netflup17-Sep-03 2:57 
GeneralRe: Endless loop Pin
Rex Fong17-Sep-03 15:02
Rex Fong17-Sep-03 15:02 
GeneralUse with CFormView, horizontal scrolling, Pin
ManojP10-Apr-03 23:50
ManojP10-Apr-03 23:50 
GeneralRe: Use with CFormView, horizontal scrolling, Pin
gawadepd22-Mar-11 3:18
gawadepd22-Mar-11 3:18 
GeneralSorting The Images By Drag and Drop Pin
pavankvk30-Mar-03 17:31
pavankvk30-Mar-03 17:31 
GeneralRe: Sorting The Images By Drag and Drop Pin
Rex Fong30-Mar-03 21:41
Rex Fong30-Mar-03 21:41 
QuestionNeed some help ... how can i clear the control? Pin
E310-Dec-02 3:48
E310-Dec-02 3:48 
GeneralThanks goto it working Pin
E37-Dec-02 9:33
E37-Dec-02 9:33 
GeneralRe: Thanks goto it working Pin
ManojP10-Apr-03 23:46
ManojP10-Apr-03 23:46 
Questionvery nice! what about list control and thumbnails? Pin
hvibh7-Dec-02 2:11
hvibh7-Dec-02 2:11 

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.