Click here to Skip to main content
15,861,168 members
Articles / Desktop Programming / MFC
Article

CEdit & CStatic Transparency Control

Rate me:
Please Sign up or sign in to vote.
4.58/5 (24 votes)
6 Oct 2001CPOL 201.4K   9K   67   30
This acticle explain how to apply transparency on CEdit and CStatic Controls

Sample Image - CtrlTrans.gif

Introduction

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
private:
    COLORREF    m_TextColor;
    COLORREF    m_BackColor;
    CBrush        m_Brush;
    // Operations
public:
    void SetTextColor(COLORREF col) 
    { 
        m_TextColor = col;
        UpdateCtrl();             
    }
    void SetBackColor(COLORREF col) 
    { 
        m_BackColor = col;                                                   
        UpdateCtrl();            
    }
private:
    void UpdateCtrl();
    // Generated message map functions
protected:
    //{{AFX_MSG(CEditTrans)
    afx_msg HBRUSH CtlColor(CDC* pDC, UINT nCtlColor);
    afx_msg void OnUpdate();
    afx_msg void OnLButtonDown(UINT nFlags, CPoint point);
    afx_msg void OnKillfocus();
    //}}AFX_MSG

    DECLARE_MESSAGE_MAP()

In the new class .cpp file add.

CEditTrans::CEditTrans()
{
    m_TextColor  = RGB(0, 0, 0);
    m_BackColor = TRANS_BACK;
}
BEGIN_MESSAGE_MAP(CEditTrans, CEdit)
    //{{AFX_MSG_MAP(CEditTrans)
    ON_WM_CTLCOLOR_REFLECT()
    ON_CONTROL_REFLECT(EN_UPDATE, OnUpdate)
    ON_WM_LBUTTONDOWN()
    ON_CONTROL_REFLECT(EN_KILLFOCUS, OnKillfocus)
    //}}AFX_MSG_MAP
END_MESSAGE_MAP()

HBRUSH CEditTrans::CtlColor(CDC* pDC, UINT nCtlColor) 
{
    m_Brush.DeleteObject();

    if (m_BackColor == TRANS_BACK) {
        m_Brush.CreateStockObject(HOLLOW_BRUSH);
        pDC->SetBkMode(TRANSPARENT);
    }
    else {
        m_Brush.CreateSolidBrush(m_BackColor);
        pDC->SetBkColor(m_BackColor);
    }

    pDC->SetTextColor(m_TextColor);

    return (HBRUSH)m_Brush;
}

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

    GetWindowRect(rect);
    pParent->ScreenToClient(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
m_edtTrans.SetBackColor(TRANS_BACK); 

That all. Enjoy!

License

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


Written By
CEO
Canada Canada

Comments and Discussions

 
QuestionRedraw on old letters Pin
burak koprulu2-Dec-11 6:32
burak koprulu2-Dec-11 6:32 
QuestionMy version that works in windows 7 Pin
Earl K.26-Nov-11 3:42
Earl K.26-Nov-11 3:42 
QuestionCommon Control manifest in VC2005&2008 break the transparent effect Pin
marship2-Aug-11 2:19
marship2-Aug-11 2:19 
AnswerRe: Common Control manifest in VC2005&2008 break the transparent effect Pin
jeabrown28-Aug-11 8:56
jeabrown28-Aug-11 8:56 
QuestionUsing plain C++ gives me a black single line editcontrol. Multiline is OK Pin
jdekeij8-Aug-08 1:26
jdekeij8-Aug-08 1:26 
QuestionUrgent Please help Pin
Tushar Jadhav6-Jul-07 21:53
Tushar Jadhav6-Jul-07 21:53 
QuestionLong string -> CEdit is dirty Pin
ritaivanova10-Apr-06 4:16
ritaivanova10-Apr-06 4:16 
AnswerRe: Long string -> CEdit is dirty Pin
Gigi B.14-Jul-06 5:27
Gigi B.14-Jul-06 5:27 
GeneralChange font of CEdit Pin
vikas amin23-Sep-05 1:31
vikas amin23-Sep-05 1:31 
GeneralMulti line edit Pin
Alex Evans8-May-05 20:21
Alex Evans8-May-05 20:21 
Generalcentering text in edit box Pin
vivadot18-Jun-04 13:34
vivadot18-Jun-04 13:34 
Generalsuggestion Pin
Paolo Messina4-May-04 5:38
professionalPaolo Messina4-May-04 5:38 
GeneralRe: suggestion Pin
MerlinYen11-Jan-06 18:57
MerlinYen11-Jan-06 18:57 
GeneralRe: suggestion Pin
Paolo Messina11-Jan-06 22:29
professionalPaolo Messina11-Jan-06 22:29 
GeneralMulti-lined Edit and ListBox Transparency Controls Pin
Member 44154923-Jun-03 1:39
Member 44154923-Jun-03 1:39 
Generalnice work . . but Pin
Skizmo7-Mar-03 1:47
Skizmo7-Mar-03 1:47 
GeneralRe: nice work . . but Pin
godelian9-Apr-03 20:51
godelian9-Apr-03 20:51 
GeneralRe: nice work . . but Pin
Paul Kissel5-Jun-03 14:27
Paul Kissel5-Jun-03 14:27 
GeneralRe: nice work . . but Pin
smjones22-Aug-03 4:43
smjones22-Aug-03 4:43 
Generalbug of edit trans Pin
Shuyi Vi25-Feb-03 14:49
Shuyi Vi25-Feb-03 14:49 
GeneralRe: bug of edit trans Pin
MerlinYen11-Jan-06 19:04
MerlinYen11-Jan-06 19:04 
Generalthis doesn't work on Windows2000 Server Familiy Pin
anonymous119-Aug-02 23:40
anonymous119-Aug-02 23:40 
Generalthis doesn't work on WindowsXP Pin
alkee23-Jul-02 22:54
alkee23-Jul-02 22:54 
hey~

static control works good , but edit control doesn't work on window xp
Poke tongue | ;-P
GeneralRe: this doesn't work on WindowsXP Pin
Anonymous25-Jul-02 11:23
Anonymous25-Jul-02 11:23 
GeneralRe: this doesn't work on WindowsXP Pin
Magetek21-Jan-03 5:39
Magetek21-Jan-03 5:39 

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

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