Edit控件的扩展类

一、 一个Edit控件的扩展类
转:http://hi.baidu.com/%CF%FB%BB%EA%D2%B9/blog/item/4711ce1a457cb10c35fa41fe.html

自己写的一个Edit控件的扩展类(源码)修改版
2010年01月17日 星期日 下午 04:35

#if !defined(AFX_COOLEDIT_H__3F4B9BD7_EFF9_45F5_AD79_EEF2B95742B3__INCLUDED_)
#define AFX_COOLEDIT_H__3F4B9BD7_EFF9_45F5_AD79_EEF2B95742B3__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
// CoolEdit.h : header file
//
/*
作者:段晓辉
版本:2.0
功能:对标准的Edit控件进行扩展,可以在标准的控件中加入小位图,
并可以改变背景、文字的颜色和文字的字体。
接口: void SetEditFont(LPLOGFONT lpFont);功能:设置编辑框的字体
void SetTextColor(COLORREF textColor);//功能:设置编辑框字体的颜色
void SetBkColor(COLORREF bkColor);//功能:设置编辑框的背景颜色
void LoadBitmap(UINT bmpID);//功能:在编辑框内增加位图
void SetClearBk(BOOL bClearBk = TRUE);//功能:设置是否对图标的背景进行去除操作,默认为清除。
void SetClearColor(COLORREF clearColor = RGB(255, 255, 255));//设置要去除的颜色。


修改: 1、修改了小图标不能和编辑框相匹配的问题
2、修改了当改变编辑框背景色时,图标有边框(背景)的问题
*/
/
// CCoolEdit window

class CCoolEdit : public CEdit
{
// Construction
public:
CCoolEdit();

// Attributes
public:

// Operations
public:

// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CCoolEdit)
//}}AFX_VIRTUAL

// Implementation
public:
void SetClearColor(COLORREF clearColor = RGB(255, 255, 255));
void SetClearBk(BOOL bClearBk = TRUE);
void SetEditFont(LPLOGFONT lpFont);
void SetTextColor(COLORREF textColor);
void SetBkColor(COLORREF bkColor);
void LoadBitmap(UINT bmpID);
virtual ~CCoolEdit();

// Generated message map functions
protected:

//{{AFX_MSG(CCoolEdit)

afx_msg BOOL OnEraseBkgnd(CDC* pDC);
afx_msg HBRUSH CtlColor(CDC* pDC, UINT nCtlColor);
afx_msg void OnSetFocus(CWnd* pOldWnd);
afx_msg void OnChange();
//}}AFX_MSG

DECLARE_MESSAGE_MAP()
private:
COLORREF m_clearColor;
BOOL m_bClearBk;
CFont m_font;
CBrush m_brush;
COLORREF m_colBack;//背景颜色
COLORREF m_colText;//文本颜色
CBitmap m_bmpBack;//用于保存加载的位图
BITMAP bmpBack;
};

/

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

#endif // !defined(AFX_COOLEDIT_H__3F4B9BD7_EFF9_45F5_AD79_EEF2B95742B3__INCLUDED_)

 

 

 

// CoolEdit.cpp : implementation file
//

#include "stdafx.h"
//#include "wdm.h"
#include "CoolEdit.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/
// CCoolEdit

CCoolEdit::CCoolEdit()
{
this->m_colBack = RGB(255, 255, 255);
this->m_colText = RGB(0, 0, 0);
this->m_brush.CreateStockObject(NULL_BRUSH);
this->m_bClearBk = TRUE;
this->m_clearColor = RGB(255, 255, 255);
}

CCoolEdit::~CCoolEdit()
{
}


BEGIN_MESSAGE_MAP(CCoolEdit, CEdit)
//{{AFX_MSG_MAP(CCoolEdit)
ON_WM_ERASEBKGND()
ON_WM_CTLCOLOR_REFLECT()
ON_WM_SETFOCUS()
ON_CONTROL_REFLECT(EN_CHANGE, OnChange)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/
// CCoolEdit message handlers
//功能:绘制背景
BOOL CCoolEdit::OnEraseBkgnd(CDC* pDC)
{
// TODO: Add your message handler code here and/or call default
if(this->m_bmpBack.GetSafeHandle() != NULL)
{
   CDC dcMem;
   dcMem.CreateCompatibleDC(pDC);
   CBitmap *pOldBitmap = dcMem.SelectObject(&m_bmpBack);

   CRect re;
   CBrush brush;
   GetClientRect(&re);
   brush.CreateSolidBrush(m_colBack);
   pDC->FillRect(&re, &brush);

   COLORREF Old = dcMem.SetBkColor(this->m_clearColor);
//   pDC->BitBlt(0, 0, bmpBack.bmWidth, bmpBack.bmHeight, &dcMem, 0, 0, SRCCOPY);
   int bl = bmpBack.bmWidth/bmpBack.bmHeight;

   if(this->m_bClearBk)
   {
    pDC->StretchBlt(0, 0, re.Height()*bl, re.Height(), &dcMem, 0, 0, bmpBack.bmWidth, bmpBack.bmHeight, SRCINVERT);

    CDC dcMask;
    dcMask.CreateCompatibleDC(pDC);
    CBitmap bitmapMask;
    bitmapMask.CreateBitmap(re.Height()*bl, re.Height(), 1, 1, NULL); //把mask位图装入mask DC
    CBitmap* pOldBitmapMask = dcMask.SelectObject(&bitmapMask); //用透明色创建“掩码”位图  
    dcMask.StretchBlt(0, 0, re.Height()*bl, re.Height(), &dcMem, 0, 0, bmpBack.bmWidth, bmpBack.bmHeight, SRCCOPY);

    pDC->BitBlt(0, 0, re.Height()*bl, re.Height(), &dcMask, 0, 0, SRCAND);
    pDC->StretchBlt(0, 0, re.Height()*bl, re.Height(), &dcMem, 0, 0, bmpBack.bmWidth, bmpBack.bmHeight, SRCINVERT);
   }
   else
   {
    pDC->StretchBlt(0, 0, re.Height()*bl, re.Height(), &dcMem, 0, 0, bmpBack.bmWidth, bmpBack.bmHeight, SRCCOPY);

   }

   dcMem.SelectObject(pOldBitmap);
   dcMem.SetBkColor(Old);
   return TRUE;
}

return CEdit::OnEraseBkgnd(pDC);

}


//功能:加载位图资源并依附于m_bmpBack对象
void CCoolEdit::LoadBitmap(UINT bmpID)
{
if(this->m_bmpBack.GetSafeHandle() != NULL)
{
   this->m_bmpBack.DeleteObject();
   this->m_bmpBack.LoadBitmap(bmpID);
}
else
{
   this->m_bmpBack.LoadBitmap(bmpID);
}
this->m_bmpBack.GetBitmap(&bmpBack);
// this->m_bmpBack.GetBitmap(&bmpChange);

}

 


HBRUSH CCoolEdit::CtlColor(CDC* pDC, UINT nCtlColor)
{
pDC->SetBkMode(TRANSPARENT);
pDC->SetTextColor(this->m_colText);
pDC->SetBkColor(this->m_colBack);

if(this->m_bmpBack.GetSafeHandle() == NULL)
{
   this->m_brush.DeleteObject();
   this->m_brush.CreateSolidBrush(this->m_colBack);
   return this->m_brush;
}
else
{
   return this->m_brush;
}

// TODO: Return a non-NULL brush if the parent's handler should not be called
// return NULL;
}


//功能:当编辑框获得焦点时,设置光标的正确位置
void CCoolEdit::OnSetFocus(CWnd* pOldWnd)
{


// TODO: Add your message handler code here

if(this->m_bmpBack.GetSafeHandle() != NULL)
{
   int bl = bmpBack.bmWidth/bmpBack.bmHeight;

///   SetMargins(this->bmpBack.bmWidth,1);

   CRect re;
   GetClientRect(&re);

   SetMargins(re.Height()*bl,1);
}

CEdit::OnSetFocus(pOldWnd);

}

void CCoolEdit::OnChange()
{
// TODO: If this is a RICHEDIT control, the control will not
// send this notification unless you override the CEdit::OnInitDialog()
// function and call CRichEditCtrl().SetEventMask()
// with the ENM_CHANGE flag ORed into the mask.

// TODO: Add your control notification handler code here

Invalidate();

}


//功能:设置编辑框的背景颜色
void CCoolEdit::SetBkColor(COLORREF bkColor)
{
this->m_colBack = bkColor;
Invalidate();
}


//功能:设置编辑框字体的颜色
void CCoolEdit::SetTextColor(COLORREF textColor)
{
this->m_colText = textColor;
Invalidate();
}


//功能:设置编辑框的字体
void CCoolEdit::SetEditFont(LPLOGFONT lpFont)
{
if(m_font.GetSafeHandle() != NULL)
{
   m_font.DeleteObject();
}
m_font.CreateFontIndirect(lpFont);
SetFont(&m_font);

}

 

void CCoolEdit::SetClearBk(BOOL bClearBk)
{
this->m_bClearBk = bClearBk;

}

void CCoolEdit::SetClearColor(COLORREF clearColor)
{
this->m_clearColor = clearColor;

}

 

 

二、 在evc下面的edit控件背景贴图

转:http://hi.baidu.com/%CA%AE%C6%DF%C8%D5%D4%C2/blog/item/0f767aec61380836279791ac.html

 

 

这个是很久以前写的程序了。这里贴出来是为了方便以后找资料。
下面的是头文件的代码:
//EditCtrl.h
#if !defined(AFX_EDITCTRL_H__DCBB58E6_7C67_4656_9CA1_BE3793A759FA__INCLUDED_)
#define AFX_EDITCTRL_H__DCBB58E6_7C67_4656_9CA1_BE3793A759FA__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
// EditCtrl.h : header file
//
#include   <Sipapi.h>
/
// CEditCtrl window

class CEditCtrl : public CEdit
{
// Construction
public:
CEditCtrl();

// Attributes
public:
CBitmap* m_MainBmp;
CBitmap *m_pOldMianBmp;
BOOL    flag;
int   Width;
int     Height;
BOOL    IsPanDown;

HIMC    m_hImc;

// Operations
public:

// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CEditCtrl)
protected:
virtual BOOL PreCreateWindow(CREATESTRUCT& cs);
//}}AFX_VIRTUAL

// Implementation
public:
void SetInvalidate(BOOL bPanDown =FALSE);
void GetBkBmp(CBitmap* bkbmp);
virtual ~CEditCtrl();

// Generated message map functions
protected:
//{{AFX_MSG(CEditCtrl)
afx_msg HBRUSH CtlColor(CDC* pDC, UINT nCtlColor);
afx_msg BOOL OnEraseBkgnd(CDC* pDC);
afx_msg void OnChange();
afx_msg void OnLButtonUp(UINT nFlags, CPoint point);
afx_msg void OnMouseMove(UINT nFlags, CPoint point);
afx_msg void OnLButtonDown(UINT nFlags, CPoint point);
//}}AFX_MSG

DECLARE_MESSAGE_MAP()
};

/

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

#endif // !defined(AFX_EDITCTRL_H__DCBB58E6_7C67_4656_9CA1_BE3793A759FA__INCLUDED_)


//下面是源文件的代码:
//EditCtrl.cpp
// EditCtrl.cpp : implementation file
//

#include "StdAfx.h"
#include "2530templatedll.h"
#include "EditCtrl.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/
// CEditCtrl
CEditCtrl::CEditCtrl()
{
flag   =FALSE;
Width =0;
Height =0;
IsPanDown =FALSE;
}


CEditCtrl::~CEditCtrl()
{

}


BEGIN_MESSAGE_MAP(CEditCtrl, CEdit)
//{{AFX_MSG_MAP(CEditCtrl)
ON_WM_CTLCOLOR_REFLECT()
ON_WM_ERASEBKGND()
ON_CONTROL_REFLECT(EN_CHANGE, OnChange)
ON_WM_LBUTTONUP()
ON_WM_MOUSEMOVE()
ON_WM_LBUTTONDOWN()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/
// CEditCtrl message handlers

HBRUSH CEditCtrl::CtlColor(CDC* pDC, UINT nCtlColor)
{
// TODO: Change any attributes of the DC here
pDC->SetBkMode(TRANSPARENT); ///选择透明背景模式
return HBRUSH(GetStockObject(HOLLOW_BRUSH));
return NULL;
}


BOOL CEditCtrl::OnEraseBkgnd(CDC* pDC)
{
// TODO: Add your message handler code here and/or call default
CDC m_MianDC;

//创建与内存兼容的DC
m_MianDC.CreateCompatibleDC(pDC);
m_pOldMianBmp =m_MianDC.SelectObject(m_MainBmp);
pDC->BitBlt(0, 0, Width, Height, &m_MianDC, 0, 0, SRCCOPY);
m_MianDC.SelectObject(m_pOldMianBmp);
m_MianDC.DeleteDC();

return TRUE;
}

void CEditCtrl::OnChange()
{
// TODO: Add your control notification handler code here
Invalidate();
}

void CEditCtrl::OnLButtonUp(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
if(!IsPanDown)
   Invalidate();
else
{
   ::SetCapture(NULL);
   ClientToScreen(&point);
   GetParent()->SendMessage(WM_LBUTTONUP, (WPARAM)m_hWnd, (LPARAM)MAKELONG(point.x,point.y));
}
CEdit::OnLButtonUp(nFlags, point);
}

void CEditCtrl::GetBkBmp(CBitmap* bkbmp)
{
BITMAP bm;

m_MainBmp =bkbmp;
//将位图资源与句柄绑定
m_MainBmp->GetObject(sizeof(BITMAP),&bm);
   
Width =bm.bmWidth;
Height =bm.bmHeight;
/*
CString str;
str.Format(_T("%d,%d"),Width,Height);
AfxMessageBox(str);
*/
}

void CEditCtrl::OnMouseMove(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
::SetCapture(NULL);
ClientToScreen(&point);
GetParent()->SendMessage(WM_MOUSEMOVE, (WPARAM)m_hWnd, (LPARAM)MAKELONG(point.x,point.y));
CEdit::OnMouseMove(nFlags, point);
}

void CEditCtrl::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
::SetCapture(NULL);
ClientToScreen(&point);
GetParent()->SendMessage(WM_LBUTTONDOWN, (WPARAM)m_hWnd, (LPARAM)MAKELONG(point.x,point.y));
CEdit::OnLButtonDown(nFlags, point);
}

void CEditCtrl::SetInvalidate(BOOL bPanDown)
{
IsPanDown =bPanDown;
}

BOOL CEditCtrl::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Add your specialized code here and/or call the base class

return CEdit::PreCreateWindow(cs);
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值