본문 바로가기

Windows/MFC

CEditEx xp style edit box

[code]
// EditEx.cpp : implementation file
//

// DATE        : June 27, 2006
// CODER    : aucd29 (aucd29@gmail.com)
// VERSION : 1.02
//
//
// NOTE : August 4, 2006
// --------------------------------------------------------------
// * 2k 에서 XP 테마 처럼 edit 박스를 보여주고 싶어서.. 작성
//
// --------------------------------------------------------------
// NOTE : August 14, 2006
// --------------------------------------------------------------
// * MouseOver시 text를 다시 write 하지 않아서 글이 사라지는
// 현상이 생기는 것 같다.
// xp 일때와 2k일때를 구분해서 적절하게 처리.. -_-2k에서테스트
// 안해봄...
//
// --------------------------------------------------------------
// NOTE : September 7, 2006
// --------------------------------------------------------------
// * 안정화 된듯!
//
// --------------------------------------------------------------
// NOTE : February 22, 2007
// --------------------------------------------------------------
// * 독립적으로 작동하게 하기 위해서 변경
// * READONLY 처리
// * DISABLE 처리
//
// --------------------------------------------------------------
// NOTE : April 12, 2007
// --------------------------------------------------------------
// * ATL 에 CAtlRegExp 이용 정규 표현식을 추가
//

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

// CEditEx

IMPLEMENT_DYNAMIC(CEditEx, CEdit)

CEditEx::CEditEx()
: m_bLoad(false)
{
    m_crColor = 0X00B99D7F;
}

CEditEx::~CEditEx()
{
    m_brOutline.DeleteObject();
    m_brGap.DeleteObject();
}

BEGIN_MESSAGE_MAP(CEditEx, CEdit)
    ON_WM_NCPAINT()
    ON_WM_ERASEBKGND()
    ON_CONTROL_REFLECT(EN_MAXTEXT, &CEditEx::OnEnMaxtext)
END_MESSAGE_MAP()

// CEditEx message handlers

void CEditEx::OnNcPaint()
{
    CDC* pDC = GetWindowDC();
    InitObject(pDC);
    
    pDC->FrameRect(rectOutline, &m_brOutline);
    pDC->FrameRect(rectGap, &m_brGap);
}

BOOL CEditEx::OnEraseBkgnd(CDC* pDC)
{
    return true;
}

void CEditEx::InitObject(CDC* pDC)
{
    if (!m_bLoad)
    {
        GetWindowRect(m_rect);

        // 사각형을 그리기 위해 설정하기
        rectOutline.SetRect(0, 0, m_rect.right - m_rect.left, m_rect.bottom - m_rect.top);
        rectGap            = rectOutline;
        rectGap.top        = rectGap.left = 1;
        rectGap.bottom -= 1;
        rectGap.right -= 1;
        
        // READONLY & DISABLE 일 경우BORDER의 색상을 GRAY로 변경한다.
        if(GetStyle() & ES_READONLY || GetStyle() & WS_DISABLED)
        {
            m_brOutline.CreateSolidBrush(0x00B2ACA5);
        }
        else
        {
            m_brOutline.CreateSolidBrush(m_crColor);
        }
        m_brGap.CreateSolidBrush(0X00FFFFFF);

        m_bLoad = true;
    }
}

void CEditEx::SetColor(COLORREF crColor)
{
    m_crColor = crColor;
}

void CEditEx::OnEnMaxtext()
{
    CString szLimitOver;

    szLimitOver.Format(L"ERROR : Max length %ld ", GetLimitText());
    AfxMessageBox(szLimitOver);
}

CString CEditEx::GetText(void)
{
    CString szWindowText;
    GetWindowText(szWindowText);

    return szWindowText;
}

bool CEditEx::IsLength(UINT nResourceID)
{
    if (!GetWindowTextLength())
    {
        AfxMessageBox(nResourceID);
        SetFocus();

        return false;
    }

    return true;
}

bool CEditEx::IsAlNum(void)
{
    if (GetWindowTextLength())
    {
        CAtlRegExp<> regPatten;
        REParseError status = regPatten.Parse(L"^[0-9a-zA-Z ]+$");
        
        if (REPARSE_ERROR_OK != status)
        {
            return false;
        }
        
        CAtlREMatchContext<> mcContext;
        if (regPatten.Match(GetText(), &mcContext))
        {
            return true;
        }

        AfxMessageBox(L"Do not match (digit, alphabet, space only)");
        SetFocus();

        return false;
    }
    
    return true;
}

bool CEditEx::IsEmail(void)
{
    if (GetWindowTextLength())
    {
        CAtlRegExp<> regPatten;
        REParseError status = regPatten.Parse(L"^[_a-zA-Z0-9-]+@[._a-zA-Z0-9-]+\\.[a-zA-Z]+$");

        if (REPARSE_ERROR_OK != status)
        {
            return false;
        }

        CAtlREMatchContext<> mcContext;
        if (regPatten.Match(GetText(), &mcContext))
        {
            return true;
        }
        
        AfxMessageBox(L"Do not match (email)");
        SetFocus();

        return false;
    }

    return true;
}

bool CEditEx::IsPhoneNumber(void)
{
    if (GetWindowTextLength())
    {
        CAtlRegExp<> regPatten;
        REParseError status = regPatten.Parse(L"^\\+?[0-9]+$");

        if (REPARSE_ERROR_OK != status)
        {
            return false;
        }

        CAtlREMatchContext<> mcContext;
        if (regPatten.Match(GetText(), &mcContext))
        {
            return true;
        }

        AfxMessageBox(L"Do not match (phone number)");
        SetFocus();

        return false;
    }

    return true;
}

bool CEditEx::IsFloat(void)
{
    if (GetWindowTextLength())
    {
        CAtlRegExp<> regPatten;
        REParseError status = regPatten.Parse(L"^[-0-9]+\\.?[0-9]+$");

        if (REPARSE_ERROR_OK != status)
        {
            return false;
        }

        CAtlREMatchContext<> mcContext;
        if (regPatten.Match(GetText(), &mcContext))
        {
            return true;
        }

        AfxMessageBox(L"Do not match (digit only)");
        SetFocus();

        return false;
    }

    return true;
}

bool CEditEx::IsAlNumOnly(void)
{
    if (GetWindowTextLength())
    {
        CAtlRegExp<> regPatten;
        REParseError status = regPatten.Parse(L"^[0-9a-zA-Z]+$");
        
        if (REPARSE_ERROR_OK != status)
        {
            return false;
        }

        CAtlREMatchContext<> mcContext;
        if (regPatten.Match(GetText(), &mcContext))
        {
            return true;
        }

        AfxMessageBox(L"Do not match (digit, alphabet only)");
        SetFocus();

        return false;
    }

    return true;
}

bool CEditEx::IsAlphabetSpace(void)
{
    if (GetWindowTextLength())
    {
        CAtlRegExp<> regPatten;
        REParseError status = regPatten.Parse(L"^[a-zA-Z ]+$");

        if (REPARSE_ERROR_OK != status)
        {
            return false;
        }

        CAtlREMatchContext<> mcContext;
        if (regPatten.Match(GetText(), &mcContext))
        {
            return true;
        }

        AfxMessageBox(L"Do not match (alphabet, space only)");
        SetFocus();

        return false;
    }

    return true;
}
[/code]

[code]
#pragma once

// regular expression
#include <atlrx.h>

// CEditEx

class CEditEx : public CEdit
{
    DECLARE_DYNAMIC(CEditEx)

public:
    CEditEx();
    virtual ~CEditEx();

protected:
    DECLARE_MESSAGE_MAP()

protected:
    afx_msg void OnEnMaxtext();
    afx_msg void OnNcPaint();
    afx_msg BOOL OnEraseBkgnd(CDC* pDC);

protected:
    CPen m_pen;
    bool m_bLoad;
    CRect m_rect, rectOutline, rectGap;
    CBrush m_brOutline, m_brGap;
    COLORREF m_crColor;

protected:
    void InitObject(CDC* pDC);

public:
    void SetColor(COLORREF crColor);
    CString GetText(void);
    bool IsLength(UINT nResourceID);
    bool IsAlNum(void);
    bool IsEmail(void);
    bool IsPhoneNumber(void);
    bool IsAlNumOnly(void);
    bool IsAlphabetSpace(void);
    bool IsFloat(void);
};
[/code]

'Windows > MFC' 카테고리의 다른 글

GDIPLUS Anigif  (0) 2013.10.02
CListCtrl Freeze column  (0) 2013.10.02
CFont 사용법 LOGFONT  (0) 2013.10.02
HICON 아이콘 뿌리기 DrawIconEx  (0) 2013.10.02
Current icon image types (Icon type 종류별 선택)  (0) 2013.10.02