//********************* // PseudoButtons: replaces CButton class for simple button operations // This solution illustrates // 1) Getting Parent and Message ID from CWnd base class // 2) Smart refrech on MouseMove // 3) Interesting Brush for drawing button window // 4) Handles loss of capture due to another windows action // 5) Uses same constructor as CButton // 6) Makes minimal changes in MsgEdit program (only the class types) // // AUTHOR: Chris Wild // DATE: October 20, 1997 //**************** #include "CPseudoButton.h" BEGIN_MESSAGE_MAP(CPseudoButton, CWnd) ON_WM_PAINT() ON_WM_LBUTTONUP() ON_WM_MOUSEMOVE() ON_WM_CAPTURECHANGED() END_MESSAGE_MAP() CPseudoButton::CPseudoButton() { highLight=false; } BOOL CPseudoButton::Create(LPCTSTR lpszCaption, DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID) { return CWnd::Create(NULL, lpszCaption, dwStyle, rect, pParentWnd, nID, NULL); } CPseudoButton::~CPseudoButton() { DestroyWindow(); } void CPseudoButton::OnPaint() { CPaintDC dc(this); CBrush* pOldBrush; CBrush newBrush(HS_VERTICAL,BLUE); CRect rect; GetClientRect(&rect); pOldBrush = dc.SelectObject(&newBrush); CString title; GetWindowText(title); if (highLight) { //mouse is in the button window dc.SetTextColor(RED); dc.SetBkColor(GREEN); } else { //mouse is outside the button window dc.SetTextColor(BLACK); dc.SetBkColor(RED); } dc.Rectangle(rect); dc.DrawText(title, -1, &rect, DT_SINGLELINE | DT_CENTER | DT_VCENTER); dc.SelectObject(pOldBrush); } // respond on message WM_MOUSEMOVE // if mouse is in the button, set the highLight variable to be true // if the mouse move out of the button, set highLight to be false // and then redraw window // However if ther window is already highlight when mouse moves in window, ignore void CPseudoButton::OnMouseMove(UINT nFlags, CPoint point) { CRect myrect; GetClientRect(&myrect); if(myrect.PtInRect(point)) { if(GetCapture() == this) return; // avoid multiple redraws SetCapture(); highLight=true; } else { ReleaseCapture(); highLight=false; CString title; // display coordinates for fun title.Format("(%d,%d)",point.x,point.y); GetParent()->SetWindowText(title); } Invalidate(); } void CPseudoButton::OnLButtonUp() { GetParent()->SendMessage(WM_COMMAND, GetDlgCtrlID(),NULL); } void CPseudoButton::OnCaptureChanged(CWnd* pWnd) { GetParent()->SetWindowText("CaptureChanged"); Beep(4000,1000); if(GetCapture() != this) { ReleaseCapture(); highLight = false; Invalidate(); } CWnd::OnCaptureChanged(this); }