Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

How i can use a bitmap as background image?

  1. Jun 27, 2004 #1
    how i can use a bitmap as background image ?
    Thanks
     
  2. jcsd
  3. Jun 27, 2004 #2
    In what? Are you using Gnome? Are you using KDE? Are you using the WinAPI? Are you using Mac OS X? Are you using another windowing environment? Are you trying to set the background for a particular piece of software (AIM for example)?

    If you're trying to do this in a piece of software of your own design which platform are you running? Are you using an IDE if so which?

    Most IDE's have the ability to select a background bmp and generate the appropriate code for you. I know KDevelope does, Glade(2) Does, Mac Interface Builder does, and I'm almost (+) VC++ does.

    [edit]The Qt interface builder does, not KDevelope. Qt interface builder can be invoke from KDevelope though before any KDE users correct me.
     
    Last edited: Jun 27, 2004
  4. Jun 27, 2004 #3
    faust9,
    I am sorry about the author problem, Okay ???
    I did never mean to correct you or anything...True!
    I am using winApi, I just do simple things, I don't know how to use a bitmap as a backgound image instead of just setting bkgound to numbers (0,1,2,3,4,5,18), can you help me ?

    Help me please..
     
  5. Jun 28, 2004 #4
    What are you trying to set the background of? I am racking my brain trying to think of something in which the background is a number but can't. Start over from the beginning and tell us exactly what you are trying to set the background of, on what platform, in what programming language and then hopfully we will be able to help.

    I think I got that this was windows. Is this a GUI application? Console?

    Is this C++/MFC, .NET, web programming, or something totally different?
     
  6. Jun 28, 2004 #5

    Background isn't a easly selectible function parameter, its a little convaluted actually. By setting a 'background' your painting a DIB bitmap (if I recall, its been about two years since I've done this because I use a Mac, and Linux) at the lowest level and then placing all of your objects on top of that picture (buttons, other BMP's and the like).

    TenNen here's a code snip from an old program written for win98. I wrote it in C so it might not work on XP (if that's what you're using). You might want ot buy a book or goto a library and get an MFC book.

    Anyway the following is the WndProc function I used:

    Code (Text):

    LRESULT CALLBACK WndProc    (HWND hWnd,
                     UINT ui<essage,
                     WPARAM wParam,
                     LPARAM lParam)
      {
        static BITMAPFILEHEADER *pbmfh;
        static BITMAPINFO *pbmi;
        static BYTE *pBits;
        static int cxDib, cyDib;

        DWORD dwFileSize, dwHighSize, dwBytesRead;
        HANDLE hFile;
        hFile = CreateFile  ("C:\\!!!!!place file path here, lose the !!!! keep the quotes",
                 GENERIC_READ,
                 FILE_SHARE_READ,
                 NULL,
                 OPEN_EXISTING,
                 FILE_FLAG_SEQUENTIAL_SCAN,
                 NULL);
        dwFileSize = GetFileSize(hFile, &bdHighSize);
        pbmfh = (BITMAPHEADER) * malloc (dxFileSize);
        ReadFile    (hFile,
             pbmfh,
             dwFileSize,
             &dwBytesRead,
             NULL);
        pbmi= (BITMAPINFO) * (pbmfh+1);
        pBits = (BYTE) * pbmfh + pbmfh->bfOffBits;
        cxDib = pbmi->bmiHeader.biWidth;
        cyDib = abs(pbmi->bmiHeader.biHeight);
        HDC hdc;
        hdc = GetDC(hWnd);
        SetDIBitsToDevice   (hdc,
                 0,
                 0,
                 cxDib,
                 cyDib,
                 0,
                 0,
                 0,
                 cyDib,
                 pBits,
                 pbmi,
                 DIB_RGB_COLOR);
        ReleaseDC (hWind, hdc);
        free (pbmfh);
        return 0;
    }
     
    Again, the above may or may not work. I don't know if the program I cut it from worked or not because I haven't used MS in so long. It's not a simple matter of calling a function with the file location, you have to jump through some hoops.

    Well, hope this helped. Good luck.
     
  7. Jun 28, 2004 #6
    Ok, so if in fact we are talking about MFC, there is a pretty useful class which I use from time to time over on Codeproject here. It extends an ATL window class so you have to insert a couple lines (most likely in your stdafx.h)

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

    You then make whatever object you want, a subclass of the included header, call load("somepicture") and you're good to go. The cool part is, the pictureWindow class gets a nice pointer to an IPicture interface and gets a nice Render() method so you don't have to deal with DIB directly.

    And here's the class. Visit the Codeproject link for more info. It's a really nice, small, reusable class. You do get some overhead with having to bring in atlbase.h but it might be worth it for you.

    Enjoy.


    Code (Text):

    /************************************
      REVISION LOG ENTRY
      Revision By: Mihai Filimon
      Revised on 11/24/99 9:45:45 AM
      Comments: PictureWindow.h: interface for the CPictureWindow class.
     ************************************/

    #if !defined(AFX_PICTUREWINDOW_H__44323373_9E89_11D3_A393_00C0DFC59237__INCLUDED_)
    #define AFX_PICTUREWINDOW_H__44323373_9E89_11D3_A393_00C0DFC59237__INCLUDED_

    #if _MSC_VER > 1000
    #pragma once
    #endif // _MSC_VER > 1000

    #include <atlbase.h>
    #include <atlwin.h>

    class CPictureWindow  : public CWindowImpl<CPictureWindow>
    {
    public:
        typedef enum HandlerTypeEnum
        {
            ClientPaint = WM_PAINT,
            BackGroundPaint = WM_ERASEBKGND
        } HandlerTypeEnum;

        CPictureWindow()
        {
            m_nMessageHandler = ClientPaint;
        };
        virtual ~CPictureWindow()
        {
        };

        BEGIN_MSG_MAP(CPictureWindow)
            MESSAGE_HANDLER(WM_PAINT, OnPaint)
            MESSAGE_HANDLER(WM_ERASEBKGND, OnEraseBkGnd)
        END_MSG_MAP()

        // Function name    : Load
        // Description      : Loads the picture into memory, and then display it!
        // Return type      : virtual BOOL
        // Argument         : LPCTSTR szFileName
        virtual BOOL Load( LPCTSTR szFileName )
        {
            BOOL bResult = FALSE;
            Close();
            if ( szFileName )
            {
                OFSTRUCT of;
                HANDLE hFile = NULL;;
                if ( (hFile = (HANDLE)OpenFile( szFileName, &of, OF_READ | OF_SHARE_COMPAT)) != (HANDLE)HFILE_ERROR )
                {
                    DWORD dwHighWord = NULL, dwSizeLow = GetFileSize( hFile, &dwHighWord );
                    DWORD dwFileSize = dwSizeLow;
                    HRESULT hResult = NULL;
                    if ( HGLOBAL hGlobal = GlobalAlloc(GMEM_MOVEABLE, dwFileSize) )
                        if ( void* pvData = GlobalLock( hGlobal ) )
                        {
                            DWORD dwReadBytes = NULL;
                            BOOL bRead = ReadFile( hFile, pvData, dwFileSize, &dwReadBytes, NULL );
                            GlobalUnlock( hGlobal );
                            if ( bRead )
                            {
                                CComPtr<IStream> spStream;
                                _ASSERTE( dwFileSize == dwReadBytes );
                                if ( SUCCEEDED( CreateStreamOnHGlobal( hGlobal, TRUE, &spStream) ) )
                                    if ( SUCCEEDED( hResult = OleLoadPicture( spStream, 0, FALSE, IID_IPicture, (void**)&m_spPicture ) ) )
                                        bResult = TRUE;
                            }
                        }
                    CloseHandle( hFile );
                }
            }
            Invalidate();
            return bResult;
        }

        HandlerTypeEnum m_nMessageHandler;

    protected:

        inline virtual BOOL IsHandlerMessage( UINT uMsg )
        {
            return m_nMessageHandler == (HandlerTypeEnum)uMsg ;
        }

        void PutPicture( IPicture* pPicture, HDC hDC, RECT rPicture )
        {
            OLE_XSIZE_HIMETRIC nWidth = NULL; OLE_YSIZE_HIMETRIC nHeight = NULL;
            pPicture->get_Width( &nWidth ); pPicture->get_Height( &nHeight );
            pPicture->Render( hDC, rPicture.left ,rPicture.top, rPicture.right - rPicture.left, rPicture.bottom - rPicture.top, 0, nHeight, nWidth, -nHeight, NULL );
        };

        LRESULT OnEraseBkGnd(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
        {
            if ( IsHandlerMessage( uMsg ) )
            {
                if ( m_spPicture )
                {
                    BeginPaint( NULL );
                        RECT r ; GetClientRect( &r );
                        HDC hDC = GetDC();
                        HWND hWndChild = GetWindow( GW_CHILD );
                        while ( ::IsWindow( hWndChild ) )
                        {
                            if ( ::IsWindowVisible( hWndChild ) )
                            {
                                RECT rChild; ::GetWindowRect( hWndChild, &rChild );
                                ScreenToClient( &rChild );
                                ExcludeClipRect( hDC, rChild.left, rChild.top, rChild.right, rChild.bottom );
                            }
                            hWndChild = ::GetWindow( hWndChild, GW_HWNDNEXT );
                        }
                        PutPicture( m_spPicture, hDC, r );
                        ReleaseDC( hDC );
                    EndPaint( NULL );
                    return TRUE;
                }
            }
            bHandled = FALSE;
            return FALSE;
        };

        LRESULT OnPaint(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
        {
            if ( IsHandlerMessage( uMsg ) )
            {
                if ( m_spPicture )
                {
                    BeginPaint( NULL );
                    RECT r ; GetClientRect( &r );
                    HDC hDC = GetDC();
                    PutPicture( m_spPicture, hDC, r );
                    ReleaseDC( hDC );
                    EndPaint( NULL );
                }
            }
            bHandled = FALSE;
            return NULL;
        };

        void Close()
        {
            m_spPicture = NULL;
        }

        //Attributes
        CComPtr<IPicture> m_spPicture;
    };

    #endif // !defined(AFX_PICTUREWINDOW_H__44323373_9E89_11D3_A393_00C0DFC59237__INCLUDED_)

     
     
  8. Jun 29, 2004 #7
    Thank you so very much, you two are really kewl...
    I can't be online now, I am really busy, we are having a break but I will get back a few hours later to post the results of whether or not my program works fine.

    Again, thanks a lot.
     
  9. Jun 29, 2004 #8
    Anteros, I am coding to make a Win32 application from scratch, I amnot using MFC... I am still new to API, I have known about it for long but never tried to learn it,
    I am actually trying to draw a clock, I think if I can use something as a background image, my application would look better, but I am stuck at it...

    faust9,
    Yes, I couldn't make your code work on my system.
    Thanks a lot anyway,
     
  10. Jun 29, 2004 #9
    That's fine, this class doesn't have to be used from MFC. In fact, it's an ATL class so it's not native MFC at all. Just create an instance of the PictureWindow class and call the SubclassWindow() method on this class giving it a HWND. This can be a handle to a dialog, control, whatever. Then call the Load() method and you're good to go.

    If you don't want to use the ATL libraries at all, well then you're on your own. Check http://www.codeproject.com for some more info. I think I saw a few articles there that may apply. I would say give that class a shot though and see if it works for you.
     
  11. Jun 29, 2004 #10
    Okay, thanks a lot,
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?



Similar Discussions: How i can use a bitmap as background image?
  1. How can I make mp3? (Replies: 2)

  2. How i can hack ? (Replies: 29)

Loading...