Click here to Skip to main content
Licence CPOL
First Posted 6 Oct 2001
Views 131,162
Downloads 4,155
Bookmarked 55 times

CEdit & CStatic Transparency Control

By DCUtility | 6 Oct 2001
This acticle explain how to apply transparency on CEdit and CStatic Controls
3 votes, 15.0%
1

2
2 votes, 10.0%
3
3 votes, 15.0%
4
12 votes, 60.0%
5
4.50/5 - 29 votes
3 removed
μ 4.13, σa 2.57 [?]

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 koprulu7:32 2 Dec '11  
QuestionMy version that works in windows 7 PinmemberEarl K.4:42 26 Nov '11  
QuestionCommon Control manifest in VC2005&2008 break the transparent effect Pinmembermarship3:19 2 Aug '11  
AnswerRe: Common Control manifest in VC2005&2008 break the transparent effect Pinmemberjeabrown9:56 28 Aug '11  
QuestionUsing plain C++ gives me a black single line editcontrol. Multiline is OK Pinmemberjdekeij2:26 8 Aug '08  
QuestionUrgent Please help PinmemberTushar Jadhav22:53 6 Jul '07  
QuestionLong string -> CEdit is dirty Pinmemberritaivanova5:16 10 Apr '06  
AnswerRe: Long string -> CEdit is dirty PinmemberGigi B.6:27 14 Jul '06  
GeneralChange font of CEdit Pinmembervikas amin2:31 23 Sep '05  
GeneralMulti line edit PinmemberAlex Evans21:21 8 May '05  
Generalcentering text in edit box Pinmembervivadotnet14:34 18 Jun '04  
Generalsuggestion PinmemberPaolo Messina6:38 4 May '04  
GeneralRe: suggestion PinmemberMerlinYen19:57 11 Jan '06  
GeneralRe: suggestion PinmemberPaolo Messina23:29 11 Jan '06  
GeneralMulti-lined Edit and ListBox Transparency Controls PinmemberRay Livezey2:39 23 Jun '03  
Generalnice work . . but PinmemberSkizmo2:47 7 Mar '03  
GeneralRe: nice work . . but Pinmembergodelian21:51 9 Apr '03  
GeneralRe: nice work . . but PinmemberPaul Kissel15:27 5 Jun '03  
GeneralRe: nice work . . but Pinmembersmjones5:43 22 Aug '03  
Generalbug of edit trans PinmemberShuyi Vi15:49 25 Feb '03  
GeneralRe: bug of edit trans PinmemberMerlinYen20:04 11 Jan '06  
Generalthis doesn't work on Windows2000 Server Familiy PinsussAnonymous10:40 20 Aug '02  
Generalthis doesn't work on WindowsXP Pinsussalkee23:54 23 Jul '02  
GeneralRe: this doesn't work on WindowsXP PinsussAnonymous12:23 25 Jul '02  
GeneralRe: this doesn't work on WindowsXP PinmemberMagetek6: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
Web01 | 2.5.120210.1 | Last Updated 7 Oct 2001
Article Copyright 2001 by DCUtility
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid