Click here to Skip to main content
Click here to Skip to main content

CEdit & CStatic Transparency Control

By , 6 Oct 2001
 

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)

About the Author

DCUtility
Other DCUtility
Canada Canada
Member

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
QuestionRedraw on old letters [modified]memberburak koprulu2 Dec '11 - 6:32 
QuestionMy version that works in windows 7memberEarl K.26 Nov '11 - 3:42 
QuestionCommon Control manifest in VC2005&2008 break the transparent effectmembermarship2 Aug '11 - 2:19 
AnswerRe: Common Control manifest in VC2005&2008 break the transparent effectmemberjeabrown28 Aug '11 - 8:56 
QuestionUsing plain C++ gives me a black single line editcontrol. Multiline is OKmemberjdekeij8 Aug '08 - 1:26 
QuestionUrgent Please helpmemberTushar Jadhav6 Jul '07 - 21:53 
I tried to write code like you. and everything is working fine except one problem. In my application I am displaying a bitmap on a dialog and showing text on them. I have shown the text on transparent static controls as per your way. The text (Static control) can also be moved with the help of mouse. The problem is that when I partially place one static control over other, the topmost control displays the background image in the transparent area. Actually it should show the text of the static control which is below it and not the bitmap of the background. Can you please help me in this matter.
 
Tushar S. Jadhav

 
Nothing is IMPOSSIBLE. Even the word IMPOSSIBLE says I-M-POSSIBLE.
QuestionLong string -> CEdit is dirtymemberritaivanova10 Apr '06 - 4:16 
AnswerRe: Long string -> CEdit is dirtymemberGigi B.14 Jul '06 - 5:27 
GeneralChange font of CEditmembervikas amin23 Sep '05 - 1:31 
GeneralMulti line editmemberAlex Evans8 May '05 - 20:21 
Generalcentering text in edit boxmembervivadotnet18 Jun '04 - 13:34 
GeneralsuggestionmemberPaolo Messina4 May '04 - 5:38 
GeneralRe: suggestionmemberMerlinYen11 Jan '06 - 18:57 
GeneralRe: suggestionmemberPaolo Messina11 Jan '06 - 22:29 
GeneralMulti-lined Edit and ListBox Transparency ControlsmemberRay Livezey23 Jun '03 - 1:39 
Generalnice work . . butmemberSkizmo7 Mar '03 - 1:47 
GeneralRe: nice work . . butmembergodelian9 Apr '03 - 20:51 
GeneralRe: nice work . . butmemberPaul Kissel5 Jun '03 - 14:27 
GeneralRe: nice work . . butmembersmjones22 Aug '03 - 4:43 
Generalbug of edit transmemberShuyi Vi25 Feb '03 - 14:49 
GeneralRe: bug of edit transmemberMerlinYen11 Jan '06 - 19:04 
Generalthis doesn't work on Windows2000 Server FamiliysussAnonymous119 Aug '02 - 23:40 
Generalthis doesn't work on WindowsXPsussalkee23 Jul '02 - 22:54 
GeneralRe: this doesn't work on WindowsXPsussAnonymous25 Jul '02 - 11:23 
GeneralRe: this doesn't work on WindowsXPmemberMagetek21 Jan '03 - 5:39 

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

Permalink | Advertise | Privacy | Mobile
Web01 | 2.6.130516.1 | Last Updated 7 Oct 2001
Article Copyright 2001 by DCUtility
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid