Click here to Skip to main content
Click here to Skip to main content
Go to top

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

     
    GeneralCompilation error when include the header file Pinmemberkmho11-Dec-06 21:42 

    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 | Mobile
    Web02 | 2.8.140916.1 | Last Updated 6 Dec 1999
    Article Copyright 1999 by Mihai Filimon
    Everything else Copyright © CodeProject, 1999-2014
    Terms of Service
    Layout: fixed | fluid