Thread: Paint BMP from another function

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    37

    Paint BMP from another function

    Hello my prob is as follows:

    I need to paint an object to the window from anoher void function...and need to access the function at any given moment to paint only then and not when the window is loaded.SO I did create my function as follows:

    Code:
    void DrawBall(HWND hwnd)
    {
    
               
               BITMAP bm;
                PAINTSTRUCT ps;
                RECT rcClient;
                HBITMAP hbmOld;
                HDC hdcMem;
        int  y_axis=10;
        int x_axis=40;       
                 
                 HDC hdc = BeginPaint(hwnd, &ps);
    
                hdcMem = CreateCompatibleDC(hdc);  
         
         
       for(i=1;i<=4;i++)
                {                
                 hdcMem = CreateCompatibleDC(hdc);
                 hbmOld = SelectObject(hdcMem, g_hbmimg[i]);
    			 GetObject(g_hbmimg[i], sizeof(bm), &bm);
    			 
                  if (i==3)
                  {
    			    y_axis=380;
    			    x_axis=-40;
                   }
                 
                 BitBlt(hdc, x_axis*(i), y_axis,bm.bmWidth,bm.bmHeight, hdcMem, 0, 0, SRCAND);
                 
                 SelectObject(hdcMem, hbmOld);
    			 DeleteDC(hdcMem);
                }
                EndPaint(hwnd, &ps);
    }

    and accessed it from my the case WM_PAINT: to try to paint it on load first..but the BMP wont be painted

    btw I passed the hwnd so as to be able to paint on the window..
    DrawBall(hwnd);

    am I wrong ? or am I totally wrong?

    Using DEV C++ and WIN XP

  2. #2
    Registered User pronecracker's Avatar
    Join Date
    Oct 2006
    Location
    netherlands
    Posts
    158
    maybe something's wrong with the global bitmaps?

    You waste some GDI resource by creating hdcMem twice, thereby losing the handle to the first dc created. This cannot be the main problem however.
    Last edited by pronecracker; 04-26-2007 at 03:08 PM. Reason: never mind

  3. #3
    Registered User
    Join Date
    Apr 2007
    Posts
    37
    The thing is, I just start Windows programming..two weeks ago. So am not quite well familiar with the functions am learning in fact by myself.So I came to wonder If I can update a BITMAP drawn or draw it once a control has been click for example..So I created the function void and placed the painting code there but it wont paint..unless the code is in the WM_PAINT case... any clue?

    btw plz ignore my lame approach to masking using SRCAND....I didnt paste the complete code with masking...

  4. #4
    Registered User
    Join Date
    Apr 2007
    Posts
    37
    aie..Yes ..its only now that I spotted that I created hdcMem twice ..but still....I'll try tomorrow, may be some sleep will help..

  5. #5
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    BeginPaint() can only be called in response to a WM_PAINT message.

    To get your app to generate a WM_PAINT msg you use

    Code:
    //look up the params
    
    InvalidateRect() //set the window and rectangle to repaint
    UpdateWindow() //bypass the normal procedure (OS msg que) and send directly to app callback ie faster
    In your code the rectangle to redraw will be empty and the HDC invalid

    try
    Code:
    RECT      Rect;
    SetRectEmpty(&Rect);
    //before the BeginPaint
    GetUpdateRect(hWnd,&Rect,FALSE);
    if(IsRectEmpty(&Rect))
       return;//nothing to draw or send paint to get something to draw
    "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

  6. #6
    Registered User Queatrix's Avatar
    Join Date
    Apr 2005
    Posts
    1,342
    Quote Originally Posted by Sober
    aie..Yes ..its only now that I spotted that I created hdcMem twice ..but still....I'll try tomorrow, may be some sleep will help..
    Yeah, maybe you should "Sober" up some.

  7. #7
    Registered User
    Join Date
    Apr 2007
    Posts
    37
    Hahahaha LOL yeah I did sober up the day after I posted the thread....and indeed I managed to solve the problem..anyway thx

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  2. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  3. Trying to paint a BMP to the window
    By abachler in forum Windows Programming
    Replies: 12
    Last Post: 04-25-2007, 12:18 PM
  4. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  5. const at the end of a sub routine?
    By Kleid-0 in forum C++ Programming
    Replies: 14
    Last Post: 10-23-2005, 06:44 PM