• 로그인 함 해보끄나?

  • Sarangnamu.net June 17, 2003
    Home Login Profile Study Open Project Q&A Google Code
    CComboBoxExt
    Last Modify : 27 December, 2010(02:33)
    // ComboBoxExt.cpp : implementation file
    //
    
    // DATE		: February 28, 2007
    // CODER	: Kurome (aucd29@gmail.com)
    // VERSION	: 1.0
    //
    //
    
    // Require
    // ----------------------------------------------------------
    // GDI+
    // installation Guide : http://sarangnamu.net/basic/basic_view.php?no=1369
    // Manual(kor) : http://winapi.co.kr/project/library/gdiplus/gdiplus.htm
    //
    // CImageLoader
    // http://www.sarangnamu.net/basic/basic_view.php?no=2839
    //
    // ----------------------------------------------------------
    // FIX : March 30, 2007
    // ----------------------------------------------------------
    // * Child dialog에서 GDI 특유에 DELETE 문제로 GDI+ 로 개발
    //   이미지로더 한개로 전체를 컨트롤 할 수 있게 변경한다.
    // * 주의점은 현재 생성자에서 이미지를 로딩하는데
    //   m_pLoadImage = g_imgplus.FindImageData(L"combobox");
    //   처음 APP 에서 해당 이미지를 로딩하고 가져다가 
    //   사용한다.
    //
    
    #include "stdafx.h"
    #include "ComboBoxExt.h"
    #include "Main Unit Manager.h"
    
    
    // CComboBoxExt
    
    IMPLEMENT_DYNAMIC(CComboBoxExt, CComboBox)
    
    CComboBoxExt::CComboBoxExt()
    : m_bLoad(false)
    , m_nMouseStatus(_CBO_MOUSEOUT)
    , m_bLoadBmp(false)
    , m_bFocus(false)
    {
    	m_crColor	   = RGB(127, 157, 185);
    	m_bmpButton[0] = NULL;
    	m_bmpButton[1] = NULL;
    	m_bmpButton[2] = NULL;
    	m_ptGap.x 	   = 0;
    	m_ptGap.y 	   = 0;
    
    #ifdef _GDIPLUS_H
    	m_pLoadImage   = g_imgplus.FindImageData(L"combobox");
    	m_crPen		   = Color(127, 157, 185);
    #endif
    }
    
    CComboBoxExt::~CComboBoxExt()
    {
    #ifdef _GDIPLUS_H
    	//delete m_crPen;
    #else
    	ft.DeleteObject();
    //	m_pen.DeleteObject();
    #endif
    }
    
    
    BEGIN_MESSAGE_MAP(CComboBoxExt, CComboBox)
    	ON_WM_ERASEBKGND()
    	ON_WM_MOUSEMOVE()
    	ON_WM_PAINT()
    	ON_WM_LBUTTONDOWN()
    	ON_WM_LBUTTONUP()
    	ON_WM_KILLFOCUS()
    	ON_CONTROL_REFLECT(CBN_SELCHANGE, &CComboBoxExt::OnCbnSelchange)
    	ON_WM_MOUSEWHEEL()
    	ON_CONTROL_REFLECT(CBN_SETFOCUS, &CComboBoxExt::OnCbnSetfocus)
    END_MESSAGE_MAP()
    
    
    // CComboBoxExt message handlers
    
    void CComboBoxExt::InitObject(CDC* pDC)
    {
    	if (!m_bLoad)
    	{
    		GetClientRect(m_rect);
    
    #ifdef _GDIPLUS_H
    		m_memBitmap = new Bitmap(m_rect.Width(), m_rect.Height());
    		m_mDCPlus	= new Graphics(m_memBitmap);
    
    		SolidBrush* m_brBackground;
    		if(GetStyle() & ES_READONLY || GetStyle() & WS_DISABLED)
    		{
    			m_crPen			= Color(201, 199, 186);
    
    			// setting brush
    			m_crBgColor		= GetSysColor(COLOR_3DFACE);
    			m_brBackground	= new SolidBrush(Color(GetRValue(m_crBgColor), GetGValue(m_crBgColor), GetBValue(m_crBgColor)));
    			
    		}
    		else
    		{
    			m_brBackground	= new SolidBrush(Color(255, 255, 255));
    		}
    
    		Pen pen(m_crPen);
    		m_mDCPlus->FillRectangle(m_brBackground, 0, 0, m_rect.Width(), m_rect.Height());		
    		m_mDCPlus->DrawRectangle(&pen, Rect(0, 0, m_rect.Width() - 1, m_rect.Height() - 1));
    
    		delete m_brBackground;
    
    		m_ptGap.x = m_rect.Width() - m_pLoadImage->m_pImg[0]->GetWidth() - 2;
    		m_ptGap.y = static_cast<int>((m_rect.Height() - m_pLoadImage->m_pImg[0]->GetHeight()) / 2.0F);
    		m_rectButtonArea.SetRect(m_ptGap.x, 0, m_rect.right, m_rect.bottom);
    
    		if (GetStyle() & CBS_DROPDOWNLIST)
    		{
    			m_rectArea = m_rect;
    		}
    		else
    		{
    			m_rectArea = m_rectButtonArea;
    		}
    
    #else
    		m_mDC.CreateCompatibleDC(pDC);
    		m_mBitmap.CreateCompatibleBitmap(pDC, m_rect.Width(), m_rect.Height());
    		m_mDC.SelectObject(&m_mBitmap);
    
    		// 메모리 DC 생성
    		mDC[0].CreateCompatibleDC(pDC);		// OUT
    		mDC[1].CreateCompatibleDC(pDC);		// OVER
    		mDC[2].CreateCompatibleDC(pDC);		// DOWN
    
    		if (m_bmpButton[0] != NULL)
    		{
    			mDC[0].SelectObject(m_bmpButton[0]);	// OUT
    			mDC[1].SelectObject(m_bmpButton[1]);	// OVER
    			mDC[2].SelectObject(m_bmpButton[2]);	// DOWN
    		}
    
    		// GAP 부분 브러시 생성
    		// READONLY 일 경우 색상을 바탕색으로
    		if(GetStyle() & ES_READONLY || GetStyle() & WS_DISABLED)
    		{
    			m_crBgColor = GetSysColor(COLOR_3DFACE);
    			m_crColor   = RGB(201, 199, 186);
    		}
    		else
    		{
    			m_crBgColor = RGB(255, 255, 255);
    		}
    
    		/*int PenStyle = PS_GEOMETRIC | PS_SOLID | PS_ENDCAP_SQUARE;
    		CPen m_pen;
    		m_pen.CreatePen(PenStyle, 1, m_crColor);*/
    
    		// FONT
    		LOGFONT lf;		
    		memset(&lf, 0, sizeof(LOGFONT));
    		GetFont()->GetLogFont(&lf);
    		ft.CreateFontIndirect(&lf);
    		m_mDC.SelectObject(&ft);
    
    		// SIZE
    		if (m_bmpButton[0] != NULL)
    		{
    			m_bmpButton[0]->GetObject(sizeof(BITMAP), &bm);
    			m_ptGap.x = m_rect.Width() - bm.bmWidth - 2;
    			m_ptGap.y = static_cast<int>((m_rect.Height() - bm.bmHeight) / 2.0F);
    		}
    		
    		// invalidate를 위해 위치를 설정
    		m_rectButtonArea.SetRect(m_ptGap.x, 0, m_rect.right, m_rect.bottom);
    
    		// 드랍 다운 스타일 일 경우
    		// 전체 영역을 아닐 경우는 버튼 위치만
    		// 이벤트를 적용 시키자.
    		if (GetStyle() & CBS_DROPDOWNLIST)
    		{
    			m_rectArea = m_rect;
    		}
    		else
    		{
    			m_rectArea = m_rectButtonArea;
    		}
    #endif;
    		m_bLoad = true;		
    	}
    }
    
    BOOL CComboBoxExt::OnEraseBkgnd(CDC* pDC)
    {
    	return true;
    }
    
    void CComboBoxExt::SetBorderColor(COLORREF crColor)
    {
    	m_crColor = crColor;
    }
    
    void CComboBoxExt::SetImages(CImageData* pImages)
    {
    	m_bmpButton[_CBO_MOUSEOUT]  = *pImages->m_pBmp++;
    	m_bmpButton[_CBO_MOUSEOVER] = *pImages->m_pBmp++;
    	m_bmpButton[_CBO_MOUSEDOWN] = *pImages->m_pBmp;
    }
    
    void CComboBoxExt::OnMouseMove(UINT nFlags, CPoint point)
    {
    	CheckButton(&point);
    
    	CComboBox::OnMouseMove(nFlags, point);
    }
    
    void CComboBoxExt::OnPaint()
    {
    	CPaintDC dc(this); // device context for painting
    
    	InitObject(&dc);
    
    #ifdef _GDIPLUS_H
    
    	Graphics g(dc);
    	m_mDCPlus->DrawImage(m_pLoadImage->m_pImg[m_nMouseStatus], m_ptGap.x, m_ptGap.y, 
    		m_pLoadImage->m_pImg[m_nMouseStatus]->GetWidth(),
    		m_pLoadImage->m_pImg[m_nMouseStatus]->GetHeight());
    	g.DrawImage(m_memBitmap, 0, 0);
    	
    #else
    
    	if (m_rectArea != m_rectButtonArea)
    	{
    		m_mDC.FillSolidRect(0, 0, m_rect.Width(), m_rect.Height(), m_crBgColor);
    		m_mDC.FrameRect(m_rect, &CBrush(m_crColor));
    		
    		CString szCurentText;
    		int nPos = GetCurSel();
    
    		if (nPos != -1)
    		{
    			GetLBText(nPos , szCurentText);
    
    			if (m_bFocus && szCurentText)
    			{
    				m_mDC.FillSolidRect(3, 3, m_rectButtonArea.left - 6, m_rect.Height() - 6 , GetSysColor(COLOR_HIGHLIGHT));
    			}
    
    			if (szCurentText.GetLength())
    			{
    				m_mDC.TextOut(4, 4, szCurentText);
    			}
    		}
    	}
    	dc.BitBlt(0, 0, m_rect.Width(), m_rect.Height(), &m_mDC, 0, 0, SRCCOPY);
    	
    	if (m_bmpButton[0])
    	{
    		dc.BitBlt(m_ptGap.x, m_ptGap.y, bm.bmWidth, bm.bmHeight, &mDC[m_nMouseStatus], 0, 0, SRCCOPY);
    	}
    
    #endif;
    }
    
    void CComboBoxExt::OnLButtonDown(UINT nFlags, CPoint point)
    {
    	if (m_rectArea.PtInRect(point))
    	{
    		m_nMouseStatus = _CBO_MOUSEDOWN;
    		Invalidate();
    		ReleaseCapture();
    	}
    	
    	CComboBox::OnLButtonDown(nFlags, point);
    }
    
    void CComboBoxExt::OnLButtonUp(UINT nFlags, CPoint point)
    {
    	m_nMouseStatus = _CBO_MOUSEOUT;
    	Invalidate();
    
    	CComboBox::OnLButtonUp(nFlags, point);
    }
    
    void CComboBoxExt::OnKillFocus(CWnd* pNewWnd)
    {
    	CComboBox::OnKillFocus(pNewWnd);
    
    	m_bFocus  = false;
    }
    
    void CComboBoxExt::OnCbnSelchange()
    {
    	m_bFocus = true;
    }
    
    BOOL CComboBoxExt::OnMouseWheel(UINT nFlags, short zDelta, CPoint pt)
    {
    	CheckButton(&pt);
    	return CComboBox::OnMouseWheel(nFlags, zDelta, pt);
    }
    
    void CComboBoxExt::CheckButton(CPoint* pt)
    {
    	if (m_rectArea.PtInRect(*pt))
    	{
    		if (m_nMouseStatus != _CBO_MOUSEDOWN && m_nMouseStatus != _CBO_MOUSEOVER)
    		{
    			m_nMouseStatus = _CBO_MOUSEOVER;
    			Invalidate();
    			SetCapture();
    		}
    	}
    	else
    	{
    		if (m_nMouseStatus != _CBO_MOUSEDOWN)
    		{
    			ReleaseCapture();
    			m_nMouseStatus = _CBO_MOUSEOUT;
    			Invalidate();
    		}
    	}
    }
    
    void CComboBoxExt::OnCbnSetfocus()
    {
    	m_bFocus = true;
    }
    
    
    header
    #pragma once #include "ImageLoader.h" // CComboBoxExt enum MouseStatus { _CBO_MOUSEOUT = 0, _CBO_MOUSEOVER, _CBO_MOUSEDOWN };
    class CComboBoxExt : public CComboBox
    {
    	DECLARE_DYNAMIC(CComboBoxExt)
    
    public:
    	CComboBoxExt();
    	virtual ~CComboBoxExt();
    
    protected:
    	CDC mDC[7], m_mDC;
    	int m_nMouseStatus;
    	bool m_bLoad, m_bLoadBmp, m_bFocus;
    	CFont ft;
    	CRect m_rect, rectOutline, rectGap;
    	CRect m_rectArea, m_rectButtonArea;
    	BITMAP bm;
    	CPoint m_ptGap;
    	CBrush m_brOutline, m_brGap;	
    	CBitmap mBitmap[4], m_mBitmap;
    	CBitmap* m_bmpButton[3];
    	COLORREF m_crColor, m_crBgColor;
    	CBitmap mBitmap2;
    
    #ifdef _GDIPLUS_H
    	CImageData* m_pLoadImage;
    	Graphics* m_mDCPlus;
    	Bitmap* m_memBitmap;
    	Color m_crPen; //, m_crBgColorPlus;
    #endif
    
    	virtual void InitObject(CDC* pDC);
    	afx_msg BOOL OnEraseBkgnd(CDC* pDC);
    	afx_msg void OnMouseMove(UINT nFlags, CPoint point);
    	afx_msg void OnPaint();
    	afx_msg void OnLButtonDown(UINT nFlags, CPoint point);
    	afx_msg void OnLButtonUp(UINT nFlags, CPoint point);
    	afx_msg void OnKillFocus(CWnd* pNewWnd);
    	afx_msg void OnCbnSelchange();
    	afx_msg BOOL OnMouseWheel(UINT nFlags, short zDelta, CPoint pt);
    	afx_msg void OnCbnSetfocus();
    	void CheckButton(CPoint* pt);
    
    	DECLARE_MESSAGE_MAP()
    public:
    	void SetBorderColor(COLORREF crColor);
    	void SetImages(CImageData* pImages);	
    };
    

    Comment


    입력하3 1508772432



    Locations of visitors to this page