Cannot paint into offscreen DC

This is a discussion on Cannot paint into offscreen DC within the Windows Programming forums, part of the Platform Specific Boards category; Hi, I have this code for drawing my custom control. I am going to do my drawing into an off-screen ...

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    34

    Cannot paint into offscreen DC

    Hi,

    I have this code for drawing my custom control. I am going to do my drawing into an off-screen DC and then blit it to the screen DC. However, it won't work. If i pass the screen DC to the Rectangle() function it will draw a rectangle, but when passing the memory DC it will not. I have done this many times before, but I can't get it to work in this particular case. Does it have anything to do with my OO approach?

    Edit: What I mean by "won't work" is that seemingly nothing gets drawn to the memory DC. No function calls fail either. I have checked return values and GetLastError(). I can for instance use the BLACKNESS raster operation on the BitBlt function and the control gets painted black.

    Code:
    LRESULT Button::OnPaint(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
    {
        PAINTSTRUCT ps;
        m_hdc = BeginPaint(hwnd,&ps);
    
        OffscreenBitmap(&ps);
    
        EndPaint(hwnd,&ps);
    
        return FALSE;
    }
    
    void Button::OffscreenBitmap(PAINTSTRUCT *ps)
    {
        HDC hdcMem = CreateCompatibleDC(ps->hdc);
        HBITMAP hbm = CreateCompatibleBitmap(ps->hdc,m_cx,m_cy);
        HBITMAP hbmOld;
    
        hbmOld = (HBITMAP)SelectObject(hdcMem,hbm);
    
        Rectangle(hdcMem,0,0,m_cx,m_cy);
    
        //DrawGradient(hdcMem);
        //LightenEdges(hdcMem);
    
        BitBlt(m_hdc,
            0,
            0,
            m_cx,
            m_cy,
            hdcMem,
            m_cx,
            m_cy,
            SRCCOPY
        );
    
        SelectObject(hdcMem,hbmOld);
    
        DeleteObject(hbm);
        DeleteDC(hdcMem);
    }
    Last edited by Eirik; 03-21-2011 at 01:38 PM.

  2. #2
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,235
    Are you sure you've got the correct pen selected into the memory DC?
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  3. #3
    Registered User
    Join Date
    Oct 2008
    Posts
    34
    Quote Originally Posted by brewbuck View Post
    Are you sure you've got the correct pen selected into the memory DC?
    I did not explicitly select a pen into the DC, though I tried that now when you mentioned it, but still the same result. I am also drawing a gradient in the DC (the commented out function) but this doesn't show up when I blit into the control's DC either... The exact same code works fine when drawing directly into the control's DC.
    Last edited by Eirik; 03-21-2011 at 02:45 PM. Reason: Typo

  4. #4
    'Allo, 'Allo, Allo
    Join Date
    Apr 2008
    Posts
    611
    Quote Originally Posted by Eirik View Post
    Code:
    BitBlt(m_hdc,
            0,
            0,
            m_cx,
            m_cy,
            hdcMem,
            m_cx,   // x-coordinate of source upper-left corner
            m_cy,   // y-coordinate of source upper-left corner
            SRCCOPY
        );
    Looks like you're trying to blit pixels m_cx -> m_cx * 2 and m_cy -> m_cy * 2 from the source dc instead of pixels 0 -> m_cx and 0 -> m_cy. The commented lines should be both be 0.

  5. #5
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,235
    Quote Originally Posted by adeyblue View Post
    Looks like you're trying to blit pixels m_cx -> m_cx * 2 and m_cy -> m_cy * 2 from the source dc instead of pixels 0 -> m_cx and 0 -> m_cy. The commented lines should be both be 0.
    Hmm. I believe you're right, but if that's true, why did he get a black rectangle when using a BLACKNESS rop? The blackness should have been confined to the destination rectangle, which as you point out, is off the edge of the bitmap... Weird.

    EDIT: Nevermind.. It's the source which is clipped, not the dest. I think you've basically explained the problem.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  6. #6
    Registered User
    Join Date
    Oct 2008
    Posts
    34
    Quote Originally Posted by adeyblue View Post
    Looks like you're trying to blit pixels m_cx -> m_cx * 2 and m_cy -> m_cy * 2 from the source dc instead of pixels 0 -> m_cx and 0 -> m_cy. The commented lines should be both be 0.
    Oh my! Thanks for spotting that adeyblue! It always helps to have a second pair of eyes to look at the code!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Screen DC vs. Printer DC
    By Kennedy in forum Windows Programming
    Replies: 2
    Last Post: 10-19-2006, 02:38 PM
  2. Win Api Basics...
    By Devil Panther in forum Windows Programming
    Replies: 19
    Last Post: 09-09-2004, 11:28 AM
  3. Smooth drawin on Paint DC
    By cfrost in forum C++ Programming
    Replies: 1
    Last Post: 07-11-2004, 04:27 PM
  4. screenshots before a window pops up?
    By MadHatter in forum Game Programming
    Replies: 13
    Last Post: 12-31-2002, 10:09 AM
  5. transferring GDI attributes.
    By Sebastiani in forum Windows Programming
    Replies: 4
    Last Post: 11-19-2002, 01:30 PM

Tags for this Thread


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