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

     
    Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
    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 
    GeneralAdd background image in dialog box in VC++memberSanjeev.Kr.mit23 Jul '09 - 5:35 
    QuestionChanging background image of panelmemberWilson Kopman15 Sep '08 - 5:06 
    GeneralCompilation error when include the header filememberkmho11 Dec '06 - 21:42 
    GeneralLoading image from resourcememberDemian Panello3 Oct '06 - 3:30 
    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 
    Generalany one have Network programming in VC++ soft notesmemberZeeshan Bilal31 May '04 - 21:01 
    GeneralRe: any one have Network programming in VC++ soft notesmemberGiles31 May '04 - 22:11 
    GeneralRe: any one have Network programming in VC++ soft notessussAnonymous30 Jul '04 - 22:10 
    GeneralGood one, thanks!memberIlushka2 Jul '03 - 21:41 
    QuestionCan your source be used in embedded VC++ (Pocket PC 2002)?memberWingWing1 Apr '03 - 2:49 
    AnswerRe: Can your source be used in embedded VC++ (Pocket PC 2002)?memberMihai Filimon4 Apr '03 - 2:37 
    GeneralTo show a BMP picture on the background of MDI clientmemberericlts30 Mar '03 - 19:05 
    QuestionHow to Adding a background image to any window use bitmap from resource in MFCmemberzm465618 Mar '03 - 22:25 
    GeneralI don`t know how to use Bitmaps in a MFC ApplicationsussHELP ME PLEASE!!!!!!!!9 Oct '02 - 7:15 
    GeneralRe: I don`t know how to use Bitmaps in a MFC ApplicationmemberAnuj Mishra13 Jul '04 - 22:43 
    GeneralFocus problemmemberj2meman1 Oct '02 - 4:17 
    QuestionResource?memberAnonymous12 Feb '02 - 7:03 
    AnswerRe: Resource?memberCarlos Antollini12 Feb '02 - 7:07 
    GeneralRe: Resource?sussBard26 Aug '02 - 0:26 
    GeneralRe: Resource?memberIlushka2 Jul '03 - 22:56 
    AnswerRe: Resource?memberBakar12 Apr '02 - 10:54 
    GeneralProblem. Debug Sssertion Failed. I need help.memberSpeaker28 Nov '01 - 22:25 
    GeneralRe: Problem. Debug Sssertion Failed. I need help.memberAnonymous12 Mar '02 - 23:20 
    GeneralRe: Problem. Debug Sssertion Failed. I need help.memberAnonymous13 Mar '02 - 1:24 
    GeneralRe: Problem. Debug Sssertion Failed. I need help.memberVNCoder19 May '02 - 18:25 
    GeneralRe: Problem. Debug Sssertion Failed. I need help.membernevedko4 Jul '04 - 12:58 
    GeneralRe: Problem. Debug Sssertion Failed. I need help.sussAnonymous16 Mar '05 - 16:04 
    GeneralBackground ImagessussScott Petrie7 Jun '00 - 5:26 
    GeneralRe: Background ImagesmemberKevin Cheung2 Apr '01 - 4:41 
    GeneralRe: Background ImagesmemberSl@jaR16 Nov '01 - 13:51 
    GeneralRe: Background ImagesmemberSecrets7 Sep '04 - 6:16 
    GeneralI Can'nt build my program when I use your Source code.sussassasin25 Feb '00 - 22:41 
    GeneralI Can'nt build my program when I use your Source code.sussassasin25 Feb '00 - 22:40 
    GeneralI Can'nt build my program when I use your Source code.sussassasin25 Feb '00 - 22:40 
    GeneralRe: Refresh ProblemsussAssassin25 Feb '00 - 22:38 
    GeneralRe: Refresh ProblemsussAssassin25 Feb '00 - 22:37 
    GeneralRe: Refresh ProblemsussAssassin25 Feb '00 - 22:37 
    GeneralRefresh ProblemsussPraveen26 Jan '00 - 0:39 
    GeneralRe: Refresh Problemsusskolo21 Feb '00 - 22:30 
    GeneralRe: Refresh ProblemsussAssassin25 Feb '00 - 22:38 
    GeneralRe: Refresh ProblemsussAssassin25 Feb '00 - 22:38 

    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