Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

WndImage Control

0.00/5 (No votes)
6 May 2002 1  
An easy-to-use control to display bitmaps (stretch, scale, tile)

Sample Image - WndImg.jpg

CWndImage Control

CWndImage can display a Windows Bitmap at arbitrary scales. It provides default scale and alignment modes for easy use, while free scaling and shifting, and a tile mode, is also available. This control can be handy if you have problems with the size of bitmaps in dialogs at different systems.

No image processing, rotating, shearing available or intended.

Overview

  • Easy use: put a static control on a dialog resource, then call CWndImage::CreateFromStatic, to replace it with the WndImage control
  • SetImg: HBITMAP, Resource ID, or SetImgFile(filename)
  • SetBltMode for predefined scaling modes
  • SetAlign for predefined alignment (left/center/right, top/center/bottom)
  • SetZoom and SetOrigin for custom placement
  • SetSourceRect for displaying only a clipped rectangle of the bitmap

Try it!

  1. add the WndImage.h and wndImage.cpp to your project, #include wndimage.h where you need it
  2. add a static or "picture" control to one of your dialogs. Give it an IDC_ST_IMG control ID.
  3. Add a CWndImage m_img member variable to your dialog class
  4. Add the following lines to OnInitDialog:
  5. m_img.CreateFromStatic( GetDlgItem(IDC_ST_IMG) );
    m_img.SetImg(IDB_MYBITMAP);  // specify a valid bitmap resource here
    
    m_img.SetBltMode(CWndImage::bltFitXY);
    

(if you used a picture control, and already specified a bitmap, you can even omit the second and third line)

Documentation

Create

BOOL Create(RECT const & r, CWnd * parent, UINT id, DWORD dwStyle = WS_CHILD | WS_VISIBLE)
BOOL CreateFromStatic(CWnd * static)

CreateFromStatic will use a static control's style, position, and ID, and replace it with the wndImage control. If a bitmap was specified for the static control (you nedd to choose 'picture control' in the resource editor for this), this bitmap is displayed in FitXY mode (stretched proportional to fit without clipping).
Not that only window-specific styles (like WS_BORDER) will be visible with the control. Window styles specific to static controls have no effect

Select Image

void SetImg(HBITMAP bmp, bool shared = false)
void SetImg(CBitmap * bmp)
bool SetImg(LPCTSTR resID, HINSTANCE instance = 0) // Load from resource
bool SetImg(UINT resID, HINSTANCE instance = 0) // Load from resource
bool SetImgFile(LPCTSTR fileName) // Load from File

shared: if true, the WndImage control will not DeleteObject() the HBITMAP provided upon destruction. The prototypes 2..5 do not share the bitmap.
instance: if not NULL, specifies the module handle from which to load the bitmap

Standard Blit Modes

void SetBltMode(int mode)

bltNormal the image is drawn in it's original size a/r
bltStretch the image is stretched to fit the entire window
bltFitX the image is scaled proportional, to horizontally fit the window a/r
bltFitY the image is scaled proportional, to vertically fit the window a/r
bltFitXY the image is scaled proportional to the largest possible size that does not clip the image a/r
bltFitSm the image is scaled proportional so that one coordinate fits the window and the other is clipped
(this mode is good for small views whose aspect ratio differs strongly from the image's a/r)
a/r
bltTile the image is repeated in it's original size to fit the window a/r

Note: the constants must be prefixed with "CWndImage::"
Note: a/r: These modes preserve the image's aspect ratio

Standard Alignment

void SetAlign(int alignX, int alignY)
void SetAlignX(int alignX)
void SetAlignY(int alignY)

Valid alignments are: bltLeft, bltTop, bltCenter, bltRight, bltBottom (I hope you can figure out their meaning)

Custom Zoom

void SetZoom(double zoomX, double zoomY)
void SetZoom(double zoom)

Sets a zoom factor (1.0 == original size, <1 smaller, >1 larger). The second prototypwe sets a proportional zoom with zoomX == zoomY
Custom Zoom cannot be used together with bltTile mode.

Custom Alignment

void SetOrigin(int origX, int origY)
void SetOriginX(int origX)
void SetOriginY(int origY)

Sets the position of the images left upper corner in the window. positive values move the image to the center, negative values move it out of sight.
Custom alignment can be used with tiling, too

Source Clipping

void SetSourceRect(RECT const & r)
void SetSourceRect()

by using SetSourceRect, you can specify a clipping rectangle for the bitmap. This can be used with all modes. The coordinates are in bitmap pixels. The secnod prototype restores the setting to the entire bitmap.
When the image is changed (SetImg, SetImgFile), all settings except the Source window are preserved. Source window is reset to the entire image.

Background

void SetBackgroundBrush(HBRUSH)
void SetBackgroundBrush(int sysColorIndex)
void SetBackgroundBrush(CBrush & brush)

Sets the brush used to fill background not occupied by the image. For valid sysColorindex values, see GetSysColor Win32 API documentation (e.g. COLOR_WINDOW for default window background color, or COLOR_3DFACE for default dialog background)
Initial color is COLOR_3DFACE

Get Info

int GetImgSizeX() const int GetImgSizeY() const
int GetBltMode() const
double GetZoomX() const double GetZoomY() const
int GetAlignX() const int GetAlignY() const
int GetOriginX() const int GetOriginY() const

These functions return what they promise. The values are correct even in standard blit or alignment modes.

HBITMAP GetBitmap(bool detach = false)

Returns a handle the bitmap in use. If detach is true, the control will no longer use it.

Revision History:

Version 1.1 CreateFromStatic now uses the initial bitmap (if any).

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here