优化大师4.61中的EDIT是怎么样实现的呀!

leech42 2002-04-12 03:27:48
看到优化大师4.61中的EDIT挺好看的,但不知是怎么样实现的,有人知道吗?
...全文
45 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
leech42 2002-04-12
  • 打赏
  • 举报
回复
谢谢了.
xpf_2000 2002-04-12
  • 打赏
  • 举报
回复
借花献佛
xpf_2000 2002-04-12
  • 打赏
  • 举报
回复
///EditEx.h////
#if !defined __EDIT_EX__
#define __EDIT_EX__

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

// EditEx.h : header file
//
/////////////////////////////////////////////////////////////////////////////
/*
[Description]
This class provide a appearance the same as the "Window优化大师".That is
when the mouse move over the edit control,the background turns to White!When the
mouse move out of the edit control,the background turns to the origion color!
[History]
2002/3/3 Coded by David!
[Flaws]
(1)The control is dealing with chinese characters which is two space than the ANSI
character by default.
(2)When the text is overflow .This class couldn't provide an effective way to scroll
the text!

*/////////////////////////////////////////////////////////////////////////////
// CEditEx window

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

void SetText(CString text);
void GetText(CString &text);
protected:
void DrawEdit(COLORREF m_clrBorder=RGB(160,150,131));

// Attributes
private:
BOOL m_bIsFocused;
BOOL m_bMouseOver;
CString m_strText;
public:

// 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 OnPaint();
afx_msg void OnSetFocus(CWnd* pOldWnd);
afx_msg void OnMouseMove(UINT nFlags, CPoint point);
afx_msg void OnKillFocus(CWnd* pNewWnd);

afx_msg BOOL OnEraseBkgnd(CDC* pDC);
afx_msg UINT OnGetDlgCode();
//}}AFX_MSG

DECLARE_MESSAGE_MAP()
};

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

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

#endif // !defined(AFX_EDITEX_H__5DDBDABE_2E27_11D6_B925_0000E8B03E24__INCLUDED_)




///EditEx.cpp///
// EditEx.cpp : implementation file
//

#include "stdafx.h"
#include "EditEx.h"

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

/////////////////////////////////////////////////////////////////////////////
// CEditEx

CEditEx::CEditEx()
{
m_bIsFocused=FALSE;
m_bMouseOver=FALSE;
}

CEditEx::~CEditEx()
{
}


BEGIN_MESSAGE_MAP(CEditEx, CEdit)
//{{AFX_MSG_MAP(CEditEx)
ON_WM_PAINT()
ON_WM_SETFOCUS()
ON_WM_MOUSEMOVE()
ON_WM_KILLFOCUS()
ON_WM_ERASEBKGND()
ON_WM_GETDLGCODE()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CEditEx message handlers

void CEditEx::OnPaint()
{

// TODO: Add your message handler code here
Default();
DrawEdit();
}

void CEditEx::DrawEdit(COLORREF m_clrBorder)
{

CRect rect;
GetClientRect(&rect);
CDC *pDC=GetDC();
COLORREF color=RGB(255,255,255);
rect.InflateRect(1,1);
pDC->Draw3dRect(&rect,m_clrBorder,m_clrBorder);
rect.InflateRect(1,1);
pDC->Draw3dRect(&rect,m_clrBorder,m_clrBorder);
rect.DeflateRect(2,2);
pDC->Draw3dRect(&rect,color,color);
rect.InflateRect(2,2);
pDC->Draw3dRect(&rect,m_clrBorder,m_clrBorder);
rect.DeflateRect(1,1);

CFont font,*pOldFont;
/* Get font */
NONCLIENTMETRICS info;
info.cbSize=sizeof(info);
SystemParametersInfo(SPI_GETNONCLIENTMETRICS,sizeof(info),&info,0);
VERIFY(font.CreateFontIndirect (&info.lfMessageFont ));
/* Get rect*/
pOldFont=pDC->SelectObject(&font);

GetWindowText(m_strText);
if(!m_bMouseOver)
{
pDC->FillRect(&rect,new CBrush(RGB(221,221,221)));
pDC->SetBkMode(TRANSPARENT);
pDC->DrawText(m_strText,&rect,DT_VCENTER|DT_SINGLELINE);


}
else
{
pDC->FillRect(&rect,new CBrush(RGB(255,255,255)));
pDC->SetBkMode(TRANSPARENT);
pDC->DrawText(m_strText,&rect,DT_VCENTER|DT_SINGLELINE);
}
pDC->SelectObject(pOldFont);
ReleaseDC(pDC);
}



void CEditEx::OnMouseMove(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
CEdit::OnMouseMove(nFlags, point);
if(m_bIsFocused) return;
if(GetCapture()!=this)//come in for the first time
{
m_bMouseOver=TRUE;
SetCapture();
DrawEdit();
}
else
{
CRect rect;
GetClientRect(&rect);
if(!rect.PtInRect(point))//Mouse move out of Edit control
{
m_bMouseOver=FALSE;
DrawEdit();
ReleaseCapture();
}
}

}

void CEditEx::OnSetFocus(CWnd* pOldWnd)
{

m_bIsFocused=TRUE;
m_bMouseOver=TRUE;
CWnd::OnSetFocus(pOldWnd);
Invalidate();


}

void CEditEx::OnKillFocus(CWnd* pNewWnd)
{

m_bIsFocused=FALSE;
m_bMouseOver=FALSE;
CWnd::OnKillFocus(pNewWnd);
Invalidate();

}




void CEditEx::SetText(CString text)
{
SetWindowText(text);
}

void CEditEx::GetText(CString &text)
{
text=m_strText;
}

BOOL CEditEx::OnEraseBkgnd(CDC* pDC)
{
// TODO: Add your message handler code here and/or call default
return TRUE;
}

UINT CEditEx::OnGetDlgCode()
{
return CEdit::OnGetDlgCode();
}


//你可像这样来调用
CEditEx m_Edit2;
CEditEx m_Edit;


//{{AFX_DATA_MAP(CZjp01Dlg)
DDX_Control(pDX, IDC_EDIT2, m_Edit2);
DDX_Control(pDX, IDC_EDIT1, m_Edit);
//}}AFX_DATA_MAP

m_Edit.SetText("WuHan University");
m_Edit2.SetText("David!");

leech42 2002-04-12
  • 打赏
  • 举报
回复
有人知道吗?

16,472

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧