工控编程吧
标题:
上位机MFC编辑框字体大小背景设置实例
[打印本页]
作者:
qq263946146
时间:
2019-7-9 09:31
标题:
上位机MFC编辑框字体大小背景设置实例
效果演示
当前这个实例用于定制编辑框文本、背景颜色和字体大小等属性。
效果如下图
(, 下载次数: 0)
上传
点击文件名下载附件
属性在例程中是通过下面代码设置
COLORREF bk = RGB(255, 255, 255);
m_Edit.bkColor( bk );
m_Edit.textColor( RGB(255,0,0) );
m_Edit.setFont( -17 );
实现过程
1.新建立基于对话框工程,复制例程根目录文件EDITEX.H
,EDITEX.CPP,LOGFONT.H到自己工程目录,并导入到工程,这样工程会多出几个集成类。
如CEditEx等。
2.然后就是这个类的使用。
在主对话框类的包含头文件并添加类实例
#include "EditEx.h"
CEditEx m_Edit;
初始化实例,设置其文本背景等属性
COLORREF bk = RGB(255, 255, 255);
m_Edit.bkColor( bk );
m_Edit.textColor( RGB(255,0,0) );
m_Edit.setFont( -17 );
3.编译,这样例程就可以运行,查看效果了
下面是文件的源代码,可以复制使用或直接下载例程使用:
(, 下载次数: 0)
上传
点击文件名下载附件
// 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.
#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_)
复制代码
// 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.h"
#include "EditEx.h"
#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;
}
复制代码
欢迎光临 工控编程吧 (https://www.gkbc8.com/)
Powered by Discuz! X3.4