[C++]
For displaying an image in a control that is placed on a dialog window the following steps are necessary:
-Create a control what manages the image displaying:
-Subclass the CWnd class.
-Add a DISPLAYSTRUCT member variable to the class. Use this member for all BiDisp function calling.
-Create a CustomCreate function what can be used for creating the control. It is important to use proper window class what compatible with BiDisp (see in the sample code) otherwise the control will work wrong.
-Create SetDIB for setting the DIB what the control has to display.
-Handle the Create, Destroy, Paint, Size, VSroll, HScroll messages with the BiDisp functions.
(See the source code of the control below (ImageControl.h, ImageControl.cpp)):
-Create the control in the dialog
-Load an image and set in the control
Source of ImageControl.h:
#pragma once
#include "afxwin.h"
extern "C"
{
#include "BiDisp.h"
#include "BiDIB.h"
}
class CImageControl :
public CWnd
{
public:
// CustomCreate
// This function can be used for creating the control (do not use Create and CreateEx)
// Parameters:
// rect control position in the parent
// pParentWnd parent window
// nId control ID
BOOL CustomCreate(RECT rect, CWnd* pParentWnd, UINT nID);
// SetNewDIB
// This function displays the given DIB in the control
// Parameters:
// hDIB DIB to display in the control
BOOL SetDIB(HDIB hDIB);
protected:
// Display structure used by BiDisp for storing scaling, position, etc.
DISPLAYSTRUCT m_Disp;
public:
CImageControl(void);
~CImageControl(void);
DECLARE_MESSAGE_MAP()
afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
afx_msg void OnDestroy();
afx_msg void OnPaint();
afx_msg void OnSize(UINT nType, int cx, int cy);
afx_msg void OnVScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar);
afx_msg void OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar);
};
Source of ImageControl.cpp:
#include "stdafx.h"
#include "ImageControl.h"
CImageControl::CImageControl(void)
{
// Clear the display structure
ZeroMemory(&m_Disp, sizeof(m_Disp));
}
CImageControl::~CImageControl(void)
{
}
BEGIN_MESSAGE_MAP(CImageControl, CWnd)
ON_WM_CREATE()
ON_WM_DESTROY()
ON_WM_PAINT()
ON_WM_SIZE()
ON_WM_VSCROLL()
ON_WM_HSCROLL()
END_MESSAGE_MAP()
int CImageControl::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CWnd::OnCreate(lpCreateStruct) == -1)
return -1;
// Handle WM_CREATE by BiDisp
DisplayWmCreate(m_hWnd, 0, 0, &m_Disp);
return 0;
}
void CImageControl::OnDestroy()
{
CWnd::OnDestroy();
// Handle WM_DESTROY by BiDisp
DisplayWmDestroy(m_hWnd, 0, 0, &m_Disp);
}
void CImageControl::OnPaint()
{
// Handle WM_PAINT by BiDisp
DisplayWmPaint(m_hWnd, &m_Disp);
}
void CImageControl::OnSize(UINT nType, int cx, int cy)
{
CWnd::OnSize(nType, cx, cy);
// Handle WM_SIZE by BiDisp
DisplayWmSize(m_hWnd, nType, MAKELPARAM(cx, cy), &m_Disp);
}
void CImageControl::OnVScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
{
// Handle WM_VSCROLL by BiDisp
DisplayWmVertScroll(m_hWnd, MAKELPARAM(nSBCode, nPos), 0, &m_Disp);
}
void CImageControl::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
{
// Handle WM_HSCROLL by BiDisp
DisplayWmHorzScroll(m_hWnd, MAKELPARAM(nSBCode, nPos), 0, &m_Disp);
}
BOOL CImageControl::CustomCreate(RECT rect,CWnd* pParentWnd, UINT nID)
{
// Create a class what is compatible with BiDisp
CString szClassName = AfxRegisterWndClass(CS_HREDRAW | CS_VREDRAW, NULL, (HBRUSH)GetStockObject(WHITE_BRUSH), NULL);
// Create style
DWORD dwStyle = WS_CHILD | WS_VISIBLE | WS_BORDER;
// Create the windows with the compatible class
BOOL bRet = Create(szClassName, _T(""), dwStyle, rect, pParentWnd, nID);
return bRet;
}
BOOL CImageControl::SetDIB(HDIB hDIB)
{
// Set origin and scaling factor and clear the display structure
RECT rOrg;
RECT rScale;
SetRect(&rOrg, 0, 0, 0, 0);
SetRect(&rScale, 1, 1, 1, 1);
ZeroMemory(&m_Disp, sizeof(m_Disp));
// Set the new DIB in BiDisp
int iRet = DisplayDIBStart(m_hWnd, hDIB, &rOrg, &rScale, DISP_SCALED | DISP_NEWDIB, &m_Disp);
return (iRet != 0);
}
Create the control in the dialog
BOOL CDiplayImageInControlDlg::OnInitDialog()
{
CDialogEx::OnInitDialog();
// Set control properties
RECT rect; // Control position in the parent window
SetRect(&rect, 10, 10, 300, 300);
UINT nControlID = 1111; // Can be any unused control ID
// Create the control
CImageControl* pImgCtrl = new CImageControl();
pImgCtrl->CustomCreate(rect, this, nControlID);
return TRUE;
}
Load an image and set in the control
int iLastError;
HDIB hDIB = LoadImageIntoDIB(_T("c:\\Test\\test.tif"), 0, &iLastError);
if (hDIB)
{
pImgCtrl->SetDIB(hDIB);
}