Load Bitmap as Window Background

This is a discussion on Load Bitmap as Window Background within the Windows Programming forums, part of the Platform Specific Boards category; Hi all, After reading this thread: http://cboard.cprogramming.com/showthread.php?t=53719 I'm under the impression that if I make a fancy background bitmap, I ...

  1. #1
    Epo
    Epo is offline
    Registered User
    Join Date
    Jun 2003
    Posts
    361

    Load Bitmap as Window Background

    Hi all,

    After reading this thread:
    how to set a background as a image?

    I'm under the impression that if I make a fancy background bitmap, I can use:
    CreatePatternBrush(LoadBitmap(hInstance, "Background.bmp"));
    In order to create an HBRUSH of my bitmap? (And then load that into my WNDCLASS structure, resulting in a very nice background for my window.

    Although (from the thread) they used a much crazier way of defining everything, which I tried to re-create, but with no success.

    So, my question is:
    Say I have made a 200x300 bitmap called, oh, "Background.bmp". Without having to create a seperate "STATIC" window to cover my main window and load the image into that, how would I go about applying Background.bmp as the background for my window?

    I have tried and tried to search up a solution here on these boards and on google. I also just got my wisdom teeth out this morning so I may not have been as thorough as I should have, but I really think I searched extensively.

    So, if there's any help out there, I'd be more than happy to accept it

    Thanks
    Pentium 4 - 2.0GHz, 512MB RAM
    NVIDIA GeForce4 MX 440
    WinXP
    Visual Studio .Net 2003
    DX9 October 2004 Update (R.I.P. VC++ 6.0 Compatability)

  2. #2
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    The thread you refer to includes the bitmap as a resource, but you could just as easily load the image from file. But use LoadImage which supersedes LoadBitmap.

    So, assuming background.bmp is in the same directory as your executable:
    Code:
    WNDCLASSEX wcx={0};
    /*fill up other wcx members*/
    wcx.hbrBackground=CreatePatternBrush((HBITMAP) LoadImage(0,_T("background.bmp"),
                                         IMAGE_BITMAP,0,0,
                                         LR_CREATEDIBSECTION|LR_LOADFROMFILE));
    should do it. If you prefer to include the background image as a resource then the syntax for LoadImage is slightly different:
    Code:
    WNDCLASSEX wcx={0};
    /*fill up other wcx members*/
    wcx.hbrBackground=CreatePatternBrush((HBITMAP) LoadImage(GetModuleHandle(0),
                                          MAKEINTRESOURCE(ID_BACK_BMP),
                                         IMAGE_BITMAP,0,0,
                                         LR_CREATEDIBSECTION);
    where ID_BACK_BMP is the resource identifier of your BITMAP image; GetModuleHandle(0) just returns the application instance; replace that with the instance of the resource containing module, if required.

    It's probably better to initially split the bitmap loading and brush creation code so you can ensure that both functions are working as expected. If they're not then use GetLastError to get some idea about why not.

    See also CreatePatternBrush.

    I also just got my wisdom teeth out this morning
    Have you tried novacain - very helpful for coding problems, too.
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  3. #3
    Epo
    Epo is offline
    Registered User
    Join Date
    Jun 2003
    Posts
    361
    I'm a big moron.

    I added that code in, still didn't work. I double-checked that Background.bmp was in my "\Game2" folder. Still didn't work. I messed around with the parameters. Still nothing. Looked back in my "\Game2" folder...turns out there's a "\Game2\Game2\" folder where the program's actually running out of

    Thanks a lot for the help Ken, it's much appreciated. Just a quick question, in this code:
    Code:
    wcx.hbrBackground=CreatePatternBrush((HBITMAP) LoadImage(0,_T("background.bmp"),
                                         IMAGE_BITMAP,0,0,
                                         LR_CREATEDIBSECTION|LR_LOADFROMFILE));
    What's the purpose of the "_T"? I did the whole "Right Click"->"Go To Definition" and followed it down into tchar.h....but still...
    /* Generic text macros to be used with string literals and character constants.
    Will also allow symbolic constants that resolve to same. */
    #define _T(x) __T(x)

    #define __T(x) L ## x
    Doesn't quite clear it up for me. It seems that it's just a safe way to pass a string as a parameter?

    I tried it without the _T:
    Code:
    HBITMAP)(LoadImage(0, "Background.bmp", IMAGE_BITMAP, 0, 0, LR_CREATEDIBSECTION|LR_LOADFROMFILE));
    And found that it still works. So just out of plain curiosity, what does it do?

    Thanks again for the help.

    Have you tried novacain - very helpful for coding problems, too.
    No doubt The Tylenol 3s they prescribed should help about the same amount. I haven't taken any yet, but if I do, I'll make sure the first thing I do is write a stunning program.
    Last edited by Epo; 07-17-2005 at 10:55 AM.
    Pentium 4 - 2.0GHz, 512MB RAM
    NVIDIA GeForce4 MX 440
    WinXP
    Visual Studio .Net 2003
    DX9 October 2004 Update (R.I.P. VC++ 6.0 Compatability)

  4. #4
    C++ Enthusiast jmd15's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    532
    Is there a particular reason you don't want to load it from resource? I find that way a little less messy and easier for me. Try it this way just to show you it works, then you can go back to loading it from file, which isn't too hard as well. When defining the window class, hbrBackground, add this:
    Code:
    wndclass.hbrBackground = CreatePatternBrush( LoadBitmap( hInstance, MAKEINTRESOURCE(BACKGROUND) ) );
    Make sure you change the wndclass and hInstance to what you defined them as. Now you could go off and make a whole different resource header file but if you're only defining one thing you should be fine. Type this in at the top, right after you include windows.h:
    Code:
    #define BACKGROUND 700
    Now go to your resource file, and add this
    Code:
    700 BITMAP "background.bmp"
    That is of course if the bitmap you are loading is called background and that it is in the same directory.
    Trinity: "Neo... nobody has ever done this before."
    Neo: "That's why it's going to work."
    c9915ec6c1f3b876ddf38514adbb94f0

  5. #5
    Epo
    Epo is offline
    Registered User
    Join Date
    Jun 2003
    Posts
    361
    Hm, that does seem cleaner...but currently I'm converting everything into a function of a class:
    Code:
    //bool cWindow::Init()
    //
    //PARAMETERS:
    //The path to a bitmap for our background
    //A pointer to the HINSTANCE generated by our WinMain() function
    //The styles for the class
    //The sytles for the window
    //The width of the window
    //The height of the window
    //The HBRUSH to default to should loading a bitmap fail
    //RETURNS:
    //Whether or not the window is created with success
    //
    //DESCRIPTION:
    //This function is a wrapper for creating a window in the centre of the screen.
    void cWindow::Init(char BackgroundPath[50], HINSTANCE *hInstance, unsigned int ClassStyles,
    		unsigned long WindowStyles, unsigned int Width, unsigned int Height, HBRUSH DefaultBrush)
    {
    ...
    }
    And I'm trying to keep everything as condensed/in one file as possible.

    But when you write it out like that, it's definitely not as complicated as the previous posts I read had me thinking.

    However, your method could easily be altered to give a warning message should "Background.bmp" not be found, and still default to an HBRUSH of the programmer's choice...I'll code it out and give it a shot, then see what comes of it Thanks jmd15.
    Pentium 4 - 2.0GHz, 512MB RAM
    NVIDIA GeForce4 MX 440
    WinXP
    Visual Studio .Net 2003
    DX9 October 2004 Update (R.I.P. VC++ 6.0 Compatability)

  6. #6
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,859
    Heard my name was taken in vain.......

    >>Have you tried novacain - very helpful for coding problems, too.

    Who is this Tylenol 3s you speak of? Haven't seen them around.
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  7. #7
    C++ Enthusiast jmd15's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    532
    No problem Epo.
    Trinity: "Neo... nobody has ever done this before."
    Neo: "That's why it's going to work."
    c9915ec6c1f3b876ddf38514adbb94f0

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Loading a bitmap (Without using glaux)
    By Shamino in forum Game Programming
    Replies: 7
    Last Post: 03-16-2006, 09:43 AM
  2. Button handler
    By Nephiroth in forum Windows Programming
    Replies: 8
    Last Post: 03-12-2006, 06:23 AM
  3. Window scrollbar
    By maxorator in forum Windows Programming
    Replies: 2
    Last Post: 10-07-2005, 01:31 PM
  4. input/output
    By dogbert234 in forum Windows Programming
    Replies: 11
    Last Post: 01-26-2005, 06:57 AM
  5. bitmap not going onto screen
    By stallion in forum Windows Programming
    Replies: 4
    Last Post: 02-22-2003, 10:07 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21