[RESOLVED] A little help! WM_PAINT + BMP
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: [RESOLVED] A little help! WM_PAINT + BMP

  1. #1
    Join Date
    May 2012
    Posts
    16

    [RESOLVED] A little help! WM_PAINT + BMP

    Hiho,

    I just want to put a background image in my app, apparently works well but not, after a few clicks to resize it, my pc slow down, and I do not see the problem or may be my code wrong...accept help and suggestions! && light my way!

    Code:
    	//Warnings 0 
    	//GetLastError No Error
    	RECT rc;
    	GetClientRect (wp_hwnd, &rc);
    	HDC hdc_dst = GetDC (wp_hwnd);
    	HDC hdc_src = CreateCompatibleDC (hdc_dst);
    	HBITMAP bg_image = LoadBitmap (GetModuleHandle(NULL), MAKEINTRESOURCE (IDM_BGBMP)); 
    	BITMAP bm;
    	PAINTSTRUCT ps;
    
    
    	switch (wp_msg)
    	{
    	case WM_PAINT:
    		BeginPaint (wp_hwnd, &ps);
    		GetObject (bg_image, sizeof (bm), &bm);
    		SelectObject (hdc_src, bg_image);
    		StretchBlt (
    			hdc_dst,
    			0,0,
    			rc.right, rc.bottom,
    			hdc_src,
    			0, 0, 
    			bm.bmWidth, bm.bmHeight,
    			SRCCOPY);
    
    		DeleteObject (&bg_image);
    		ReleaseDC (wp_hwnd, hdc_dst);
    		DeleteDC (hdc_src);
    		DeleteObject (&bm);
            EndPaint(wp_hwnd, &ps);
    		break;
    
    	case WM_SIZE:
    		SendMessage (wp_hwnd,WM_PAINT,0,0);
    		break;

  2. #2
    Join Date
    Apr 1999
    Posts
    27,426

    Re: A little help! WM_PAINT + BMP

    Quote Originally Posted by v_nom70 View Post
    Hiho,

    I just want to put a background image in my app, apparently works well but not, after a few clicks to resize it, my pc slow down, and I do not see the problem or may be my code wrong...accept help and suggestions! && light my way!
    You should really check your return values for those functions you're calling. You're just assuming they work without seeing if any of them return an error.
    Code:
    DeleteObject (&bg_image);
    Why are you passing the address of an HBITMAP to DeleteObject?

    http://msdn.microsoft.com/en-us/libr...(v=vs.85).aspx

    If you checked the return value of DeleteObject, does it return 0 or non-zero? If it is zero, then you would have discovered the problem is with this function.

    Regards,

    Paul McKenzie

  3. #3
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    5,960

    Re: A little help! WM_PAINT + BMP

    BITMAP is just a structure intended to get bitmap info. This is not a GDI object, so DeleteObject(&bm) makes no sense. The deleted object should be HBITMAP bg_image. But with this you have another problem: you never unselect it prior to deleting hdc_src. However you must do that:
    Code:
    HBITMAP hbmOld = SelectObject(hdc_src, bg_image);
    . . .
    SelectObject(hdc_src, hbmOld); // unselecting bg_image 
    // now you're allowed to delete
    DeleteObject(bg_image);
    DeleteDC(hdc_src);
    Best regards,
    Igor

  4. #4
    Join Date
    May 2012
    Posts
    16

    Re: A little help! WM_PAINT + BMP

    mmm ok's im testing now... thanks guys.

  5. #5
    Join Date
    Aug 2000
    Location
    New York, NY, USA
    Posts
    5,526

    Re: A little help! WM_PAINT + BMP

    Do you really need to load your image in response to EVERY windows message you get? (hint – no, you don’t).
    Since you only attempt to delete this HBITMAP while processing WM_PAINT message, all other ones are leaking.
    Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
    Convenience and productivity tools for Microsoft Visual Studio:
    FeinViewer - an integrated GDI objects viewer for Visual C++ Debugger, and more...

  6. #6
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,359

    Re: A little help! WM_PAINT + BMP

    2 OP:
    You should not (must not!) send WM_PAINT message (in response on the WM_SIZE)
    Instead you should (if you really need to) call InvalidateRect which will cause Windows to send WM_PAINT message to your window.
    Victor Nijegorodov

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center