Transparent Edit Using a Pattern Background Brush

Friday Apr 2nd 2004 by Jose Reyes

This article explains how to create a transparent edit control. Instead of using a NULL_BRUSH for the CtlColor message, we create a pattern brush using a chunk of bitmap from the parent window from the place where the editor is going to be shown.

While working on a project that needed a transparent edit control, I found an article from Duncan Weir on this Web site about editor background colors. To see the article, click here

In this article, to make an editor transparent, a NULL_BRUSH was created and returned from the OnCtlColor message handler. This idea was very simple, but when I tried to use it, I found that the blue rectangle of text selection remained in the background after the text was unselected.

So, instead of creating a NULL_BRUSH, I created a pattern brush by using a chunk of bitmap from where the edit control is going to be shown. That is, instead of using this:

void CInPlaceEdit::PrepareBackground( CRect pos )

I did this instead:

void CInPlaceEdit::PrepareBackground( CRect pos )
   CClientDC thisDC( this );
   CClientDC parentDC( GetParent() );
   CDC thisMem;

   INT OldParentMapMode = parentDC.SetMapMode( MM_LOENGLISH );

   CBitmap* pOldThisBmp;

   thisMem.CreateCompatibleDC( &thisDC );
   INT OldThisMapMode = thisMem.SetMapMode( MM_LOENGLISH );

   CSize sz = pos.Size();

   m_Background.CreateCompatibleBitmap( &thisDC,, );

   pOldThisBmp = thisMem.SelectObject( &m_Background );

   thisMem.BitBlt( 0, 0,,, &parentDC, pos.left,
         , SRCCOPY );

   thisMem.SetMapMode( OldThisMapMode );
   thisMem.SelectObject( pOldThisBmp );

   parentDC.SetMapMode( OldParentMapMode );


   m_Brush.CreatePatternBrush( &m_Background );
   if ( m_Brush.m_hObject != NULL )

and then this:

HBRUSH CInPlaceEdit::CtlColor(CDC* pDC, UINT nCtlColor)
   pDC->SelectObject( &m_Brush );

   pDC->SetBrushOrg( 0, 0 );


   return (HBRUSH)m_Brush;
Note: The PrepareBackgound method must be called before showing the edit control. I have created an small sample application to ilustrate that approach. See the code for details.

I hope this can help somebody.


Mobile Site | Full Site
Copyright 2017 © QuinStreet Inc. All Rights Reserved