Click here to Skip to main content
Click here to Skip to main content

Adding a background image to any window

By , 5 Dec 1999
 
  • Download source files - 2 Kb
  • There are times you may need to change your dialog or control background. It can be easy if you are using the CPictureWindow class. This class uses one of the ATL classes, CWindowImpl. This does not means that the CPictureWindow class cannot be used in MFC applications. All that you need to do is insert two new lines in your stdafx.h:

    #include < atlbase.h > ;
    extern CComModule _Module;
    

    The class is used by subclassing another window. So if you want to change the background of one of your dialogs or a control, all that you need is to add a new member of CPictureWindow in the object, and then subclass the object with your new member.

    For instance, if you want to set the background image in a dialog, you would add a member variable m_PictureWindow to your dialog, and in the OnInitDialog call the function m_PictureWindow.SubclassWindow(m_hWnd ); where m_hWnd is the window handle of the dialog.

    If you want to handle the EraseBackGround message in the dialog you have to call

    m_PictureWindow.nMessageHandler = CPictureWindow::BackGroundPaint. 
    
    After you do this you have to call the Load function of the CPictureWindow class. In this way your background of the dialog will display an image.

    #include "PictureWindow.h"
    BOOL XXXX::OnInitDialog()
    {
    	...
    	m_PictureWindow.SubclassWindow( m_hWnd );
    	m_PictureWindow.m_nMessageHandler = CPictureWindow::BackGroundPaint;
    	m_PictureWindow.Load("image.bmp");
    };
    

    Also, If you have one control child of one dialog (window) and you need to add a background picture all that you need to do is:

    #include "PictureWindow.h"
    BOOL XXXX::OnInitDialog()
    {
    	...
    
    	// The IDC_STATIC1 is the identifier of child window, and the m_hWnd is the
    	// parent window handle
    	m_PictureWindow.SubclassWindow( ::GetDlgItem( m_hWnd, IDC_STATIC1 ) ); 
    	m_PictureWindow.m_nMessageHandler = CPictureWindow::ClientPaint; // This is the default value
    	m_PictureWindow.Load("image.bmp");
    };
    

    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

    About the Author

    Mihai Filimon
    Software Developer Exontrol
    Romania Romania
    Member
    No Biography provided

    Sign Up to vote   Poor Excellent
    Add a reason or comment to your vote: x
    Votes of 3 or less require a comment

    Comments and Discussions

     
    You must Sign In to use this message board.
    Search this forum  
        Spacing  Noise  Layout  Per page   
    GeneralLoad Image from URLmemberJAGUARSOFT9 Apr '10 - 1:25 
    Hi,
     
    Thanks for useful stuff.
     
    Is there any quicj/easy way to load Image from URL
    such as httpp://www.abc.com/Image.bmp ?
    HSO

    GeneralAdd background image in dialog box in VC++memberSanjeev.Kr.mit23 Jul '09 - 5:35 
    Hi, friends,
    I m using one dialog in which 2 command buttons and edit box and labels are there.
    I want to add a picture for the background,for this i create a picture tool and add the picture.
     
    But when i run the application only picture is visible command buttons, edit box and labels are invisible.
    When i pressed the tab buttons the command buttons and edit box become visible but labels are still invisible.
     
    Please suggest me how can i make labels, edit box and command buttons visible without pressing tab buttons.
    QuestionChanging background image of panelmemberWilson Kopman15 Sep '08 - 5:06 
    I am developing my own slide show system that will continue to change background image of a panel replacing it with a picture read from a directory every 3seconds .This happens at runtime,How can i achieve this since in vc++ images used by the Form are in the resource class that reads them from .resx of the Form concern?
    GeneralCompilation error when include the header filememberkmho11 Dec '06 - 21:42 
    hello all,
     
    i got the compilation as below, anyone can help? Thanks
     
    LIBCMTD.lib(sprintf.obj) : error LNK2005: _sprintf already defined in msvcrtd.lib(MSVCR80D.dll)
    LIBCMTD.lib(sprintf.obj) : error LNK2005: _sprintf_s already defined in msvcrtd.lib(MSVCR80D.dll)
    LIBCMTD.lib(swprintf.obj) : error LNK2005: _swprintf_s already defined in msvcrtd.lib(MSVCR80D.dll)
     

    Regards
    Ho
    GeneralLoading image from resourcememberDemian Panello3 Oct '06 - 3:30 
    Hi.
     
    Good article.
     
    But, how could I extend your class so it will able to load a bmp from resource instead of a file. I mean, ie. adding an overloaded Load function with the Bitmap ID instead of file path.
     
    It's your class able to add this feature???
     
    Thank you.
     
    Demian.
     
    "I have always wished that my computer would be as easy to use as my
    telephone. My wish has come true. I no longer know how to use my telephone."

    -Bjarne Stroustrup, computer science professor, designer of C++
    programming language (1950- )

    GeneralBackground have many options? center, stretch, tilemembervietdoorgroup11 Jul '06 - 4:00 
    Background have many options? As center, stretch, tile?
     
    --------------------------
    ERP VN: www.erpvn.net
    GeneralImage is not displayingmemberkumarkatikireddy2 Mar '06 - 19:27 
    Hi,
     
    i have used cpicture in my ATL dialog to display a background image for the dialog. I have done it sucessfully and the code is compiling very well. But at the run time, it is not displaying any background image.
     
    Please any body help me reagrding this.
     

    Regards,
     
    kumar
     
    kumarkatikireddy
    Generalany one have Network programming in VC++ soft notesmemberZeeshan Bilal31 May '04 - 21:01 

     
    hi.
     
    any one have Network programming (Microsoft Press)in VC++ soft copy ... other help abut it..
     
    thanx

    GeneralRe: any one have Network programming in VC++ soft notesmemberGiles31 May '04 - 22:11 
    yes.
    GeneralRe: any one have Network programming in VC++ soft notessussAnonymous30 Jul '04 - 22:10 

    PLZ Send it to me ,, at zahid_ash@yahoo.com
     
    thanx
    GeneralGood one, thanks!memberIlushka2 Jul '03 - 21:41 
    Just what i was looking for! Smile | :)
     
    Sincerely yours, Ilya Kalujny.
    QuestionCan your source be used in embedded VC++ (Pocket PC 2002)?memberWingWing1 Apr '03 - 2:49 
    as title!
    AnswerRe: Can your source be used in embedded VC++ (Pocket PC 2002)?memberMihai Filimon4 Apr '03 - 2:37 
    Be my guest.
     
    Exontrol Inc. Software
    http://www.exontrol.com

    GeneralTo show a BMP picture on the background of MDI clientmemberericlts30 Mar '03 - 19:05 
    Hello friends,
     
    I would like to show a BMP picture on the MDI client's background, Could you please give me a suggestion? Thanks a lot.
     
    Good day.
     
    Eric Lee
    QuestionHow to Adding a background image to any window use bitmap from resource in MFCmemberzm465618 Mar '03 - 22:25 
    How to Adding a background image to any window use bitmap from resource in MFC.
    The method in this class does't work.

    GeneralI don`t know how to use Bitmaps in a MFC ApplicationsussHELP ME PLEASE!!!!!!!!9 Oct '02 - 7:15 
    I need to load a bitmap file, after or that show it in a window and let the user to choose a point of the image with the mouse(like if that were a matrix (x,y)). I must change the color of that point, and show it again.
    Tanks for help me......
    GeneralRe: I don`t know how to use Bitmaps in a MFC ApplicationmemberAnuj Mishra13 Jul '04 - 22:43 
    Use CBitmap class for loading image and then u have to change in BITMAP structure.
     
    Good Luck !!!
     
    Anuj Mishra
    Software Engineer
    GeneralFocus problemmemberj2meman1 Oct '02 - 4:17 
    I am new to the MFC world. I was able to successfully use your class to create a background image in my dialog. I have also placed some controls in my dialog. Once I loose focus to my dialog [alt+tab] then I come back my controls are not repainted. All I see is the background image. Please let me know how I can resolve this issue?
    thanks a lot.
    QuestionResource?memberAnonymous12 Feb '02 - 7:03 
    How would we load the image from a resource???
     
    say a LoadResoruce(CString Resrouce);
     
    Thanks,
     
    David
    AnswerRe: Resource?memberCarlos Antollini12 Feb '02 - 7:07 
    LoadImage, LoadBitmap, LoadIcon....
     
    Regards...
     
    BUFFERS=20 FILES=15 2nd down, 4th quarter, 5 yards to go!

     
    Carlos Antollini.
    Sonork ID 100.10529 cantollini
    GeneralRe: Resource?sussBard26 Aug '02 - 0:26 
    Hi,
     
    I think the question is how to rewrite the code that no path to the image has to be specified but that a resource identifier can be passed.
     
    Regards
    Bernhard
    GeneralRe: Resource?memberIlushka2 Jul '03 - 22:56 
    I accomplished that by using OleCreatePictureIndirect. Nice and easy.
    Great piece of code BTW. Light and reusable.
     
    Sincerely yours, Ilya Kalujny.
    AnswerRe: Resource?memberBakar12 Apr '02 - 10:54 
    if i m right you are asking about how to convert a RESOURCE ID into a string which used as parameter in differnt Loading Function. SO you may use MAKEINTRESOURCE(ID_RESOURCE) Macro to convert Resource id into string.Blush | :O
    GeneralProblem. Debug Sssertion Failed. I need help.memberSpeaker28 Nov '01 - 22:25 
    Hello, I am a student and I am make my project with VC6 and MFC. I am a novate with VC6. I had inserted this code in my program. It run well, but when I close my program I had the next error:
    Debug Assertion Failed!
    program: my project
    File:...\vc98\atl\include\atlwin.h
    Line: 1903
     
    I do not know ATL and I do not solve this problem. Anybody can help me?
     
    Thanks,
     
    Speaker
    GeneralRe: Problem. Debug Sssertion Failed. I need help.memberAnonymous12 Mar '02 - 23:20 
    Hi. I too got the same error when I used this picturewindow object in an SDI (form view). This occurs because the window (view) gets destroyed before the picturewindow object, as you would have declared the picwnd object as a member of the view class. To avoid it, make the picwnd object global. However, this assertion failure would not have occurred in case of a dialog box application.
    Hope this helps.

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

    Permalink | Advertise | Privacy | Mobile
    Web03 | 2.6.130516.1 | Last Updated 6 Dec 1999
    Article Copyright 1999 by Mihai Filimon
    Everything else Copyright © CodeProject, 1999-2013
    Terms of Use
    Layout: fixed | fluid