Click here to Skip to main content
13,598,644 members
Click here to Skip to main content
Add your own
alternative version


50 bookmarked
Posted 20 Nov 1999
Licenced CPOL

An auto-sizing bitmap picture control

, 20 Nov 1999
Rate this:
Please Sign up or sign in to vote.
A picture control that displays a picture according to the size of the control, and not the picture itself.

Sample Image


The Picture control available from the dialog editor component bar is great for quickly displaying a picture in a dialog, but it only displays the picture at the original picture's size. A problem occurs if you want to display a bitmap which must be aligned with other controls (eg. a bitmap of an arrow). If you change the size of the dialog box font, then the size and position of each control will also change, but the size of the displayed bitmap will not, resulting in a mis-aligned picture. The problem also occurs if the system font size is changed (The matrox millenium drivers allow you to do this).

To overcome this problem I wrote a CStatic derived class that displays a bitmap according to the size of the underlying CStatic window. When the font size changes, the CStatic window size changes, and the bitmap will be StretchBlt'd to the new size. This allows images to be displayed smaller and larger than their original size.

The easist way to use this class is to add the CBitmapPicture class to your project then create a CStatic object to your dialog, and attach a member variable of type CBitmapPicture to the object. Then in your OnInitDialog function, call CBitmapPicture::SetBitmap to set the bitmap to be used.

BOOL SetBitmap(UINT nIDResource);           // Loads bitmap from resource ID
BOOL SetBitmap(LPCTSTR lpszResourceName);   // Loads bitmap from resource name
BOOL SetBitmap(HBITMAP hBitmap);            // Not recommended, as reloads can't<BR>                                            // be done


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Chris Maunder
Founder CodeProject
Canada Canada
Chris is the Co-founder, Administrator, Architect, Chief Editor and Shameless Hack who wrote and runs The Code Project. He's been programming since 1988 while pretending to be, in various guises, an astrophysicist, mathematician, physicist, hydrologist, geomorphologist, defence intelligence researcher and then, when all that got a bit rough on the nerves, a web developer. He is a Microsoft Visual C++ MVP both globally and for Canada locally.

His programming experience includes C/C++, C#, SQL, MFC, ASP, ASP.NET, and far, far too much FORTRAN. He has worked on PocketPCs, AIX mainframes, Sun workstations, and a CRAY YMP C90 behemoth but finds notebooks take up less desk space.

He dodges, he weaves, and he never gets enough sleep. He is kind to small animals.

Chris was born and bred in Australia but splits his time between Toronto and Melbourne, depending on the weather. For relaxation he is into road cycling, snowboarding, rock climbing, and storm chasing.

You may also be interested in...


Comments and Discussions

Generalstatic Picture control not loading a bmp Pin
ThisIsMeRon2-Feb-09 3:29
memberThisIsMeRon2-Feb-09 3:29 
Questionhow to reset pixel of picture control ? Pin
hansen.Xue8-Oct-06 17:49
memberhansen.Xue8-Oct-06 17:49 
GeneralDisplaying the picture Pin
gabriel92726-Jul-05 8:11
membergabriel92726-Jul-05 8:11 
GeneralAbout error checking Pin
gabriel92726-Jul-05 8:02
membergabriel92726-Jul-05 8:02 
QuestionLoad from a file? Pin
Snillet2k28-Aug-03 22:10
memberSnillet2k28-Aug-03 22:10 
AnswerRe: Load from a file? Pin
brocky10-Jan-05 4:27
memberbrocky10-Jan-05 4:27 
GeneralRe: Load from a file? Pin
He||Fire19-May-06 2:29
memberHe||Fire19-May-06 2:29 
Generalbitmap and vector Pin
Erik YO19-May-03 8:28
sussErik YO19-May-03 8:28 
GeneralDisplay a bitmap in a dialog or on a form view Pin
Warren J. Hebert2-Apr-02 14:38
memberWarren J. Hebert2-Apr-02 14:38 
QuestionBitmap in Dialog. gif, jpg? Pin
Guido4-Jun-01 12:32
memberGuido4-Jun-01 12:32 
AnswerRe: Bitmap in Dialog. gif, jpg? Pin
hvibh28-Nov-02 23:46
memberhvibh28-Nov-02 23:46 
GeneralRe: Bitmap in Dialog. gif, jpg? Pin
WREY4-Jun-03 8:17
memberWREY4-Jun-03 8:17 
AnswerRe: Bitmap in Dialog. gif, jpg? Pin
WREY4-Jun-03 8:19
memberWREY4-Jun-03 8:19 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web02-2016 | 2.8.180621.3 | Last Updated 21 Nov 1999
Article Copyright 1999 by Chris Maunder
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid