Click here to Skip to main content
Click here to Skip to main content
Go to top

Picture Box for WTL

, 4 Aug 2004
Rate this:
Please Sign up or sign in to vote.
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 <span class="code-string">"PictureBox.h"
</span>

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)

Share

About the Author

Trilobyte
Technical Lead Premotion, Liones Internet BV
Netherlands Netherlands
I am Bert Willems a 24 year old developer living in Drachten, The Netherlands. I was born on 4 July 1985 in Drachten. My great passion is development of software, I simply love doing it. Since I was young I have been fascinated by technique and constructing things, first with Lego and later on things for the computer. I started with simple websites and some scripting of games, and then I decided I would make a career out of it and started with the development of business software. I am not sure why I love programming so much, but I know it has something to do with expressing my creativity in it.

Comments and Discussions

 
GeneralNew location for the article downloads PinmemberTrilobyte30-Apr-09 0:18 
QuestionHow can i use CScrollWindowImpl in WTL 8.0 Pinmemberkamrul04527-Feb-08 9:38 
AnswerRe: How can i use CScrollWindowImpl in WTL 8.0 PinmemberPaul Selormey1-Mar-09 23:07 
GeneralDoom 3 rocks Pinmembertechnomanceraus4-Aug-04 18:45 
GeneralRe: Doom 3 rocks PinmemberMr. TriloByte5-Aug-04 8:58 

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

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

| Advertise | Privacy | Mobile
Web03 | 2.8.140916.1 | Last Updated 5 Aug 2004
Article Copyright 2004 by Trilobyte
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid