Saturday, November 26, 2011

Creating a transparent textbox/edit control


First of all, the concept here is not originally mine but I have made a lot of changes to the original code. The original code can be seen here: http://www.codeguru.com/cpp/controls/editctrl/backgroundcolor/article.php/c6857

The code was written under MFC framework but what it does is pretty obvious. I mean, if you want to apply it in a non-MFC project, you already knew what to do.

Before I posted my modified code here, I made sure first that it works. I am using this code in a project I'm working on as of this posting.

My IDE is MS Visual Studio 2010. OS: Windows 7.

So, here we go.



// The MFC header class --> transedit.h

#pragma once

#pragma pack
(push, 1)

class TransEdit : public CEdit
{
    CBrush m_Brush;
    COLORREF m_clrText;
public:
    TransEdit();
    void SetTextColor(COLORREF clr);
    DECLARE_DYNCREATE(TransEdit)

private:
    void PrepareBackground();
    DECLARE_MESSAGE_MAP()

protected:
    //{{AFX_MSG(TransEdit)
    afx_msg HBRUSH CtlColor(CDC* pDC, UINT nCtlColor);
    //}}AFX_MSG
public:
    afx_msg void OnEnUpdate();
    afx_msg void OnSize(UINT nType, int cx, int cy);
};

#pragma pack(pop)



// The TransEdit implementation --> transedit.cpp

#include "stdafx.h"
#include "transedit.h"

BEGIN_MESSAGE_MAP(TransEdit, CEdit)
    ON_WM_CTLCOLOR_REFLECT()
    ON_CONTROL_REFLECT(EN_UPDATE, OnEnUpdate)
    ON_WM_SIZE()
END_MESSAGE_MAP()

IMPLEMENT_DYNCREATE(TransEdit, CEdit)


TransEdit::TransEdit()
{
    m_clrText = GetSysColor(COLOR_WINDOWTEXT);
}

void TransEdit::SetTextColor(COLORREF clr)
{
    m_clrText = clr;
}

HBRUSH TransEdit::CtlColor(CDC *pDC, UINT nCtlColor)
{
    pDC->SetBkMode(TRANSPARENT);
    pDC->SetTextColor(m_clrText);
    return (HBRUSH)m_Brush;
}

void TransEdit::PrepareBackground()
{
    if( m_Brush.m_hObject!=0 )
        return;

    CWnd * pParent = GetParent();
    CBitmap bkgnd;
    CClientDC dc(pParent);
    CDC thisMem;
    CRect pos, rc;
    GetWindowRect(pos);
    rc = pos;
    ScreenToClient(rc);
    pParent->ScreenToClient(pos);
    pos.InflateRect(rc.left, rc.top);

    CBitmap* pOldThisBmp;
    thisMem.CreateCompatibleDC( &dc );
    CSize sz = pos.Size();

    bkgnd.DeleteObject();
    bkgnd.CreateCompatibleBitmap(&dc, sz.cx, sz.cy );
    pOldThisBmp = thisMem.SelectObject(&bkgnd);
    thisMem.BitBlt(0, 0, sz.cx, sz.cy, &dc, pos.left, pos.top, SRCCOPY);
    thisMem.SelectObject(pOldThisBmp);
    m_Brush.DeleteObject();
    m_Brush.CreatePatternBrush(&bkgnd);
}

void TransEdit::OnEnUpdate()
{
    CRect rc;
    CWnd * pParent = GetParent();
    GetWindowRect(rc);
    pParent->ScreenToClient(rc);
    pParent->InvalidateRect(rc, 1);
    PrepareBackground();
}

void TransEdit::OnSize(UINT nType, int cx, int cy)
{
    m_Brush.DeleteObject();
    BOOL b = IsWindowVisible();
    CEdit::OnSize(nType, cx, cy);
    if( b )
        ShowWindow(SW_HIDE);
    OnEnUpdate();
    if( b )
        ShowWindow(SW_SHOW);
}



That’s all it!

If you find this useful, feel free to use/modify it. You want to claim it as your own? Go on, f@ck yourself!

No comments:

Post a Comment