PreviewCtrl






4.74/5 (20 votes)
Sep 2, 2005
1 min read

86790

3656
Showing an image in a CStatic control.
Introduction
Drawing a CBitmap
into a CStatic
is a piece of cake. At least if you stop to mess around with CStatic::SetBitmap()
. To get some running code, I derived CPreviewRect
from CStatic
and implemented the display routines manually. This small application demonstrates its usage.
Features
- The shown images are resizable and always shown in correct aspect ratio.
- The shown images can be resized to thumbnail size to save up memory and to speed up drawing.
- The shown images can be specified by:
- a path name
- a resource ID
- another
CBitmap
- raw RGB data (in conjunction with width and height information)
- Compared to
CStatic::SetBitmap()
it works.
Background
CStatic::SetBitmap()
drove me mad.
Using the code
Guess you have a dialog with a CStatic
control on it, and you want to show loaded bitmaps in this control. First of all please make sure that the control's name differs from IDC_STATIC
and create a related member variable. Now modify your code as follows:
In your dialog's header file:
// change the control's class from CStatic to CPreviewRect
CPreviewRect m_bitmap;
In your FileOpen handler:
// create your file dialog CFileDialog dlg(TRUE, 0, 0, OFN_HIDEREADONLY | OFN_FILEMUSTEXIST, "Bitmaps (.bmp)|*.bmp|All files(*.*)|*.*||"); // ask user if (dlg.DoModal() == IDCANCEL) return; // load the selected bitmap file m_bitmap.LoadBitmap(dlg.GetPathName()); // check recent file operation (IsInitialized() should return TRUE) if (!m_bitmap.IsInitialized()) { // load error bitmap from a resource (if you have any) m_bitmap.LoadBitmap(IDB_BLUESCREEN); } // resize bitmap to save up memory and speed up drawing m_bitmap.CreateThumbnail(320, 240);
That's it!
History
Version 1.1
Added:
BorrowBitmap(CBitmap* pBitmap)
Changed:
CreateBitmap(CBitmap* pBitmap)
has been renamed toCopyBitmap(CBitmap* pBitmap)
Version 1.0
Added:
LoadBitmap(const CString& Path)
LoadBitmap(UINT nIDResource)
CreateBitmap(CBitmap* pBitmap)
CreateBitmap(int Width, int Height, unsigned char* BGRA)
CreateThumbnail(int MaxWidth, int MaxHeight)
Reset(BOOL GraphicalUpdate = TRUE)