Thread: Redrawing/refreshing a displayed bitmap in MFC?

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    72

    Redrawing/refreshing a displayed bitmap in MFC?

    through this post, I've managed to make pictures show up in a dialog box (great info there).

    However, if I move another window over and then off of my displayed image, the displayed image doesn't redraw. Is there a way to force a redraw when it needs it? How do you determine when it needs it?

    I'll post code if necessary, it just doesn't seem so in this case.

    ....I'm finally beginning to get up to speed with Windows programming - it ain't so bad when you get used to it.

    Thanks.

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    The original code I posted works properly. The OnPaint() method is called whenever the window needs updating.

    Post you OnPaint() code and we'll go from there.

    gg

  3. #3
    Registered User
    Join Date
    Oct 2002
    Posts
    72
    both the init and the paint:
    Code:
    BOOL CFloatingDlg::OnInitDialog()
    {
    	CDialog::OnInitDialog();
    	
    	SetWindowPos(NULL, 
    			0, 0,
    			960, 540,
    			SWP_NOMOVE | SWP_NOZORDER); 
    
    	return TRUE;  // return TRUE  unless you set the focus to a control
    }
    
    void CFloatingDlg::OnPaint()
    {
    	// CPaintDC dc(this); // device context for painting
    	// TODO: Add your message handler code here
    	// Do not call CDialog::OnPaint() for painting messages
    	if (IsIconic())
    	{
    		CPaintDC dc(this); // device context for painting
    
    		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
    
    		// Center icon in client rectangle
    		int cxIcon = GetSystemMetrics(SM_CXICON);
    		int cyIcon = GetSystemMetrics(SM_CYICON);
    		CRect rect;
    		GetClientRect(&rect);
    
    		int x = (rect.Width() - cxIcon + 1) / 2;
    		int y = (rect.Height() - cyIcon + 1) / 2;
    	}
    	else
    	{
            // our paint DC
            CPaintDC dc(this);
    
            // load up our bitmap into bmME
            CBitmap cbmME;
            
            // for resources use:
            //    cbmME.LoadBitmap(IDB_BITMAP_ME);
    
            // for files use this:
    		HBITMAP hBmp = (HBITMAP)::LoadImage(NULL, "preview.bmp", IMAGE_BITMAP,
                                                960, 540, LR_LOADFROMFILE | LR_CREATEDIBSECTION);
            if (hBmp == NULL)
            {
    			TRACE0("::LoadImage('preview.bmp') failed");
                return;
            }//if
           
    	    cbmME.Attach(hBmp);
            
            // draw to dialog
            CRect framerect;
    	this->GetWindowRect(framerect);
            ScreenToClient(framerect);
    
            // create a memory DC
            CDC dcMem;
    	    dcMem.CreateCompatibleDC(&dc);
    
            // select the bitmap into the memory dc
            CBitmap *old = dcMem.SelectObject(&cbmME);
    
            // get the raw bitmap for access to its attributes
            BITMAP bm;
            cbmME.GetBitmap(&bm);
    
            // blit it
            dc.BitBlt(framerect.left, framerect.top, 
                      bm.bmWidth, bm.bmHeight, 
                      &dcMem, 0, 0, SRCCOPY);
    
            dcMem.SelectObject(old);
            ::DeleteObject(hBmp);
    
    		CDialog::OnPaint();
    	}
    }
    note that I changed your code so that it was no longer attaching the bitmap to the static frame, but to the dialog itself. Maybe this is the problem? (I was just noticing your use of cbmMe which is not defined in my code)... hmm.... looking at it, I have no idea what cbmME is.
    Last edited by BrianK; 03-30-2004 at 10:41 PM.

  4. #4
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    Is it the title bar of the dialog that isn't redrawing properly?

    gg

  5. #5
    Registered User
    Join Date
    Oct 2002
    Posts
    72
    Quote Originally Posted by Codeplug
    Is it the title bar of the dialog that isn't redrawing properly?

    gg
    no, the image itself. It's rather large and resized (in half). If I drag a window over it, it does not redraw completly. see attached

  6. #6
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    Damn, is that a 6 meg bitmap I see?
    You're gonna want to optimize that drawing code a bit

    Add the following as member variables of the dialog:
    CBitmap cbmME;
    HBITMAP hBmp;
    CDC dcMem;
    CBitmap *old;
    int bmWidth, bmHeight;

    Do all the setup work in OnInitDialog(). When you call CreateCompatibleDC(x), call GetDC() for the x parameter (followed by a ReleaseDC()).

    The OnPaint() should then only have to a bitblt.

    Don't forget to release resources in the dialogs destructor (or OnDestroy()).

    gg

  7. #7
    Registered User
    Join Date
    Oct 2002
    Posts
    72
    Quote Originally Posted by Codeplug
    Damn, is that a 6 meg bitmap I see?
    You're gonna want to optimize that drawing code a bit
    Yeah, you should see the 16 bit per channel (48 bit per pixel) floating point image before it gets moved down to 8 bit per channel (24 bit per pixel) bitmap. Them's are clost to 16 MB per image & I have to deal with literally thousands of them. It's not what I'd call quick.

    Thanks a ton for the info. I'll give it a try when I get back to the office.

    btw, where is this info coming from? Is it just experience and/or can you recommend some online tutorials or a good book on the subject?

  8. #8
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    My first MFC book was this.
    Identifying areas for optimization comes somewhat from experience.

    gg

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with Bitmap Display
    By The Brain in forum Windows Programming
    Replies: 7
    Last Post: 03-23-2009, 05:33 AM
  2. OpenGL -- Bitmaps
    By HQSneaker in forum Game Programming
    Replies: 14
    Last Post: 09-06-2004, 04:04 PM
  3. 256-Color Bitmap in MFC Resource :: Doesn't Load Right
    By SyntaxBubble in forum Windows Programming
    Replies: 2
    Last Post: 09-16-2003, 03:55 PM
  4. CButton and Bitmap Interface :: MFC
    By kuphryn in forum Windows Programming
    Replies: 3
    Last Post: 02-21-2003, 02:36 PM
  5. WIndows programming?
    By hostensteffa in forum Windows Programming
    Replies: 7
    Last Post: 06-07-2002, 08:52 PM