Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

CEdit & CStatic Transparency Control

0.00/5 (No votes)
6 Oct 2001 1  
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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here