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):
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.
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
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.
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
Bookmarks