Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Picture Box for WTL

0.00/5 (No votes)
4 Aug 2004 1  
An article on how to use the Trilobyte-Solutions.nl CPictureBox control to load and display *.bmp, *.jpg, *.png and *.gif files.

Introduction

This code is extremely useful if you want to display images. It loads *.bmp *.jpg *.png and *.gif files from disk or from resources included in your program. The control has scrollbar support and a build in context menu for easy usage which can be disabled.

Using the code

The following example assumes that you want the picture box to be in the client area of the main view.

//  CMainFrm.h

#include "PictureBox.h"


class CMainFrame:
{
public:
  // Other code here

  
  WTL::CPictureBox  m_PictureBox;
};


// CMainFrm.cpp

LRESULT CMainFrame::OnCreate(UINT /*uMsg*/, WPARAM /*wParam*/, 
    LPARAM /*lParam*/, BOOL& /*bHandled*/)
{
  // Some code

  
  // Create the Picture Box

  m_hwndClient = m_PictureBox.Create(m_hWnd, rcDefault, NULL,
    WS_VISIBLE|WS_CHILD|WS_CLIPCHILDREN|WS_CLIPSIBLINGS, WS_EX_CLIENTEDGE);
  
  // The control is created, now we can load a picture in it.

  // To load an image in the Picture Box use any of the functions below

  
  // Some more code

}
The create function of the CPictureBox works just like the create function of any CWindowImpl derived class. By default the control will position the bitmap in the center of the view, and it will use the build in context menu.

The CPictureBox functions

// Load an image from a file

// pszFileName is the path to the image

// the function returns true if succesfull, otherwise it returns false.

bool  LoadBitmapFromFile(LPCTSTR pszFileName);


// Load an image from a resource

// hInstance is the handle to the HINSTANCE you want to load from

// uiIDResource is the ID of the resource

// the function returns true if succesfull, otherwise it returns false.

bool  LoadBitmapFromID(HINSTANCE hInstance, UINT uiIDResource)


// Set the bitmap directly

// hBitmap is the Handle to the bitmap

// if  bOwner is true, the CPictureBox will 

// destroy the hBitamp when it is destroyed

// otherwise you have to clean up the hBitmap yourself

void  SetBitmap(HBITMAP hBitmap, bool bOwner = true)


// Detaches the bitmap from the CPictureBox and returns the bitmap

HBITMAP  GetBitmap()


// returns a save handle to the bitmap

HBITMAP  GetSaveBitmap()const


// if bCenter is true, the control will position 

// the bitmap in the center of the view

// otherwise the bitmap will be positioned in the 

// top left corner of the view

void  CenterPicture(bool bCenter)


// if bStretch is true, the control will stretct the 

// bitmap so that it fits the view

// otherwise the bitmap will be drawn normaly

void  StretchPicture(bool bStretch)


// if bUseMenu is true, the control will use the build in context menu

// otherwise the context menu will not be used

void  UseMenu(bool bUseMenu)

Points of Interest

I wrote this code because WTL did not have any classes that could display *.jpg, *.png, *.gif or had scrollbar support.

Additional Information

For additional information, questions and bug report, visit my website: http://www.Trilobyte-Solutions.nl, or contact me at bertwillems@trilobyte-solutions.nl.

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