Click here to Skip to main content
15,867,308 members
Articles / Desktop Programming / MFC
Article

An auto-sizing bitmap picture control

Rate me:
Please Sign up or sign in to vote.
2.91/5 (12 votes)
20 Nov 1999CPOL1 min read 150.1K   4.8K   53   13
A picture control that displays a picture according to the size of the control, and not the picture itself.

Sample Image

Introduction

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

License

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


Written By
Founder CodeProject
Canada Canada
Chris Maunder is the co-founder of CodeProject and ContentLab.com, and has been a prominent figure in the software development community for nearly 30 years. Hailing from Australia, Chris has a background in Mathematics, Astrophysics, Environmental Engineering and Defence Research. His programming endeavours span everything from FORTRAN on Super Computers, C++/MFC on Windows, through to to high-load .NET web applications and Python AI applications on everything from macOS to a Raspberry Pi. Chris is a full-stack developer who is as comfortable with SQL as he is with CSS.

In the late 1990s, he and his business partner David Cunningham recognized the need for a platform that would facilitate knowledge-sharing among developers, leading to the establishment of CodeProject.com in 1999. Chris's expertise in programming and his passion for fostering a collaborative environment have played a pivotal role in the success of CodeProject.com. Over the years, the website has grown into a vibrant community where programmers worldwide can connect, exchange ideas, and find solutions to coding challenges. Chris is a prolific contributor to the developer community through his articles and tutorials, and his latest passion project, CodeProject.AI.

In addition to his work with CodeProject.com, Chris co-founded ContentLab and DeveloperMedia, two projects focussed on helping companies make their Software Projects a success. Chris's roles included Product Development, Content Creation, Client Satisfaction and Systems Automation.

Comments and Discussions

 
Generalstatic Picture control not loading a bmp Pin
VC++Maniac2-Feb-09 3:29
VC++Maniac2-Feb-09 3:29 
Questionhow to reset pixel of picture control ? Pin
hansen.Xue8-Oct-06 17:49
hansen.Xue8-Oct-06 17:49 
GeneralDisplaying the picture Pin
gabriel92726-Jul-05 8:11
gabriel92726-Jul-05 8:11 
GeneralAbout error checking Pin
gabriel92726-Jul-05 8:02
gabriel92726-Jul-05 8:02 
QuestionLoad from a file? Pin
Snillet2k28-Aug-03 22:10
Snillet2k28-Aug-03 22:10 
AnswerRe: Load from a file? Pin
brocky10-Jan-05 4:27
brocky10-Jan-05 4:27 
GeneralRe: Load from a file? Pin
twizted_sc19-May-06 2:29
twizted_sc19-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
Warren J. Hebert2-Apr-02 14:38 
QuestionBitmap in Dialog. gif, jpg? Pin
Guido4-Jun-01 12:32
Guido4-Jun-01 12:32 
AnswerRe: Bitmap in Dialog. gif, jpg? Pin
hvibh28-Nov-02 23:46
hvibh28-Nov-02 23:46 
GeneralRe: Bitmap in Dialog. gif, jpg? Pin
4-Jun-03 8:17
suss4-Jun-03 8:17 
AnswerRe: Bitmap in Dialog. gif, jpg? Pin
4-Jun-03 8:19
suss4-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.