-
October 5th, 2012, 06:36 AM
#1
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?
-
October 5th, 2012, 07:01 AM
#2
Re: Drawing text on a bitmap
Originally Posted by Salvadoravi
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
-
October 5th, 2012, 08:00 AM
#3
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.
-
October 5th, 2012, 08:42 AM
#4
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??
-
October 6th, 2012, 06:51 PM
#5
Re: Drawing text on a bitmap
Originally Posted by Salvadoravi
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
-
October 9th, 2012, 01:55 AM
#6
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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|