大家来分析分析这个类是怎样工作的?(CEditEx)我编译它成功,但总觉得它怪怪的!
// EditEx.hxx : header file
//
// Copyright (C) 1996 Bill Berry All rights reserved.
//
// This class is freely distributable as long as the copyright accompanies
// the header and implementaion sources.
//
// Also, please send me any improvements or bug fixes to bberry@javanet.com
//
// Modification History:
//
// Bill Berry November 1996 Created
// March 1998 Update
//
// Description:
//
// Extended CEdit class. Allows easy customization of the following:
//
// 1. COLORREF bkColor( COLORREF crColor )
// - Sets back ground color of the control
//
// 2. COLORREF bkColor()
// - Returns back ground color of control
//
// 3. COLORREF textColor( COLORREF crColor )
// - Sets text or foreground color of the control
//
// 4. COLORREF textColor() const
// - Returns text (or foreground) color of control
//
// 5. void setCustomMask( CEditMask* editMask /* NULL means default state */ )
//
// 6. void definePopupMenu( UINT uResourceID = 0 /* 0 uses default */ )
// - Overide default context menu with new menu resource.
//
// *** Set new font for this control ***
//
// 7. void setFont( const LOGFONT* lpLogFont );
//
// 8. void setFont( LONG fontHeight = -8,
// LONG fontWeight = FW_NORMAL,
// UCHAR pitchAndFamily = DEFAULT_PITCH | FF_DONTCARE,
// LPCSTR faceName = _T("MS Sans Serif"
// );
//
// Remarks:
//
// When using the CEditEx control do not free the CEditMask pointer
// assigned by the setCustomMask call. This class will free it for
// you once it leaves scope.
//
// Declares CLogFont - implementation file is not defined for this class.
//
// Discussion:
//
// This is a subclass of the LOGFONT structure and defines
// the attributes of a font. See the Microsoft documentation
// for more information.
//
// Modification History:
//
// Bill Berry March 1996 Creation
//
//
// Copyright (C) Bill Berry 1996
//
// This class may be used freely. Please distribute with its
// copyright notice in place.
//
#ifndef LOGFONT_H_E7F7D1A0_AE36_11d1_9257_006097CAB461
#define LOGFONT_H_E7F7D1A0_AE36_11d1_9257_006097CAB461
#ifndef __cplusplus
# error C++ compilation is required.
#endif
#if _MSC_VER >= 1000
#pragma once
#endif // _MSC_VER >= 1000
struct CLogFont : public LOGFONT
{
CLogFont( LONG fontHeight = -8,
LONG fontWeight = FW_NORMAL,
UCHAR pitchAndFamily = DEFAULT_PITCH | FF_DONTCARE,
LPCSTR faceName = _T("MS Sans Serif" )
)
{
const int size = sizeof(*this);
memset( this, 0, size );
lfHeight = fontHeight;
lfWeight = fontWeight;
lfPitchAndFamily = pitchAndFamily;
_ASSERT( LF_FACESIZE > lstrlen( faceName ) );
lstrcpy( lfFaceName, faceName );
}
// Note: No need for CLogFont& operator =(const CLogFont& lf) {...}
// We let the compiler handle it...
void lfFaceNameSet( LPCSTR faceName )
{
_ASSERT( faceName );
if ( !faceName ) return;
_ASSERT( LF_FACESIZE > lstrlen( faceName ) );
lstrcpy( lfFaceName, faceName );
}
};
#endif // LOGFONT_H_E7F7D1A0_AE36_11d1_9257_006097CAB461
#if !defined(AFX_EDITEX_H__B3788DB1_4FA1_11D1_9FB8_006097B531B3__INCLUDED_)
#define AFX_EDITEX_H__B3788DB1_4FA1_11D1_9FB8_006097B531B3__INCLUDED_
#if _MSC_VER >= 1000
#pragma once
#endif // _MSC_VER >= 1000
/////////////////////////////////////////////////////////////////////////////
// CEditEx window
class CEditMask
{
public:
virtual BOOL AddChar( UINT nChar ) = 0;
};
class CEditEx : public CEdit
{
// Construction
public:
CEditEx();
DECLARE_DYNCREATE( CEditEx )
// Copy Semantics
// Block copy construction...
//
// No m_hWnd defined for object.
//
private:
CEditEx( const CEditEx& );
public:
// Allow basics to be copied...
CEditEx& operator = ( const CEditEx& );
// Attributes
public:
void setCustomMask( CEditMask* editMask )
{
if ( m_pEditMask ) delete m_pEditMask;
m_pEditMask = editMask;
}
COLORREF bkColor ( COLORREF );
COLORREF textColor( COLORREF );
COLORREF bkColor() const { return m_crBkColor; }
COLORREF textColor() const { return m_crTextColor; }
void setFont( const LOGFONT* lpLogFont );
void setFont( LONG fontHeight = -8,
LONG fontWeight = FW_NORMAL,
UCHAR pitchAndFamily = DEFAULT_PITCH | FF_DONTCARE,
LPCSTR faceName = _T("MS Sans Serif" ) );
void definePopupMenu( UINT uResourceID = 0 /* 0 uses default */ )
{
m_MenuResourceID = uResourceID;
}
// Operations:
public:
// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CEditEx)
//}}AFX_VIRTUAL
// Implementation
public:
virtual ~CEditEx();
// Generated message map functions
protected:
//{{AFX_MSG(CEditEx)
afx_msg void OnContextMenu(CWnd* pWnd, CPoint point);
afx_msg void OnChar(UINT nChar, UINT nRepCnt, UINT nFlags);
//}}AFX_MSG
afx_msg HBRUSH CtlColor(CDC* pDC, UINT nCtlColor);
DECLARE_MESSAGE_MAP()
// Customize your brush
//
virtual BOOL CreateBrushType();
private:
CBrush m_brBkGround;
COLORREF m_crBkColor;
COLORREF m_crTextColor;
CEditMask* m_pEditMask;
CFont* m_pCFont;
UINT m_MenuResourceID;
};
/////////////////////////////////////////////////////////////////////////////
//{{AFX_INSERT_LOCATION}}
// Microsoft Developer Studio will insert additional declarations immediately before the previous line.
#endif // !defined(AFX_EDITEX_H__B3788DB1_4FA1_11D1_9FB8_006097B531B3__INCLUDED_)
/////////////////////////////////////////////////////////////////////////////
// EditEx.cxx : implementation file - header file is EditEx.hxx
//
// Copyright (C) 1996 Bill Berry All rights reserved.
//
// This class is freely distributable as long as the above copyright
// accompanies the header and implementaion sources.
//
// Also, please send me any improvements or bug fixes to bberry@javanet.com
//
//
// Modification History:
//
// Bill Berry November 1996 Created
// March 1998 Update
//
// Description:
//
// Extended CEdit class. Allows easy customization of the following:
//
// 1. COLORREF bkColor( COLORREF crColor )
// - Sets back ground color of the control
//
// 2. COLORREF bkColor()
// - Returns back ground color of control
//
// 3. COLORREF textColor( COLORREF crColor )
// - Sets text or foreground color of the control
//
// 4. COLORREF textColor() const
// - Returns text (or foreground) color of control
//
// 5. void setCustomMask( CEditMask* editMask /* NULL means default state */ )
//
// 6. void definePopupMenu( UINT uResourceID = 0 /* 0 uses default */ )
// - Overide default context menu with new menu resource.
//
// *** Set new font for this control ***
//
// 7. void setFont( const LOGFONT* lpLogFont );
//
// 8. void setFont( LONG fontHeight = -8,
// LONG fontWeight = FW_NORMAL,
// UCHAR pitchAndFamily = DEFAULT_PITCH | FF_DONTCARE,
// LPCSTR faceName = _T("MS Sans Serif"
// );
//
// Remarks:
//
// When using the CEditEx control do not free the CEditMask pointer
// assigned by the setCustomMask call. This class will free it for
// you once it leaves scope.
//
#include "stdafx.h"
#include "LogFont.hxx"
#include "EditEx.hxx"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CEditEx
IMPLEMENT_DYNCREATE(CEditEx, CEdit)
BEGIN_MESSAGE_MAP(CEditEx, CEdit)
//{{AFX_MSG_MAP(CEditEx)
ON_WM_CHAR()
ON_WM_CONTEXTMENU()
//}}AFX_MSG_MAP
ON_WM_CTLCOLOR_REFLECT()
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// Public Interfaces:
//
// Construction:
//
//** CEditEx::CEditEx()
//
CEditEx::CEditEx() : m_pCFont(0)
{
// Use default popup menu
//
m_MenuResourceID = 0;
// Use system colors for defaults
//
m_crTextColor = ::GetSysColor( COLOR_WINDOWTEXT );
m_crBkColor = ::GetSysColor( COLOR_WINDOW );
// The default brush type: SOLID
//
CreateBrushType();
// Edit mask is null
//
m_pEditMask = 0;
}
//** CEditEx::~CEditEx()
//
CEditEx::~CEditEx()
{
if ( m_pCFont ) delete m_pCFont;
if ( m_pEditMask ) delete m_pEditMask;
}
// Note: Copy construction is blocked for this class.
// This is because there would be no defined
// m_hWnd during the construction of the object.
//
// CEditEx::CEditEx( const CEditEx& o )
//
// Allow = operator to be used for copying basics.
//
CEditEx& CEditEx::operator = ( const CEditEx& o )
{
_ASSERT( o != *this ); // You probably did not mean to do this...
if ( o == *this ) return *this; // copying self...
bkColor( o.m_crBkColor );
textColor( o.m_crTextColor );
if ( o.m_pCFont ) {
CLogFont pLogFont;
o.m_pCFont->GetLogFont( &pLogFont );
setFont( &pLogFont );
}
return *this;
}
//** void SetFont( const LOGFONT* lpLogFont )
//
void CEditEx::setFont( const LOGFONT* lpLogFont )
{
_ASSERT( lpLogFont ); // logfont is not defined!!!
if ( !lpLogFont ) return;
if ( m_pCFont ) delete m_pCFont;
m_pCFont = new CFont;
m_pCFont->CreateFontIndirect( lpLogFont );
SetFont( m_pCFont );
}
void CEditEx::setFont( LONG fontHeight /* = -8 */,
LONG fontWeight /* = FW_NORMAL */,
UCHAR pitchAndFamily /* = DEFAULT_PITCH | FF_DONTCARE*/,
LPCSTR faceName /* = _T("MS Sans Serif") */ )
{
if ( m_pCFont )
{
delete m_pCFont;
}
m_pCFont = new CFont;
const CLogFont lf( fontHeight,
FW_NORMAL,
pitchAndFamily,
faceName
);
m_pCFont->CreateFontIndirect( &lf );
SetFont( m_pCFont );
}
//** COLORREF CEditEx::bkColor( COLORREF crColor )
//
COLORREF CEditEx::bkColor( COLORREF crColor )
{
_ASSERT(::IsWindow(m_hWnd));
COLORREF crPrevBkColor = m_crBkColor;
m_crBkColor = crColor;
m_brBkGround.DeleteObject();
CreateBrushType();
Invalidate();
return crPrevBkColor;
}
//** COLORREF CEditEx::textColor( COLORREF crColor )
//
COLORREF CEditEx::textColor( COLORREF crColor )
{
_ASSERT(::IsWindow(m_hWnd));
COLORREF crPrevTextColor = m_crTextColor;
m_crTextColor = crColor;
Invalidate();
return crPrevTextColor;
}
/////////////////////////////////////////////////////////////////////////////
// Protected and private interfaces:
//
//** BOOL CEditEx::CreateBrushType()
//
BOOL CEditEx::CreateBrushType()
{
return m_brBkGround.CreateSolidBrush( m_crBkColor );
}
//** void CEditEx::OnContextMenu(CWnd* pWnd, CPoint point)
//
void CEditEx::OnContextMenu(CWnd* pWnd, CPoint point)
{
// Use default popup
//
if ( !m_MenuResourceID ) {
CEdit::OnContextMenu( pWnd, point );
return;
}
// Replace default popup menu
//
CMenu menu;
if ( !menu.LoadMenu( m_MenuResourceID ) ) {
CEdit::OnContextMenu( pWnd, point);
}
else {
CMenu* pContext = menu.GetSubMenu(0);
pContext->TrackPopupMenu( TPM_LEFTALIGN | TPM_LEFTBUTTON | TPM_RIGHTBUTTON, point.x, point.y, this );
}
}
//** void CEditEx::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)
//
void CEditEx::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)
{
if ( 0 == m_pEditMask ) {
CEdit::OnChar(nChar, nRepCnt, nFlags);
return;
}
// else Use custom mask...
//
if ( m_pEditMask->AddChar( nChar ) )
CEdit::OnChar(nChar, nRepCnt, nFlags);
else
MessageBeep(0);
}
//** HBRUSH CEditEx::CtlColor(CDC* pDC, UINT nCtlColor)
//
HBRUSH CEditEx::CtlColor(CDC* pDC, UINT nCtlColor)
{
pDC->SetTextColor( m_crTextColor );
pDC->SetBkColor( m_crBkColor );
return (HBRUSH)m_brBkGround;
}
这是操作的代码◎◎◎◎◎◎◎◎◎◎◎◎◎◎◎◎◎◎◎◎
// CEditExAndCStaticExDlg.h : header file
//
#include "editex.hxx"
protected:
CEditEx m_edit;
// CEditExAndCStaticExDlg.cpp : implementation file
//
BOOL CCEditExAndCStaticExDlg::OnInitDialog()
{
CDialog::OnInitDialog();
m_edit.SubclassDlgItem( IDC_EDIT_CTRL, this );
m_edit.bkColor( RGB(0,122,122) );
m_edit.textColor( RGB(255,0,0) );
m_edit.setFont( -17 );
}
可疑之处:
(1)
class CEditMask
{
public:
virtual BOOL AddChar( UINT nChar ) = 0;
};
没看到它实现呀!但他是纯虚函数!
(2)
ON_WM_CTLCOLOR_REFLECT()在MSDN中查不到,是什么东东?
(3)
为什么它能编译成功,并能显示出效果来!