扫一扫
分享文章到微信
扫一扫
关注官方公众号
至顶头条
void CMenuButton::PreSubclassWindow() { ModifyStyle( 0, BS_OWNERDRAW ); //设置按钮属性为自绘式 CButton::PreSubclassWindow(); } |
void CMenuButton::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct) { CDC *pDC = CDC::FromHandle( lpDrawItemStruct->hDC ); m_ButRect = lpDrawItemStruct->rcItem; //获取按钮尺寸 int nSavedDC = pDC->SaveDC(); VERIFY( pDC ); DrawButton(pDC); //绘制按钮 pDC->RestoreDC( nSavedDC ); } |
void CMenuButton::DrawButton(CDC *pDC) { m_LRect.SetRect( m_ButRect.left, m_ButRect.top, m_ButRect.right-21, m_ButRect.bottom ); //按钮主体区尺寸 m_RRect.SetRect( m_ButRect.right-20, m_ButRect.top, m_ButRect.right, m_ButRect.bottom ); //按钮选择区尺寸 CPen Pen; Pen.CreatePen(PS_SOLID, 1, RGB(192,192,192) ); pDC->SelectObject( &Pen ); pDC->FillSolidRect( m_ButRect, m_BackColor ); //画背景 switch( m_State ) //不同状态画不同边框 { case 0: //正常按钮 pDC->DrawEdge( &m_LRect, BDR_RAISEDINNER, BF_RECT ); pDC->DrawEdge( &m_RRect, BDR_RAISEDINNER, BF_RECT ); break; case 1: //鼠标进入时的按钮 pDC->DrawEdge( &m_LRect, BDR_RAISEDINNER, BF_RECT ); pDC->DrawEdge( &m_RRect, BDR_RAISEDINNER, BF_RECT ); pDC->MoveTo( m_ButRect.TopLeft() ); pDC->LineTo( m_ButRect.right, m_ButRect.top ); break; case 2: //单击按钮主体区时的按钮 pDC->DrawEdge( &m_RRect, BDR_RAISEDINNER, BF_RECT ); break; case 3: //单击按钮选择区时的按钮 pDC->DrawEdge( &m_LRect, BDR_RAISEDINNER, BF_RECT ); break; } POINT m_pt[3], m_ptCentre; //箭头坐标(三个顶点) m_ptCentre = m_RRect.CenterPoint(); //选择区中点位置 m_pt[0].x = m_ptCentre.x-3; //计算箭头坐标 m_pt[0].y = m_ptCentre.y-2; m_pt[1].x = m_ptCentre.x+4; m_pt[1].y = m_ptCentre.y-2; m_pt[2].x = m_ptCentre.x; m_pt[2].y = m_ptCentre.y+2; pDC->SelectStockObject( BLACK_BRUSH ); //定义画刷(黑色) CRgn rgn; rgn.CreatePolygonRgn( m_pt, 3, ALTERNATE ); pDC->PaintRgn( &rgn ); //画选择区箭头 pDC->SetTextColor( m_ForeColor ); //画主体区文字 pDC->SetBkMode( TRANSPARENT ); pDC->DrawText( m_strText, &m_LRect, DT_SINGLELINE | DT_CENTER | DT_VCENTER | DT_END_ELLIPSIS); } |
void CMenuButton::OnMouseMove(UINT nFlags, CPoint point) { if( !b_InFlag || GetCapture()!=this ) //鼠标进入按钮 { b_InFlag = true; //设置进入标志 SetCapture(); //捕获鼠标 m_State = 1; //置按钮状态(1-当前按钮) if( b_ClickFlag ) //检测单击选择区标志 { m_Menu.Detach(); //清除打开的菜单 m_Menu.DestroyMenu(); b_ClickFlag = false; } Invalidate(); //重绘按钮 } else { if ( !m_ButRect.PtInRect(point) ) //鼠标离开按钮 { b_InFlag = false; //清除进入标志 ReleaseCapture(); //释放鼠标捕获 b_ClickBut = false; //清除单击标志 m_State = 0; //置按钮状态(0-正常按钮) if( b_ClickFlag ) //检测单击选择区标志 { m_Menu.Detach(); //清除打开的菜单 m_Menu.DestroyMenu(); b_ClickFlag = false; } Invalidate(); //重绘按钮 } } CButton::OnMouseMove(nFlags, point); } |
void CMenuButton::OnLButtonDown(UINT nFlags, CPoint point) { if( m_LRect.PtInRect(point) ) //单击按钮主体区 { m_State = 2; //置按钮状态(2-正常按钮) b_ClickBut = true; //设置单击按钮标志 Invalidate(); //重绘按钮 } else if( m_RRect.PtInRect(point) && m_MenuID ) //单击选择区 { m_State = 3; b_ClickBut = false; //清除单击按钮标志 Invalidate(); //重绘按钮 b_ClickFlag = !b_ClickFlag; //单击选择区标志 if( b_ClickFlag ) //一次单击,弹出菜单 { CRect rect = m_RRect; ClientToScreen(rect); //转换为屏幕坐标 point = rect.BottomRight(); point.x -= rect.Width(); //设置弹出菜单的位置 VERIFY(m_Menu.LoadMenu(m_MenuID)); //装入菜单资源 CMenu* pPopup = m_Menu.GetSubMenu(0); ASSERT(pPopup != NULL); CWnd* pWndPopupOwner = this; while (pWndPopupOwner->GetStyle() & WS_CHILD) pWndPopupOwner = pWndPopupOwner->GetParent(); pPopup->TrackPopupMenu(TPM_LEFTALIGN | TPM_LEFTBUTTON, point.x, point.y, pWndPopupOwner); //弹出菜单 } else //再次单击,清除菜单 { m_Menu.Detach(); m_Menu.DestroyMenu(); } } CButton::OnLButtonDown(nFlags, point); } |
void CMenuButton::OnLButtonUp(UINT nFlags, CPoint point) { m_State = 0; //恢复为正常按钮 Invalidate(); //重绘按钮 CButton::OnLButtonUp(nFlags, point); } |
如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。
现场直击|2021世界人工智能大会
直击5G创新地带,就在2021MWC上海
5G已至 转型当时——服务提供商如何把握转型的绝佳时机
寻找自己的Flag
华为开发者大会2020(Cloud)- 科技行者