troubleshooting Question

Trying to create Flicker free CStatic with Bitmap background

Avatar of microwhat
microwhat asked on
C++Visual C++.NETSystem Programming
8 Comments2 Solutions2805 ViewsLast Modified:
I have a transparent background CStatic text class(CStatic subclass). The CStatic txt is located in a dialog with a bitmap for a background. I'm trying to implement flicker free with this. Currently the code below is flickering less, but it's not flicker free. Also, i can't seem to get it to display the background as transparent with this flicker free code, this is something it did with no issue without the flicker free code. Any input is welcome. I've read/searched a lot and i'm just about stuck at this point.
BOOL CTransparentStatic::OnEraseBkgnd(CDC* pDC)
{
return TRUE;
}
void CTransparentStatic::OnPaint()
{
	CPaintDC dc(this); // device context for painting
	CDC      memDC;
	CBitmap  memBM;
	CBrush   hbrBkGnd;

	// Where to draw text
	CRect client_rect;
	GetClientRect(client_rect);

	// Get the caption
	CString szText;
	GetWindowText(szText);

	// Get the font
	CFont *pFont, *pOldFont;
	pFont = GetFont();


	
		// Create a compatible DC.
		memDC.CreateCompatibleDC(&dc);

		// Create a bitmap big enough for our client rectangle.
		memBM.CreateCompatibleBitmap(&dc,
										client_rect.right-client_rect.left,
										client_rect.bottom-client_rect.top);

		// Select the bitmap into the off-screen memory DC.
		CBitmap *hbmOld = memDC.SelectObject(&memBM);

		// Erase the background.
		hbrBkGnd.CreateSolidBrush(GetSysColor(COLOR_WINDOW));
		memDC.FillRect( &client_rect, &hbrBkGnd);
		hbrBkGnd.DeleteObject();

		// Select the font.
		if(pFont){
			pOldFont = memDC.SelectObject(pFont);
		}

		// Render the image into the offscreen DC.
		memDC.SetBkMode(TRANSPARENT);
		//memDC.SetTextColor(GetSysColor(COLOR_WINDOWTEXT));
		memDC.SetTextColor ( RGB(54,54,112) );
		memDC.DrawText(szText, client_rect, DT_LEFT);

		if(pOldFont){
			memDC.SelectObject(pOldFont);
		}

		// Blt the changes to the screen DC.
		dc.BitBlt(client_rect.left, client_rect.top,
			   client_rect.right-client_rect.left,
			   client_rect.bottom-client_rect.top,
			   &memDC,
			   0, 0,
			   SRCCOPY);

		// Done with off-screen bitmap and DC, clean up.
		memDC.SelectObject(hbmOld);
		memBM.DeleteObject();
		memDC.DeleteDC();
}
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 2 Answers and 8 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 2 Answers and 8 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros