본문 바로가기

Visual C++/CMFCButton

MFC CMFCButton example

MFC에서 CMFCButton을 이용하여 버튼 위에 이미지와 툴팁을 설정하는 예


여기서는 WM_MOUSEHOVER, WM_MOUSELEAVE 등의 메시지 핸들러가 사용되므로 앞서 소개한 페이지를 먼저 참조하세요


ToolWndButton.h

#pragma once



// ToolWndButton


class ToolWndButton : public CMFCButton

{

DECLARE_DYNAMIC(ToolWndButton)


public:

ToolWndButton();

ToolWndButton(int nID, int onBmpId, int offBmpId, CWnd* pParent, int posIdx);

virtual ~ToolWndButton();


protected:

DECLARE_MESSAGE_MAP()

public:

afx_msg void OnMouseMove(UINT nFlags, CPoint point);

afx_msg void OnMouseHover(UINT nFlags, CPoint point);

afx_msg void OnMouseLeave();

int m_onBmpId;

int m_offBmpId;

BOOL m_bTrackMouse;

BOOL m_bHover;

afx_msg void OnBnClicked();

int m_nID;

        int m_nIdx;


static CStringArray toolTipTitles;

static CStringArray toolTips;


static void InitTooltipString(void)

{

if(toolTipTitles.GetCount()<1)

{

toolTipTitles.Add(_T("밝기"));

toolTipTitles.Add(_T("밝기"));

toolTipTitles.Add(_T("대조"));

toolTipTitles.Add(_T("대조"));

}


if(toolTips.GetCount()<1)

{

toolTips.Add(_T("더 밝게"));

toolTips.Add(_T("더 어둡게"));

toolTips.Add(_T("강하게"));

toolTips.Add(_T("약하게"));

}

}

};




ToolWndButton.cpp

// ToolWndButton.cpp : implementation file

#include "stdafx.h"

#include "WndOpen.h"

#include "ToolWndButton.h"



// ToolWndButton


CStringArray ToolWndButton::toolTipTitles;

CStringArray ToolWndButton::toolTips;



IMPLEMENT_DYNAMIC(ToolWndButton, CMFCButton)



ToolWndButton::ToolWndButton()

: m_bTrackMouse(FALSE)

, m_bHover(FALSE)

, m_nID(0)

, m_nIdx(0)

{

}



ToolWndButton::ToolWndButton(int nID, int onBmpId, int offBmpId, CWnd* pParent, int posIdx)

{

// Create a bitmap button.


m_bTrackMouse = FALSE;

m_nID = nID;

m_onBmpId = onBmpId;

m_offBmpId = offBmpId;

m_nIdx = posIdx;


int size = 60; 

int gab = 10;

int top = posIdx*(size+gab);


this->Create(_T("툴 윈도우"), WS_CHILD|WS_VISIBLE|BS_BITMAP, 

  CRect(0,top,size,top+size), pParent, nID);


m_pToolTip = NULL; // this is Defined in afxbutton.h, as: CToolTipCtrl* CMFCButton m_pToolTip;  

        this->m_bDrawFocus = TRUE; 

        this->m_bHighlightChecked = FALSE; 

        this->m_bTransparent = FALSE;

        this->m_bTransparent = CMFCButton::BUTTONSTYLE_3D;

this->SetImage(offBmpId);

ToolWndButton::InitTooltipString();


TRACKMOUSEEVENT tme;

tme.cbSize = sizeof(tme);

tme.hwndTrack = m_hWnd;

tme.dwFlags = TME_LEAVE|TME_HOVER;

tme.dwHoverTime = 1;

TrackMouseEvent(&tme);

}


ToolWndButton::~ToolWndButton()

{

}



BEGIN_MESSAGE_MAP(ToolWndButton, CMFCButton)

ON_WM_MOUSEMOVE()

ON_WM_MOUSEHOVER()

ON_WM_MOUSELEAVE()

ON_CONTROL_REFLECT(BN_CLICKED, &ToolWndButton::OnBnClicked)

END_MESSAGE_MAP()




// ToolWndButton message handlers



void ToolWndButton::OnMouseMove(UINT nFlags, CPoint point)

{

// TODO: Add your message handler code here and/or call default

if(!m_bTrackMouse)

{

TRACKMOUSEEVENT tme;

tme.cbSize = sizeof(tme);

tme.hwndTrack = m_hWnd;

tme.dwFlags = TME_LEAVE|TME_HOVER;

tme.dwHoverTime = 1;

if(TrackMouseEvent(&tme))

{

//m_bTrackMouse = TRUE;

}

}

CMFCButton::OnMouseMove(nFlags, point);

}



void ToolWndButton::OnMouseHover(UINT nFlags, CPoint point)

{

// TODO: Add your message handler code here and/or call default

if(m_bHover) 

{

return CMFCButton::OnMouseHover(nFlags, point);

}

m_bHover = TRUE;

//AfxMessageBox(_T("OnMouseHover()"));

SetMouseCursorHand();

if(m_pToolTip == NULL) 

    {

        m_pToolTip = new CToolTipCtrl;

        m_pToolTip->Create(this,TTS_ALWAYSTIP);

        m_pToolTip->SetDelayTime(TTDT_INITIAL,0);

        m_pToolTip->SetTitle(TTI_INFO,toolTipTitles.GetAt(m_nIdx));

        m_pToolTip->AddTool(this,toolTips.GetAt(m_nIdx));

        m_pToolTip->Activate(TRUE);

    }    

    SetImage(m_onBmpId);

    Invalidate();   


CMFCButton::OnMouseHover(nFlags, point);

}



void ToolWndButton::OnMouseLeave()

{

// TODO: Add your message handler code here and/or call default

m_bHover = FALSE;


SetImage(m_offBmpId);

        Invalidate();


        CToolTipCtrl *t = m_pToolTip;

        m_pToolTip = NULL;

        delete(t); 


CMFCButton::OnMouseLeave();

}



void ToolWndButton::OnBnClicked()

{

// TODO: Add your control notification handler code here

CString msg;

msg.Format(_T("눌린버튼 %d"), m_nID);

AfxMessageBox(msg);

}