Click here to Skip to main content
11,581,186 members (63,681 online)
Click here to Skip to main content

CEdit & CStatic Transparency Control

, 6 Oct 2001 CPOL 155.9K 7K 63
Rate this:
Please Sign up or sign in to vote.
This acticle explain how to apply transparency on CEdit and CStatic Controls

Sample Image - CtrlTrans.gif


To apply transparency on CEdit control, create a new class derived from CEdit control and simply add these members.

In the new class .h file add.

// Attributes
    COLORREF    m_TextColor;
    COLORREF    m_BackColor;
    CBrush        m_Brush;
    // Operations
    void SetTextColor(COLORREF col) 
        m_TextColor = col;
    void SetBackColor(COLORREF col) 
        m_BackColor = col;                                                   
    void UpdateCtrl();
    // Generated message map functions
    afx_msg HBRUSH CtlColor(CDC* pDC, UINT nCtlColor);
    afx_msg void OnUpdate();
    afx_msg void OnLButtonDown(UINT nFlags, CPoint point);
    afx_msg void OnKillfocus();


In the new class .cpp file add.

    m_TextColor  = RGB(0, 0, 0);
    m_BackColor = TRANS_BACK;

HBRUSH CEditTrans::CtlColor(CDC* pDC, UINT nCtlColor) 

    if (m_BackColor == TRANS_BACK) {
    else {


    return (HBRUSH)m_Brush;

void CEditTrans::OnUpdate() 
void CEditTrans::UpdateCtrl()
    CWnd* pParent = GetParent();
    CRect   rect;

    rect.DeflateRect(2, 2);

    pParent->InvalidateRect(rect, FALSE); 

Implementation of the class

#include "EditTrans.h"

//Derived control from ClassWizard
CEditTrans m_edtTrans; 

//To make transparency

That all. Enjoy!


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Canada Canada

You may also be interested in...

Comments and Discussions

QuestionRedraw on old letters Pin
burak koprulu2-Dec-11 6:32
memberburak koprulu2-Dec-11 6:32 
QuestionMy version that works in windows 7 Pin
Earl K.26-Nov-11 3:42
memberEarl K.26-Nov-11 3:42 
QuestionCommon Control manifest in VC2005&2008 break the transparent effect Pin
marship2-Aug-11 2:19
membermarship2-Aug-11 2:19 
AnswerRe: Common Control manifest in VC2005&2008 break the transparent effect Pin
jeabrown28-Aug-11 8:56
memberjeabrown28-Aug-11 8:56 
QuestionUsing plain C++ gives me a black single line editcontrol. Multiline is OK Pin
jdekeij8-Aug-08 1:26
memberjdekeij8-Aug-08 1:26 
QuestionUrgent Please help Pin
Tushar Jadhav6-Jul-07 21:53
memberTushar Jadhav6-Jul-07 21:53 
QuestionLong string -> CEdit is dirty Pin
ritaivanova10-Apr-06 4:16
memberritaivanova10-Apr-06 4:16 
AnswerRe: Long string -> CEdit is dirty Pin
Gigi B.14-Jul-06 5:27
memberGigi B.14-Jul-06 5:27 
The problem with the dirt appears only when you select some part of the text and then you try to extend your selection to text that doesn' fit in the EditBox (in other words, when the text is scrolled).

Also there is another bug: let's say we have the following text:
123456. Select 456 from left to right, don't release the mouse button and select 123. You will se that 123456 is selected, instead of 123!

Here's a workaround:
add OnMouseMove message handler for the EditBox, and put there the following code:

void CEditTrans::OnMouseMove(UINT nFlags, CPoint point)
if(nFlags & MK_LBUTTON)

CEdit::OnMouseMove(nFlags, point);

there will be a small flickering, but the 2 bugs are solved!
GeneralChange font of CEdit Pin
vikas amin23-Sep-05 1:31
membervikas amin23-Sep-05 1:31 
GeneralMulti line edit Pin
Alex Evans8-May-05 20:21
memberAlex Evans8-May-05 20:21 
Generalcentering text in edit box Pin
vivadotnet18-Jun-04 13:34
membervivadotnet18-Jun-04 13:34 
Generalsuggestion Pin
Paolo Messina4-May-04 5:38
memberPaolo Messina4-May-04 5:38 
GeneralRe: suggestion Pin
MerlinYen11-Jan-06 18:57
memberMerlinYen11-Jan-06 18:57 
GeneralRe: suggestion Pin
Paolo Messina11-Jan-06 22:29
memberPaolo Messina11-Jan-06 22:29 
GeneralMulti-lined Edit and ListBox Transparency Controls Pin
Ray Livezey23-Jun-03 1:39
memberRay Livezey23-Jun-03 1:39 
Generalnice work . . but Pin
Skizmo7-Mar-03 1:47
memberSkizmo7-Mar-03 1:47 
GeneralRe: nice work . . but Pin
godelian9-Apr-03 20:51
membergodelian9-Apr-03 20:51 
GeneralRe: nice work . . but Pin
Paul Kissel5-Jun-03 14:27
memberPaul Kissel5-Jun-03 14:27 
GeneralRe: nice work . . but Pin
smjones22-Aug-03 4:43
membersmjones22-Aug-03 4:43 
Generalbug of edit trans Pin
Shuyi Vi25-Feb-03 14:49
memberShuyi Vi25-Feb-03 14:49 
GeneralRe: bug of edit trans Pin
MerlinYen11-Jan-06 19:04
memberMerlinYen11-Jan-06 19:04 
Generalthis doesn't work on Windows2000 Server Familiy Pin
Anonymous119-Aug-02 23:40
sussAnonymous119-Aug-02 23:40 
Generalthis doesn't work on WindowsXP Pin
alkee23-Jul-02 22:54
sussalkee23-Jul-02 22:54 
GeneralRe: this doesn't work on WindowsXP Pin
Anonymous25-Jul-02 11:23
sussAnonymous25-Jul-02 11:23 
GeneralRe: this doesn't work on WindowsXP Pin
Magetek21-Jan-03 5:39
memberMagetek21-Jan-03 5:39 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150603.1 | Last Updated 7 Oct 2001
Article Copyright 2001 by DCUtility
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid