// LinkButton.cpp #include "stdafx.h" #include "LinkButton.h" #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif ///////////////////////////////////////////////////////////////////////////// // CLinkButton BEGIN_MESSAGE_MAP(CLinkButton, CButton) //{{AFX_MSG_MAP(CLinkButton) ON_WM_ERASEBKGND() ON_WM_MOUSEMOVE() ON_WM_LBUTTONDBLCLK() ON_WM_LBUTTONDOWN() ON_WM_LBUTTONUP() ON_WM_SETFOCUS() ON_WM_KILLFOCUS() //}}AFX_MSG_MAP END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CLinkButton message handlers void CLinkButton::OnLButtonDown(UINT nFlags, CPoint point) { if(!IsWindowEnabled()) return; LButtonDown=true; SetFocus(); CButton::OnLButtonDown(nFlags, point); } void CLinkButton::OnLButtonDblClk(UINT nFlags, CPoint point) { if(!IsWindowEnabled()) return; LButtonDown=true; SetFocus(); CButton::OnLButtonDblClk(nFlags, point); } void CLinkButton::OnMouseMove(UINT nFlags, CPoint point) { if(!IsWindowEnabled()) return; bool Over=MouseOver; if(!MouseOver) { MouseOver=true; if(!Captured) { SetCapture(); Captured=true; } } if(Captured) { CRect Rect; GetClientRect(Rect); if(!Rect.PtInRect(point)) { MouseOver=false; if(!LButtonDown) { //We can't release it if the Left button is being held down ReleaseCapture(); Captured=false; } } } if(Over!=MouseOver) { // If the mouse moved in or out of our ClientRect: Cursor=::SetCursor(Cursor); //Swap the value of Cursor as we swap Cursors Redraw(); } CButton::OnMouseMove(nFlags, point); } void CLinkButton::OnLButtonUp(UINT nFlags, CPoint point) { bool Clicked=false; if(Captured) { ReleaseCapture(); Captured=false; if(LButtonDown) { CRect Rect; GetClientRect(Rect); if(Rect.PtInRect(point)) Clicked=true; } } LButtonDown=false; if(Clicked) { MouseOver=false; // Releasing the button releases the resources: Cursor=::SetCursor(Cursor); //Swap the value of Cursor as we swap Cursors Redraw(); GetOwner()->SendMessage(WM_COMMAND, GetDlgCtrlID()); }else CButton::OnLButtonUp(nFlags, point); } BOOL CLinkButton::OnEraseBkgnd(CDC* pDC) {return true;} // return CButton::OnEraseBkgnd(pDC);} void CLinkButton::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct) { CDC* pDC=CDC::FromHandle(lpDrawItemStruct->hDC); // device context for painting if(MouseOver || KeyDown) { pDC->SelectObject(LinkFont); pDC->SetTextColor(LinkColor); }else{ pDC->SelectObject(TextFont); pDC->SetTextColor(TextColor); } pDC->FillSolidRect(&lpDrawItemStruct->rcItem, Focus ? FocusColor : ButtonColor); if(!IsWindowVisible()) return; CString S; GetWindowText(S); lpDrawItemStruct->rcItem.left=5; pDC->DrawText(S, &lpDrawItemStruct->rcItem, DT_LEFT|DT_SINGLELINE|DT_VCENTER); } void CLinkButton::OnSetFocus(CWnd* pOldWnd) { Focus=true; CButton::OnSetFocus(pOldWnd); } void CLinkButton::OnKillFocus(CWnd* pNewWnd) { Focus=false; CButton::OnKillFocus(pNewWnd); } BOOL CLinkButton::PreTranslateMessage(MSG* pMsg) { if(pMsg->wParam ==VK_RETURN) { //Let the Return and Enter keys operate the button when it has focus if(pMsg->message==WM_KEYDOWN) { if(KeyDown) return true; KeyDown=true; Redraw(); GetOwner()->PostMessage(WM_COMMAND, GetDlgCtrlID()); }else{ KeyDown=false; Redraw(); } return true; } return CButton::PreTranslateMessage(pMsg); }