CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Dec 2005
    Posts
    445

    Post Drawing text on a bitmap

    Hi all,

    I wrote a simple clock that shows the time over bitmap as follows:

    Code:
    void CMyClock::OnPaint()
    {
        CRect rect;
        GetClientRect(&rect);
    
        CPaintDC dc(this); // device context for painting
    
        CDC memDC;
        memDC.CreateCompatibleDC(&dc);
        memDC.SelectObject(m_bmpMyPic);
    
        memDC.TextOut(10,5, m_szCurrentTime);
    
        dc.BitBlt(0, 0 , 130, 39, &memDC, 0,0, SRCCOPY);
    }
    
    
    void CMyClock::OnTimer(UINT nIDEvent)
    {
        ....
        szCurrentTime = // The current time as a string
        Invalidate();       // Show the new time  
    }
    Everything works fine expect that I have white background border around my clock so I've modifyed the code (the change is bold):

    Code:
    void CMyClock::OnPaint()
    {
        CRect rect;
        GetClientRect(&rect);
    
        CPaintDC dc(this); // device context for painting
    
        CDC memDC;
        memDC.CreateCompatibleDC(&dc);
        memDC.SelectObject(m_bmpMyPic);
    
         pDC->SetBkMode(TRANSPARENT); 
        memDC.TextOut(10,5, m_szCurrentTime);
    
        dc.BitBlt(0, 0 , 130, 39, &memDC, 0,0, SRCCOPY);
    }
    Now I got rid off the white border but everytime I draw a text it write on top of the previous text so after few seconds everything is a mess and the time is no longer readable.

    Can anyone please tell me what am I doing wrong?

  2. #2
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,042

    Re: Drawing text on a bitmap

    Quote Originally Posted by Salvadoravi View Post
    Now I got rid off the white border but everytime I draw a text it write on top of the previous text so after few seconds everything is a mess and the time is no longer readable.

    Can anyone please tell me what am I doing wrong?
    Draw the background before you draw the text. You can use something like memDC.FillSolidRect(...).
    Cheers, D Drmmr

    Please put [code][/code] tags around your code to preserve indentation and make it more readable.

    As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky

  3. #3
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: Drawing text on a bitmap

    Well, it's a bitmap, so what would you expect? You have to "erase" the previous state before you draw the current one. So you have to do as D_Drmmr said, and repaint the rect where the text is drawn before you draw the text again.
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  4. #4
    Join Date
    Dec 2005
    Posts
    445

    Re: Drawing text on a bitmap

    Guys,

    I've tried,

    Code:
    CRect rect;
    GetClientRect(&rect);
    
    CPaintDC dc(this); // device context for painting
    
    CDC memDC;
    memDC.CreateCompatibleDC(&dc);
    memDC.FillSolidRect(rect, 0x0000FF);
    memDC.SelectObject(m_bmpMyPic);
    
    memDC.SetBkMode(TRANSPARENT); 
    memDC.TextOut(10,5, szCurrentTime);
    
    dc.BitBlt(0, 0 , 130, 39, &memDC, 0,0, SRCCOPY);
    I get the same result.

    Code:
    CRect rect;
    GetClientRect(&rect);
    
    CPaintDC dc(this); // device context for painting
    
    CDC memDC;
    memDC.CreateCompatibleDC(&dc);
    dc.FillSolidRect(rect, 0x0000FF);
    memDC.SelectObject(m_bmpMyPic);
    
    memDC.SetBkMode(TRANSPARENT); 
    memDC.TextOut(10,5, szCurrentTime);
    
    dc.BitBlt(0, 0 , 130, 39, &memDC, 0,0, SRCCOPY);
    Same problem + very bad flickering.

    Code:
    CRect rect;
    GetClientRect(&rect);
    
    CPaintDC dc(this); // device context for painting
    
    CDC memDC;
    memDC.CreateCompatibleDC(&dc);
    memDC.SelectObject(m_bmpMyPic);
    
    memDC.SetBkMode(TRANSPARENT);
    memDC.FillSolidRect(rect, 0x0000FF);
    memDC.TextOut(10,5, szCurrentTime);
    
    dc.BitBlt(0, 0 , 130, 39, &memDC, 0,0, SRCCOPY);
    This one works but the background is red and not a bitmap.

    Can you please guide me what goes wrong??

  5. #5
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,042

    Re: Drawing text on a bitmap

    Quote Originally Posted by Salvadoravi View Post
    This one works but the background is red and not a bitmap.

    Can you please guide me what goes wrong??
    It's red, because you painted it red. Please explain what you want to do.
    Cheers, D Drmmr

    Please put [code][/code] tags around your code to preserve indentation and make it more readable.

    As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky

  6. #6
    Join Date
    Nov 2003
    Location
    Belgium
    Posts
    8,150

    Re: Drawing text on a bitmap

    You need to create another bitmap, something as follows:
    - In your onpaint, create a temporary bitmap
    - blit your m_bmpMyPic bitmap to this temporary bitmap
    - draw the clock on this temporary bitmap
    - blit the temporary bitmap to the CPaintDC
    Marc Gregoire - NuonSoft (http://www.nuonsoft.com)
    My Blog
    Wallpaper Cycler 3.5.0.97

    Author of Professional C++, 4th Edition by Wiley/Wrox (includes C++17 features)
    ISBN: 978-1-119-42130-6
    [ http://www.facebook.com/professionalcpp ]

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured