| bitbucket: C++ MFC ATL WTL Win32 COM ActiveX Samples Tutorials Source Code Controls |
WndImage |
Download Sample Project |
CWndImage implements the minimal steps required to display a bitmap. The class is "plug-and-play" for MFC, most of the magic happens in the OnPaint function which should be easy to port to Win32 or ATL. Some features like scaling, cutting and tiling were added on the way, when they were a spinoff of the required implementation (or existing snippets).
Overview |
Version 1.2 | ||||||||||||||||
![]() Sample application screenshot |
Easy to use:
Features:
|
Note on the demo: The flicker when resizing is caused by the dialog itself, not by the CWndImage. Avoiding this requires quite some work, so I didn't include it here.
Will not be done: image processing, rotating, shearing. Loading other file formats than BMP. For the latter you can use one of the many libraries to load images into an HBITMAP, and then continue. The class is neither optimized nor intended to handle large images. Using large images on weak hardware will hog the system.
Try it!
| 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, bool useDIB = true) // Load from resource bool SetImg(UINT resID, HINSTANCE instance = 0, bool useDIB = true) // Load from resource bool SetImgFile(LPCTSTR fileName, bool useDIB = true) // 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. useDIB: if true (default), the bitmap is loaded as DIB section. This is necessary for correct palette managment. see the "Palette Issues" for more. |
|||||||||||||||||||||||||||||||||||
| Standard Blit Modes | void SetBltMode(int mode)
Note: the constants must be prefixed with "CWndImage::" |
|||||||||||||||||||||||||||||||||||
| 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 prototype 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 second 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 |
These functions return what they promise. The values are valid in custom and in standard blit or alignment modes. HBITMAP GetBitmap(bool detach = false) CRect GetVisibleRect() |
Correct display at 256 colors turned out to be more than a "quick fix". Well, I learnt a lot about bitmaps, palettes & GDI... Thanks to the codeproject & codeguru site. (I don't quite remember which actual article I used)
Load Bitmap:
For correct display on 256 color, the bitmap must be loaded as DIB section. This is the
default (V1.2) for SetImg(resID), SetImg(resName), and SetImgFile.
Otherwise, Windows will map the bitmaps colors already when loading the bitmap, and this
can look quite ugly. You can control this by the useDIB parameter in SetImg(File). In the
sample, you can play around with the "use DIBSECTION" checkbox.
Capture Palette changes:
The image needs to redisplay after system palette changes. The corresponding message (WM_PALETTECHANGED) is only sent to top level windows, so here's what you have to do:
a) Add a WM_PALETTECHANGED handler to your top level window, and call the m_wndImg.OnAppPaletteChanged() function. This technique is used in the sample.
b) alternatively, you can place the following code in your handler:
// WM_PALETTECHANGED message handler (top level window) void CMainFrame::OnPaletteChanged(CWnd* pFocusWnd )
{
SendMessageToDescendants(WM_PALETTECHANGED, (WPARAM) pFocusWnd->GetSafeHwnd(), 0);
CDialog::OnPaletteChanged(pFocusWnd);
}
|
This will forward the message to all child windows. CWndImage has a handler for this, and other windows should be unharmed.
The following sample images are included:
Revision History:
| Version 1.2 | Correct display in 256 color modes The signature of some members has changed (SetImg, SetImgFile). An argument with default value was added, so existing code should compile ok. However, the default value changes behaviour 8to the better) - see "Palette issues" added "GetVisibleRect" |
| Version 1.1 | CreateFromStatic now uses the initial bitmap (if any). |
bitbucket c++ site © Peter Hauptmann. Questions
& Comments to cherea@cherea.de
page updated 29/02/04