When you insert a picture on a Dialog box the main aim of your dialog box is just not to display the Resource bitmaps that are added at compile time but to display the image from a file. Now to display a bitmap I could not find many good resources but if you google out with the keyword LoadPic.exe you can find some help with that sample but that is way too complicated for a simple image display and so I thought of all doing it with my shallow knowledge of Subclasing.
Steps
1. Add a picture Box to your application
2. Change the Type of the picture box to OWNER_DRAW. [Skip this step. Its not needed as corrected by lplover2k]
3. Add a variable to the picture box. By default it will be CStatic. I call it picPreview
4. Change the CStatic to CPictureBox. Don't forget to add the header files at the top.
5. Call the SetBitmap function to display the URL in the picture box.
picPreview.SetBitmap(CString m_sBitmap)
PictureBox.h
PictureBox.cpp
The attached zip file contains the above 2 header files
Edited the code to correct some errors regarding unnecessary include files. Thanks to lplover2k.
Steps
1. Add a picture Box to your application
2. Change the Type of the picture box to OWNER_DRAW. [Skip this step. Its not needed as corrected by lplover2k]
3. Add a variable to the picture box. By default it will be CStatic. I call it picPreview
4. Change the CStatic to CPictureBox. Don't forget to add the header files at the top.
5. Call the SetBitmap function to display the URL in the picture box.
picPreview.SetBitmap(CString m_sBitmap)
PictureBox.h
Code: CPP
#pragma once
// CPictureBox
class CPictureBox : public CStatic
{
DECLARE_DYNAMIC(CPictureBox)
public:
CPictureBox();
virtual ~CPictureBox();
void SetBitmap(CString strBitmap);
protected:
DECLARE_MESSAGE_MAP()
void ShowBitmap(CPaintDC *pDC);
CString m_sBitmap;
CBitmap m_bmpBitmap;
BITMAP bm;
public:
afx_msg void OnPaint();
};
Code: CPP
// PictureBox.cpp : implementation file
//
#include "stdafx.h"
#include "PictureBox.h"
#include ".\picturebox.h"
// CPictureBox
IMPLEMENT_DYNAMIC(CPictureBox, CStatic)
CPictureBox::CPictureBox()
{
}
CPictureBox::~CPictureBox()
{
}
BEGIN_MESSAGE_MAP(CPictureBox, CStatic)
ON_WM_PAINT()
END_MESSAGE_MAP()
// CPictureBox message handlers
void CPictureBox::ShowBitmap(CPaintDC *pdc)
{
//Create a device context to load the bitmap into
CDC dcMem;
dcMem.CreateCompatibleDC(pdc);
//Get the Display area available
CRect lRect;
GetClientRect(lRect);
lRect.NormalizeRect();
//select the bitmap into compatible device context
CBitmap* pOldBitmap = (CBitmap*)dcMem.SelectObject(&m_bmpBitmap);
//m_bmpBitmap.SetBitmapDimension(lRect.Width(),lRect.Height());
//copy & resize the window to the dialog window
pdc->StretchBlt(0,0,lRect.Width(),lRect.Height(),&dcMem,0,0,bm.bmWidth,bm.bmHeight,SRCCOPY);
}
void CPictureBox::OnPaint()
{
CPaintDC dc(this); // device context for painting
RECT rect;
GetClientRect(&rect);
dc.FillSolidRect(&rect, RGB(255,255,255));
if(m_sBitmap!="")
ShowBitmap(&dc);
}
void CPictureBox::SetBitmap(CString strBitmap)
{
m_sBitmap = strBitmap;
HBITMAP hBitmap = (HBITMAP) ::LoadImage(AfxGetInstanceHandle(),
m_sBitmap, IMAGE_BITMAP, 0, 0,
LR_LOADFROMFILE | LR_CREATEDIBSECTION);
// Do we have a valid handle for the loaded image?
if (hBitmap)
{
// Delete the current bitmap
if (m_bmpBitmap.DeleteObject())
m_bmpBitmap.Detach(); // If there was a bitmap, detach it
// Attach the currently loaded bitmap to the bitmap object
m_bmpBitmap.Attach(hBitmap);
}
m_bmpBitmap.GetBitmap(&bm); //Get Bitmap Structure
Invalidate();
}
Edited the code to correct some errors regarding unnecessary include files. Thanks to lplover2k.
fioguz
likes this

