본문 바로가기

Windows/MFC

CHorbarText

[code]기존에 Horizantal bar를 생성한뒤 앞에 text를 넣는게 다소 귀차나서 -_ -? 만든 클래스[/code]

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

// DATE        : February 22, 2007
// CODER    : aucd29 (aucd29@gmail.com)
// VERSION    : 1.0
//
//

// NOTE : February 22, 2007
// ----------------------------------------------------------
// * 간단한 거니깐 GDI 를 이용해서 만들도록 하자. (아무래도
// 속도가.. 훗...
//


#include "stdafx.h"
#include "GDIPLUS_TEST.h"
#include "HorbarText.h"


// CHorbarText

IMPLEMENT_DYNAMIC(CHorbarText, CStatic)

CHorbarText::CHorbarText()
: m_szWindowText(_T(""))
, m_bIsLoad(false)
, m_nMiddlePos(0)
, m_nHeight(0)
, m_szFont(L"")
, m_bFontBold(false)
{
    m_crHorbar         = RGB(157, 157, 161);
    m_crHorbarShadow = RGB(255, 255, 255);
    m_crFontColor     = RGB(0, 70, 213);        // 퍼렁 ㅋ
}

CHorbarText::~CHorbarText()
{
    DeleteObject(m_pen);
    DeleteObject(m_penShadow);
    DeleteObject(m_oTextFont);
    DeleteObject(mBitmap);
    DeleteObject(mDC);
}


BEGIN_MESSAGE_MAP(CHorbarText, CStatic)
    ON_WM_ERASEBKGND()
    ON_WM_PAINT()
END_MESSAGE_MAP()

// CHorbarText message handlers

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

void CHorbarText::GetDefaultFont(void)
{
    // 시스템 폰트 가져와서 적용하기
    LOGFONT hfont;
    GetFont()->GetLogFont(&hfont);

    if (m_szFont.GetLength())
    {
        wcscpy_s(hfont.lfFaceName, m_szFont);
    }

    if (m_nHeight)
    {
        hfont.lfHeight = m_nHeight;
    }

    if (m_bFontBold)
    {
        hfont.lfWeight = FW_BOLD;
    }    

    m_oTextFont.CreateFontIndirect(&hfont);
}

void CHorbarText::InitObject(CDC* pDC)
{
    // 한번만 로드...
    if (!m_bIsLoad)
    {
        int PenStyle = PS_GEOMETRIC | PS_SOLID | PS_ENDCAP_SQUARE;
        
        // MDC와 비트맵에 현재 dc를 정의 및 설정
        GetClientRect(m_rect);
        GetDefaultFont();
        GetWindowText(m_szWindowText);

        try
        {
            // 메모리 DC를 생성하구
            mDC.CreateCompatibleDC(pDC);
            mBitmap.CreateCompatibleBitmap(pDC, m_rect.Width(), m_rect.Height());    
            mDC.SelectObject(&mBitmap);

            // 기본적으로 배경색이 검정이 들어가더라..
            // 폰트와 배경색을 지정해주자. ^^
            mDC.FillSolidRect(m_rect, GetSysColor(COLOR_3DFACE));
            mDC.SelectObject(GetSysColorBrush(COLOR_3DFACE));
            mDC.SelectObject(&m_oTextFont);
            mDC.SetTextColor(m_crFontColor);

            // 펜 설정
            m_pen.CreatePen(PenStyle, 1, m_crHorbar);
            m_penShadow.CreatePen(PenStyle, 1, m_crHorbarShadow);
            m_nMiddlePos = static_cast<int>(m_rect.Height() / 2);
        }
        catch (CResourceException* e)
        {
            e->ReportError();
            e->Delete();
        }

        // 재 로드를 방지하기 위해서 작업
        m_bIsLoad = true;
    }
}

void CHorbarText::SetHorbarColor(COLORREF crHorbar, COLORREF crHorbarShadow)
{
    m_crHorbar         = crHorbar;
    m_crHorbarShadow = crHorbarShadow;
}

void CHorbarText::OnPaint()
{
    CPaintDC dc(this); // device context for painting

    InitObject(&dc);

    // 메모리DC 에 선을 먼저 그리고
    mDC.MoveTo(0, m_nMiddlePos);
    mDC.SelectObject(&m_pen);
    mDC.LineTo(m_rect.Width(), m_nMiddlePos);
    mDC.SelectObject(&m_penShadow);
    mDC.MoveTo(0, m_nMiddlePos + 1);
    mDC.LineTo(m_rect.Width() - 1, m_nMiddlePos + 1);
    mDC.LineTo(m_rect.Width(), m_nMiddlePos - 1);

    // 글자 넣고 여백을 위해 빈칸 넣고
    mDC.DrawText(m_szWindowText + L" ",m_rect,DT_LEFT);

    // 내용을 넣어주고 ㅎㅎ
    dc.BitBlt(0, 0, m_rect.Width(), m_rect.Height(), &mDC, 0, 0, SRCCOPY);
}

void CHorbarText::SetFontStyle(CString szFont, int nHeight, COLORREF crColor)
{
    // 폰트 변경
    m_szFont = szFont;

    // 폰트 크기 변경
    if (nHeight)
    {
        m_nHeight = nHeight;
    }

    // 폰트 색 변경
    if (crColor)
    {
        m_crFontColor = crColor;
    }
}

void CHorbarText::SetFontBold(void)
{
    m_bFontBold = true;
}
[/code]

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

DRAWITEMSTRUCT, LPDRAWITEMSTRUCT  (0) 2013.10.02
GetSysColor  (0) 2013.10.02
다이얼로그에 색을 가져오자 Dialog color (GetSysColor)  (0) 2013.10.02
CButtonEx  (0) 2013.10.02
CRectEx  (0) 2013.10.02