#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2007
    Posts
    10
    Rep Power
    0

    Background Image on WINAPI Window


    I was wondering in advance before I start coding anything which may prove to be a waste a time, if it is possible to change the background of a WINAPI window to an image. I know it is possible to change the colour but I am not too sure about images. Thanks.
  2. #2
  3. ASP.Net MVP
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Aug 2003
    Location
    WI
    Posts
    4,378
    Rep Power
    1511
    Not sure, but I think you have to process WM_PAINT and draw it yourself.

    [edit]1800th post
    Primary Forum: .Net Development
    Holy cow, I'm now an ASP.Net MVP!

    [Moving to ASP.Net] | [.Net Dos and Don't for VB6 Programmers]

    http://twitter.com/jcoehoorn
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2007
    Posts
    10
    Rep Power
    0
    Thanks, I've done some research and that seems like the way to go.
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2007
    Posts
    10
    Rep Power
    0
    I've been battling for the last few hours to get an image onto a window that I have created, but to no avail. The properties that I set for the window e.g. size, background colour all work. I've tried a few different examples but none of them work, the one below is based on the skeleton code provided by Dev C++ for a Windows application, with "case WM_PAINT" been taking from somewhere else:
    Code:
    #include <windows.h>
    HINSTANCE hInst=NULL;
    /*  Declare Windows procedure  */
    LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);
    
    char szClassName[ ] = "Television";
    
    int WINAPI WinMain (HINSTANCE hThisInstance,
                        HINSTANCE hPrevInstance,
                        LPSTR lpszArgument,
                        int nFunsterStil)
    
    {
        HWND hwnd;               /* This is the handle for our window */
        MSG messages;            /* Here messages to the application are saved */
        WNDCLASSEX wincl;        /* Data structure for the windowclass */
    
        /* The Window structure */
        wincl.hInstance = hThisInstance;
        wincl.lpszClassName = szClassName;
        wincl.lpfnWndProc = WindowProcedure;      /* This function is called by windows */
        wincl.style = CS_DBLCLKS;                 /* Catch double-clicks */
        wincl.cbSize = sizeof (WNDCLASSEX);
    
        /* Use default icon and mouse-pointer */
        wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
        wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
        wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
        wincl.lpszMenuName = NULL;                 /* No menu */
        wincl.cbClsExtra = 0;                      /* No extra bytes after the window class */
        wincl.cbWndExtra = 0;                      /* structure or the window instance */
        /* Use Windows's default color as the background of the window */
        wincl.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
        if (!RegisterClassEx (&wincl))
            return 0;
    
        /* The class is registered, let's create the program*/
        hwnd = CreateWindowEx (
               0,                   /* Extended possibilites for variation */
               szClassName,         /* Classname */
               "Windows App",       /* Title Text */
               WS_OVERLAPPEDWINDOW, /* default window */
               CW_USEDEFAULT,       /* Windows decides the position */
               CW_USEDEFAULT,       /* where the window ends up on the screen */
               544,                 /* The programs width */
               375,                 /* and height in pixels */
               HWND_DESKTOP,        /* The window is a child-window to desktop */
               NULL,                /* No menu */
               hThisInstance,       /* Program Instance handler */
               NULL                 /* No Window Creation data */
               );
    
        /* Make the window visible on the screen */
        ShowWindow (hwnd, nFunsterStil);
    
        /* Run the message loop. It will run until GetMessage() returns 0 */
        while (GetMessage (&messages, NULL, 0, 0))
        {
            /* Translate virtual-key messages into character messages */
            TranslateMessage(&messages);
            /* Send message to WindowProcedure */
            DispatchMessage(&messages);
        }
    
        /* The program return-value is 0 - The value that PostQuitMessage() gave */
        return messages.wParam;
    }
    
    
    /*  This function is called by the Windows function DispatchMessage()  */
    
    LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
    {
        HDC hDC, MemDCExercising;
        PAINTSTRUCT Ps;
        HBITMAP bmpExercising;
        BITMAP bm;
        
        switch (message)                  /* handle the messages */
        {
            case WM_DESTROY:
                PostQuitMessage (0);       /* send a WM_QUIT to the message queue */
                break;
                
            case WM_PAINT:
    	        hDC = BeginPaint(hwnd, &Ps);
    
    	    // Load the bitmap from the resource
    	    bmpExercising = LoadBitmap(hInst,"EXERCISEBMP");
    	    // Create a memory device compatible with the above DC variable
    	    MemDCExercising = CreateCompatibleDC(hDC);
                 // Select the new bitmap
            SelectObject(MemDCExercising, bmpExercising);
            
            GetObject(bmpExercising, sizeof(bm), &bm);
    	    // Copy the bits from the memory DC into the current dc
    	    BitBlt(hDC, 10, 10,bm.bmWidth, bm.bmHeight, MemDCExercising, 0, 0, SRCCOPY);
    
    	    // Restore the old bitmap
    	    DeleteDC(MemDCExercising);
    	    DeleteObject(bmpExercising);
    	    EndPaint(hwnd, &Ps);
    	    break;
            default:                      /* for messages that we don't deal with */
                return DefWindowProc (hwnd, message, wParam, lParam);
        }
    
        return 0;
    }
    Also I'm also sure that my .rc file has been defined correctly, so does anyone have an idea as to what might be causing this?
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    May 2004
    Posts
    3,417
    Rep Power
    887
    Are you sure you're getting the WM_PAINT message? Have tried putting a break point in the WM_PAINT handling code to see if it actually gets called?

    I think you might need the following just after creating the Window:

    Code:
    if (!hwndMain) 
    	return FALSE; 
     
    // Show the window using the flag specified by the program 
    // that started the application, and send the application 
    // a WM_PAINT message. 
     
    ShowWindow(hwndMain, SW_SHOWDEFAULT); 
    UpdateWindow(hwndMain);
    I got the above code from the MSDN examples.

    I also think you should be calling GetUpdateRect() to determine whether you need to call BeginPaint().

    You might get better, faster assistance if this were posted in the Windows forum rather than the C/C++/C# forum. The issue appears to be Windows specific and not a language problem.
    I no longer wish to be associated with this site.
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2007
    Posts
    10
    Rep Power
    0
    Thanks for the response but that didn't seem to work. In terms of posting on a the Windows Forum, there doesn't seem to be a section for GDI development.
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    May 2004
    Posts
    3,417
    Rep Power
    887
    I no longer wish to be associated with this site.
  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2007
    Posts
    10
    Rep Power
    0
    I've managed to track the problem down to the image not being loaded. I thought it was simply enough to define it within a .rc file and then reference it, but it looks like I have to take care of things in a header file too. Visual C++ takes care of this but because I'm using Dev-C++ I have no clue how to do this.

IMN logo majestic logo threadwatch logo seochat tools logo