Click here to Skip to main content
Licence CPOL
First Posted 6 Oct 2001
Views 134,415
Bookmarked 56 times

CEdit & CStatic Transparency Control

By | 6 Oct 2001 | Article
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)

About the Author

DCUtility

Other
DCUtility
Canada Canada

Member

Home Page

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

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionRedraw on old letters [modified] Pinmemberburak koprulu6:32 2 Dec '11  
QuestionMy version that works in windows 7 PinmemberEarl K.3:42 26 Nov '11  
QuestionCommon Control manifest in VC2005&2008 break the transparent effect Pinmembermarship2:19 2 Aug '11  
AnswerRe: Common Control manifest in VC2005&2008 break the transparent effect Pinmemberjeabrown8:56 28 Aug '11  
QuestionUsing plain C++ gives me a black single line editcontrol. Multiline is OK Pinmemberjdekeij1:26 8 Aug '08  
QuestionUrgent Please help PinmemberTushar Jadhav21:53 6 Jul '07  
QuestionLong string -> CEdit is dirty Pinmemberritaivanova4:16 10 Apr '06  
If an user writes more simbols than the CEdit length, we see a dirt.
Please, help to fix it!

AnswerRe: Long string -> CEdit is dirty PinmemberGigi B.5:27 14 Jul '06  
GeneralChange font of CEdit Pinmembervikas amin1:31 23 Sep '05  
GeneralMulti line edit PinmemberAlex Evans20:21 8 May '05  
Generalcentering text in edit box Pinmembervivadotnet13:34 18 Jun '04  
Generalsuggestion PinmemberPaolo Messina5:38 4 May '04  
GeneralRe: suggestion PinmemberMerlinYen18:57 11 Jan '06  
GeneralRe: suggestion PinmemberPaolo Messina22:29 11 Jan '06  
GeneralMulti-lined Edit and ListBox Transparency Controls PinmemberRay Livezey1:39 23 Jun '03  
Generalnice work . . but PinmemberSkizmo1:47 7 Mar '03  
GeneralRe: nice work . . but Pinmembergodelian20:51 9 Apr '03  
GeneralRe: nice work . . but PinmemberPaul Kissel14:27 5 Jun '03  
GeneralRe: nice work . . but Pinmembersmjones4:43 22 Aug '03  
Generalbug of edit trans PinmemberShuyi Vi14:49 25 Feb '03  
GeneralRe: bug of edit trans PinmemberMerlinYen19:04 11 Jan '06  
Generalthis doesn't work on Windows2000 Server Familiy PinsussAnonymous123:40 19 Aug '02  
Generalthis doesn't work on WindowsXP Pinsussalkee22:54 23 Jul '02  
GeneralRe: this doesn't work on WindowsXP PinsussAnonymous11:23 25 Jul '02  
GeneralRe: this doesn't work on WindowsXP PinmemberMagetek5:39 21 Jan '03  

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.

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