Re: How to transparent editbox control works correctly...

From: AliR (AliR_at_mailserver.com)
Date: 12/01/04


Date: Wed, 01 Dec 2004 16:35:00 GMT

Unfortunately what you are trying to do is not going to give you what you
want. You can't simply override OnCtrlColor and expect everything to work
correctly. Since you are trying to do something none standard then some of
the standard ways of doing things will not work. Here is why, since when the
edit controls text changes then background will have to be updated then you
can't simply attach a CString to your edit control and hope that UpdateData
takes care of things.
For this to work correctly you will have to inherit from a CEdit class, and
within that class override OnCtrlColor and OnEraseBkgrnd to do your
transparent stuff, and then you will also have to take over anything that
changes the text of the edit control, like SetWindowText, OnKeyDown,
OnKeyUp, and so forth so that you can update what is under the edit control
at the time the changes happen. With all of that said, you will need to use
m_MyEdit.SetWindowText instead of UpdateData! That's because DDX_Text
simpley calls the system SetWindowText(hWnd,Text,...); which in turn sends a
WM_SETTEXT message to your edit contorl, and that doesn't help much. Why you
ask is because if you catch the WM_SETTEXT message in order to update what's
in the background then the text for the edit control will never get set
since the WM_SETTEXT message is never going to get to the control itself. I
hope all of these is making sence. Now here is what you have to do get rid
of the DDX_Text compeletly. And tie a CTransparnetEdit control to the
variable by using DDX_Control. And instead of calling UpdateData use
m_MyEdit.SetWindowText(Text);
Here is the code for the CTransparentEdit, (It has a little more than what
you need but it does the trick)

#if
!defined(AFX_CTransparentEdit_H__BE153505_6D27_11D4_8CDF_0008C73F82B8__INCLU
DED_)

#define
AFX_CTransparentEdit_H__BE153505_6D27_11D4_8CDF_0008C73F82B8__INCLUDED_

#if _MSC_VER > 1000

#pragma once

#endif // _MSC_VER > 1000

// CTransparentEdit.h : header file

//

////////////////////////////////////////////////////////////////////////////
/

// CTransparentEdit window

#define WM_EDITGOTRETURN WM_USER+100

#define WM_EDITGOTESC WM_USER+101

class CTransparentEdit : public CEdit

{

// Construction

public:

CTransparentEdit();

// Attributes

public:

// Operations

public:

void SetWindowText( LPCTSTR lpszString );

void SetFont(int nPointSize, CString FaceName,COLORREF Color);

void SetNumberOnly(BOOL Number);

virtual BOOL PreTranslateMessage( MSG* pMsg );

// Overrides

// ClassWizard generated virtual function overrides

//{{AFX_VIRTUAL(CTransparentEdit)

protected:

virtual BOOL PreCreateWindow(CREATESTRUCT& cs);

virtual void PreSubclassWindow();

//}}AFX_VIRTUAL

// Implementation

public:

virtual ~CTransparentEdit();

// Generated message map functions

protected:

CRect m_Rect;

CWnd *m_pParent;

CFont m_Font;

COLORREF m_Color;

CString m_ForbidenChars;

BOOL m_NumberOnly;

//{{AFX_MSG(CTransparentEdit)

afx_msg BOOL OnEraseBkgnd(CDC* pDC);

afx_msg HBRUSH CtlColor(CDC* pDC, UINT nCtlColor);

afx_msg void OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags);

afx_msg void OnKeyUp(UINT nChar, UINT nRepCnt, UINT nFlags);

afx_msg void OnKillFocus( CWnd* pNewWnd );

afx_msg void OnPaint();

afx_msg void OnChar(UINT nChar, UINT nRepCnt, UINT nFlags);

//}}AFX_MSG

DECLARE_MESSAGE_MAP()

};

////////////////////////////////////////////////////////////////////////////
/

//{{AFX_INSERT_LOCATION}}

// Microsoft Visual C++ will insert additional declarations immediately
before the previous line.

#endif //
!defined(AFX_CTransparentEdit_H__BE153505_6D27_11D4_8CDF_0008C73F82B8__INCLU
DED_)

// CTransparentEdit.cpp : implementation file

//

#include "stdafx.h"

#include "CTransparentEdit.h"

#ifdef _DEBUG

#define new DEBUG_NEW

#undef THIS_FILE

static char THIS_FILE[] = __FILE__;

#endif

////////////////////////////////////////////////////////////////////////////
/

// CTransparentEdit

CTransparentEdit::CTransparentEdit()

:m_Color(RGB(255,255,255)),

m_pParent(NULL),

m_NumberOnly(FALSE)

{

m_ForbidenChars = "\'=";

}

CTransparentEdit::~CTransparentEdit()

{

}

BEGIN_MESSAGE_MAP(CTransparentEdit, CEdit)

//{{AFX_MSG_MAP(CTransparentEdit)

ON_WM_ERASEBKGND()

ON_WM_CTLCOLOR_REFLECT()

ON_WM_KEYDOWN()

ON_WM_KEYUP()

ON_WM_KILLFOCUS()

ON_WM_PAINT()

ON_WM_CHAR()

//}}AFX_MSG_MAP

END_MESSAGE_MAP()

////////////////////////////////////////////////////////////////////////////
/

// CTransparentEdit message handlers

void CTransparentEdit::SetNumberOnly(BOOL Number)

{

m_NumberOnly = Number;

}

BOOL CTransparentEdit::PreCreateWindow(CREATESTRUCT& cs)

{

BOOL ret = CEdit::PreCreateWindow(cs);

return ret;

}

void CTransparentEdit::PreSubclassWindow()

{

CEdit::PreSubclassWindow();

}

BOOL CTransparentEdit::OnEraseBkgnd(CDC* pDC)

{

return TRUE;

}

HBRUSH CTransparentEdit::CtlColor(CDC* pDC, UINT nCtlColor)

{

pDC->SetTextColor(m_Color);

pDC->SetBkMode(TRANSPARENT);

HBRUSH brush = (HBRUSH)GetStockObject(HOLLOW_BRUSH);

return brush;

}

void CTransparentEdit::SetFont(int nPointSize, CString FaceName,COLORREF
Color)

{

m_Color = Color;

m_Font.DeleteObject();

m_Font.CreateFont(nPointSize,0,0,0,FW_BOLD,FALSE,FALSE,FALSE,ANSI_CHARSET,OU
T_CHARACTER_PRECIS,CLIP_CHARACTER_PRECIS,PROOF_QUALITY,DEFAULT_PITCH,FaceNam
e);

CEdit::SetFont(&m_Font);

}

void CTransparentEdit::SetWindowText( LPCTSTR lpszString )

{

CEdit::SetWindowText(lpszString);

m_pParent = GetParent();

if (m_pParent)

{

GetClientRect(&m_Rect);

ClientToScreen(&m_Rect);

m_pParent->ScreenToClient(&m_Rect);

m_pParent->InvalidateRect(&m_Rect);

m_pParent->UpdateWindow();

}

}

void CTransparentEdit::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)

{

CEdit::OnKeyDown(nChar, nRepCnt, nFlags);

m_pParent = GetParent();

if (m_pParent)

{

GetClientRect(&m_Rect);

ClientToScreen(&m_Rect);

m_pParent->ScreenToClient(&m_Rect);

m_pParent->InvalidateRect(&m_Rect);

m_pParent->UpdateWindow();

}

}

void CTransparentEdit::OnKeyUp(UINT nChar, UINT nRepCnt, UINT nFlags)

{

m_pParent = GetParent();

if (m_pParent)

{

GetClientRect(&m_Rect);

ClientToScreen(&m_Rect);

m_pParent->ScreenToClient(&m_Rect);

m_pParent->InvalidateRect(&m_Rect);

m_pParent->UpdateWindow();

}

CEdit::OnKeyUp(nChar, nRepCnt, nFlags);

}

/*

void CTransparentEdit::OnSetFocus(CWnd *pOldWnd)

{

CEdit::OnKillFocus(pNewWnd);

}

*/

void CTransparentEdit::OnKillFocus( CWnd* pNewWnd )

{

CEdit::OnKillFocus(pNewWnd);

m_pParent = GetParent();

if (m_pParent)

{

GetClientRect(&m_Rect);

ClientToScreen(&m_Rect);

m_pParent->ScreenToClient(&m_Rect);

m_pParent->InvalidateRect(&m_Rect);

m_pParent->UpdateWindow();

}

}

void CTransparentEdit::OnPaint()

{

CEdit::OnPaint();

}

BOOL CTransparentEdit::PreTranslateMessage( MSG* pMsg )

{

if (pMsg->message == WM_KEYDOWN)

{

if (pMsg->wParam == VK_RETURN)

{

m_pParent = GetParent();

if (m_pParent)

{

m_pParent->SendMessage(WM_EDITGOTRETURN,0,(long)m_hWnd);

return TRUE;

}

}

else if (pMsg->wParam == VK_ESCAPE)

{

m_pParent = GetParent();

if (m_pParent)

{

m_pParent->SendMessage(WM_EDITGOTESC,0,(long)m_hWnd);

return TRUE;

}

}

}

return CEdit::PreTranslateMessage(pMsg);

}

void CTransparentEdit::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)

{

if (m_NumberOnly && !(isdigit(nChar) || nChar == '-'))

{

return;

}

if (m_ForbidenChars.Find(nChar) == -1)

{

CEdit::OnChar(nChar, nRepCnt, nFlags);

}

}

"Mujtaba" <tomujtaba@hotmail.com> wrote in message
news:8f4f92aa.0411302156.523af565@posting.google.com...
> Thanks...
>
> But this does not solve my problem.
>
> did you yourself try it or only you are guessing?
>
> Let check it one thing if you know something about transparent edit
> control, make a simple MFC dialog, place a picture control and
> assignet it a picture as a resource. Now place an edit control from
> controls bar.
>
> Now in your application Dialog class capture OnCtlColor event and
> write this code there...
>
> if(pWnd->GetDlgCtrlID() == IDC_EDT_Time ) //IDC_EDT_Time this is ID of
> //edit control
> {
> pDC->SetBkMode(TRANSPARENT);
> hbr = (HBRUSH)GetStockObject(NULL_BRUSH);
> }
>
>
>
> Attach a string type variable with edit box control (say m_str), then
> place a button on dialog with code
>
> m_str = "check this";
> UpdateData(FALSE);
>
>
> Now run your application
>
> press button
>
> your will see this text in edit control with transparent background.
>
> Now select the text in the edit box control and overwrite it through
> keyboard.
>
> Now check whether its new text or it is overwritting it with previous
> one.
>
> My problem is that i want new text, did you understand.
>
> Now if you try it and have any solution please share with me.
>
>
> Mujtaba
>
>
>
>
>
>
> "AliR" <AliR@mailserver.com> wrote in message
news:<uA1rd.25170$fC4.5860@newssvr11.news.prodigy.com>...
> > How about this!!!!
> >
> > void LSTransparentEdit::SetWindowText( LPCTSTR lpszString )
> >
> > {
> >
> > CEdit::SetWindowText(lpszString);
> >
> > CWnd *pParent = GetParent();
> >
> > if (pParent)
> >
> > {
> >
> > GetClientRect(&m_Rect);
> >
> > ClientToScreen(&m_Rect);
> >
> > pParent->ScreenToClient(&m_Rect);
> >
> > pParent->InvalidateRect(&m_Rect);
> >
> > pParent->UpdateWindow();
> >
> > }
> >
> > }
> >
> >
> >
> > "Mujtaba" <tomujtaba@hotmail.com> wrote in message
> > news:8f4f92aa.0411300457.3b9cbf56@posting.google.com...
> > > Thanks for your reply...
> > >
> > > But this does not work. Actually i have mentioned my problem in the
> > > question that i am getting this edit box values from Timer which
> > > displays current time. I am not manualy updating the edit box values,
> > > only changed by timer.
> > >
> > >
> > > so i could not use your code. Could you please help me out.
> > >
> > > Mujtaba
> > >
> > >
> > >
> > > "AliR" <AliR@mailserver.com> wrote in message
> > news:<6NHqd.38718$Al3.22575@newssvr30.news.prodigy.com>...
> > > > See if these will help solve your problem. See comments in the code
> > below
> > > >
> > > > void LSTransparentEdit::OnKeyDown(UINT nChar, UINT nRepCnt, UINT
nFlags)
> > > > {
> > > > CEdit::OnKeyDown(nChar, nRepCnt, nFlags);
> > > > CWnd *pParent = GetParent();
> > > > if (pParent)
> > > > {
> > > > GetClientRect(&m_Rect); //get the edit
controls
> > > > size
> > > > ClientToScreen(&m_Rect); //change to screen
> > > > corrdinates
> > > > pParent->ScreenToClient(&m_Rect); //change to parent
corrdinates
> > > > pParent->InvalidateRect(&m_Rect); //force parent to redraw
> > what's
> > > > under the edit control
> > > > pParent->UpdateWindow();
> > > > }
> >
> > > > }
> > > >
> > > >
> > > > void LSTransparentEdit::OnKeyUp(UINT nChar, UINT nRepCnt, UINT
nFlags)
> > > > {
> > > > CWnd *pParent = GetParent();
> > > > if (pParent)
> > > > {
> > > > GetClientRect(&m_Rect);
> > > > ClientToScreen(&m_Rect);
> > > > pParent->ScreenToClient(&m_Rect);
> > > > pParent->InvalidateRect(&m_Rect);
> > > > pParent->UpdateWindow();
> > > > }
> > > >
> > > > CEdit::OnKeyUp(nChar, nRepCnt, nFlags);
> > > > }
> > > >
> > > > AliR.
> > > >
> > > > "Mujtaba" <tomujtaba@hotmail.com> wrote in message
> > > > news:8f4f92aa.0411290610.658ec872@posting.google.com...
> > > > > Hi
> > > > >
> > > > > i am working on an application where i have dialog based
application.
> > > > > On dialog there is a picture control which displays a picture. Now
i
> > > > > want to put an edit box. Text box string is current time which i
am
> > > > > getting from OnTimer handler.
> > > > >
> > > > >
> > > > > i want that editbox control should be transparent except text in
it.
> > > > >
> > > > > for this i have work with following code and get succeed
> > > > >
> > > > > in OnCtlHandler of dialog
> > > > >
> > > > > if(pWnd->GetDlgCtrlID() == IDC_EDT_Isha)
> > > > > {
> > > > > pDC->SetBkMode(TRANSPARENT);
> > > > > hbr = (HBRUSH)GetStockObject(NULL_BRUSH);
> > > > >
> > > > > }
> > > > >
> > > > >
> > > > > it makes the backgound tranparent but when the value of the edit
box
> > > > > changes it write it over the previous one- means over-written both
> > > > > texts.
> > > > >
> > > > >
> > > > > Could anyboby please help me out.
> > > > >
> > > > > Thanks in advance
> > > > >
> > > > > Mujtaba


Loading