Click here to Skip to main content
15,879,326 members
Articles / Desktop Programming / WTL
Article

Picture Box for WTL

Rate me:
Please Sign up or sign in to vote.
4.00/5 (9 votes)
4 Aug 2004CPOL 60K   2.5K   18   5
An article on how to use the Trilobyte-Solutions.nl CPictureBox control to load and display *.bmp, *.jpg, *.png and *.gif files.

Image 1

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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Netherlands Netherlands
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralNew location for the article downloads Pin
Trilobyte30-Apr-09 0:18
Trilobyte30-Apr-09 0:18 
QuestionHow can i use CScrollWindowImpl in WTL 8.0 Pin
kamrul04527-Feb-08 9:38
kamrul04527-Feb-08 9:38 
AnswerRe: How can i use CScrollWindowImpl in WTL 8.0 Pin
Paul Selormey1-Mar-09 23:07
Paul Selormey1-Mar-09 23:07 
GeneralDoom 3 rocks Pin
technomanceraus4-Aug-04 18:45
technomanceraus4-Aug-04 18:45 
GeneralRe: Doom 3 rocks Pin
Trilobyte5-Aug-04 8:58
Trilobyte5-Aug-04 8:58 

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.