Click here to Skip to main content
11,431,844 members (64,276 online)
Click here to Skip to main content

Adding a background image to any window

, 5 Dec 1999
Rate this:
Please Sign up or sign in to vote.
An ATL class that allows you to add a background image to any window
  • Download source files - 2 Kb
  • <!-- Article Starts -->

    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

    Share

    About the Author

    Mihai Filimon
    Software Developer Exontrol
    Romania Romania
    No Biography provided

    Comments and Discussions

     
    GeneralLoad Image from URL Pin
    JAGUARSOFT9-Apr-10 2:25
    memberJAGUARSOFT9-Apr-10 2:25 
    GeneralAdd background image in dialog box in VC++ Pin
    Sanjeev.Kr.mit23-Jul-09 6:35
    memberSanjeev.Kr.mit23-Jul-09 6:35 
    QuestionChanging background image of panel Pin
    Wilson Kopman15-Sep-08 6:06
    memberWilson Kopman15-Sep-08 6:06 
    GeneralCompilation error when include the header file Pin
    kmho11-Dec-06 22:42
    memberkmho11-Dec-06 22:42 
    GeneralLoading image from resource Pin
    Demian Panello3-Oct-06 4:30
    memberDemian Panello3-Oct-06 4:30 
    GeneralBackground have many options? center, stretch, tile Pin
    vietdoorgroup11-Jul-06 5:00
    membervietdoorgroup11-Jul-06 5:00 
    GeneralImage is not displaying Pin
    kumarkatikireddy2-Mar-06 20:27
    memberkumarkatikireddy2-Mar-06 20:27 
    Generalany one have Network programming in VC++ soft notes Pin
    Zeeshan Bilal31-May-04 22:01
    memberZeeshan Bilal31-May-04 22:01 
    GeneralRe: any one have Network programming in VC++ soft notes Pin
    Giles31-May-04 23:11
    memberGiles31-May-04 23:11 
    GeneralRe: any one have Network programming in VC++ soft notes Pin
    Anonymous30-Jul-04 23:10
    sussAnonymous30-Jul-04 23:10 
    GeneralGood one, thanks! Pin
    Ilushka2-Jul-03 22:41
    memberIlushka2-Jul-03 22:41 
    QuestionCan your source be used in embedded VC++ (Pocket PC 2002)? Pin
    WingWing1-Apr-03 3:49
    memberWingWing1-Apr-03 3:49 
    AnswerRe: Can your source be used in embedded VC++ (Pocket PC 2002)? Pin
    Mihai Filimon4-Apr-03 3:37
    memberMihai Filimon4-Apr-03 3:37 
    GeneralTo show a BMP picture on the background of MDI client Pin
    ericlts30-Mar-03 20:05
    memberericlts30-Mar-03 20:05 
    QuestionHow to Adding a background image to any window use bitmap from resource in MFC Pin
    zm465618-Mar-03 23:25
    memberzm465618-Mar-03 23:25 
    GeneralI don`t know how to use Bitmaps in a MFC Application Pin
    HELP ME PLEASE!!!!!!!!9-Oct-02 8:15
    sussHELP ME PLEASE!!!!!!!!9-Oct-02 8:15 
    GeneralRe: I don`t know how to use Bitmaps in a MFC Application Pin
    Anuj Mishra13-Jul-04 23:43
    memberAnuj Mishra13-Jul-04 23:43 
    GeneralFocus problem Pin
    j2meman1-Oct-02 5:17
    memberj2meman1-Oct-02 5:17 
    QuestionResource? Pin
    Anonymous12-Feb-02 8:03
    memberAnonymous12-Feb-02 8:03 
    AnswerRe: Resource? Pin
    Carlos Antollini12-Feb-02 8:07
    memberCarlos Antollini12-Feb-02 8:07 
    GeneralRe: Resource? Pin
    Bard26-Aug-02 1:26
    sussBard26-Aug-02 1:26 
    GeneralRe: Resource? Pin
    Ilushka2-Jul-03 23:56
    memberIlushka2-Jul-03 23:56 
    AnswerRe: Resource? Pin
    Bakar12-Apr-02 11:54
    memberBakar12-Apr-02 11: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. Pin
    Speaker28-Nov-01 23:25
    memberSpeaker28-Nov-01 23:25 
    GeneralRe: Problem. Debug Sssertion Failed. I need help. Pin
    Anonymous13-Mar-02 0:20
    memberAnonymous13-Mar-02 0:20 
    GeneralRe: Problem. Debug Sssertion Failed. I need help. Pin
    Anonymous13-Mar-02 2:24
    memberAnonymous13-Mar-02 2:24 
    GeneralRe: Problem. Debug Sssertion Failed. I need help. Pin
    VNCoder19-May-02 19:25
    memberVNCoder19-May-02 19:25 
    GeneralRe: Problem. Debug Sssertion Failed. I need help. Pin
    nevedko4-Jul-04 13:58
    membernevedko4-Jul-04 13:58 
    GeneralRe: Problem. Debug Sssertion Failed. I need help. Pin
    Anonymous16-Mar-05 17:04
    sussAnonymous16-Mar-05 17:04 
    GeneralBackground Images Pin
    Scott Petrie7-Jun-00 6:26
    sussScott Petrie7-Jun-00 6:26 
    GeneralRe: Background Images Pin
    Kevin Cheung2-Apr-01 5:41
    memberKevin Cheung2-Apr-01 5:41 
    GeneralRe: Background Images Pin
    Sl@jaR16-Nov-01 14:51
    memberSl@jaR16-Nov-01 14:51 
    GeneralRe: Background Images Pin
    Secrets7-Sep-04 7:16
    memberSecrets7-Sep-04 7:16 
    GeneralI Can'nt build my program when I use your Source code. Pin
    assasin25-Feb-00 23:41
    sussassasin25-Feb-00 23:41 
    GeneralI Can'nt build my program when I use your Source code. Pin
    assasin25-Feb-00 23:40
    sussassasin25-Feb-00 23:40 
    GeneralI Can'nt build my program when I use your Source code. Pin
    assasin25-Feb-00 23:40
    sussassasin25-Feb-00 23:40 
    GeneralRe: Refresh Problem Pin
    Assassin25-Feb-00 23:38
    sussAssassin25-Feb-00 23:38 
    GeneralRe: Refresh Problem Pin
    Assassin25-Feb-00 23:37
    sussAssassin25-Feb-00 23:37 
    GeneralRe: Refresh Problem Pin
    Assassin25-Feb-00 23:37
    sussAssassin25-Feb-00 23:37 
    GeneralRefresh Problem Pin
    Praveen26-Jan-00 1:39
    sussPraveen26-Jan-00 1:39 
    GeneralRe: Refresh Problem Pin
    kolo21-Feb-00 23:30
    susskolo21-Feb-00 23:30 
    GeneralRe: Refresh Problem Pin
    Assassin25-Feb-00 23:38
    sussAssassin25-Feb-00 23:38 
    GeneralRe: Refresh Problem Pin
    Assassin25-Feb-00 23:38
    sussAssassin25-Feb-00 23:38 

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

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

    | Advertise | Privacy | Terms of Use | Mobile
    Web04 | 2.8.150428.2 | Last Updated 6 Dec 1999
    Article Copyright 1999 by Mihai Filimon
    Everything else Copyright © CodeProject, 1999-2015
    Layout: fixed | fluid