📄 tabctrl.cpp
字号:
// fill the background CBrush brush, *pOldBrush; brush.CreateSolidBrush(m_crBack); pOldBrush = pDC->SelectObject(&brush); pDC->PatBlt(rect.left, rect.top, rect.Width(), rect.Height(), PATCOPY); pDC->SelectObject(pOldBrush); brush.DeleteObject(); // draw border CRect rcUp, rcDown; rcUp.SetRect (rect.left, rect.top, rect.right, rect.CenterPoint().y); rcDown.SetRect(rect.left, rect.CenterPoint().y, rect.right, rect.bottom); pDC->Draw3dRect(rcUp, m_crGuideN, m_crGuideN); pDC->Draw3dRect(rcDown, m_crGuideN, m_crGuideN); rcUp.DeflateRect(1,1,1,1); rcDown.DeflateRect(1,1,1,1); if(nCheck == TC_SCROLL_UP) { rcUp.OffsetRect(1,1); pDC->Draw3dRect(rcDown, RGB(255,255,255), RGB( 68, 68, 68)); } else if(nCheck == TC_SCROLL_DOWN) { rcDown.OffsetRect(1,1); pDC->Draw3dRect(rcUp, RGB(255,255,255), RGB( 68, 68, 68)); } else { pDC->Draw3dRect(rcDown, RGB(255,255,255), RGB( 68, 68, 68)); pDC->Draw3dRect(rcUp, RGB(255,255,255), RGB( 68, 68, 68)); } // draw up arrow int nArrowHeight = rcUp.Height() / 3; POINT pPoint[3]; pPoint[0].x = rcUp.CenterPoint().x; pPoint[0].y = rcUp.top+nArrowHeight; pPoint[1].x = rcUp.CenterPoint().x+nArrowHeight; pPoint[1].y = rcUp.top+nArrowHeight*2; pPoint[2].x = rcUp.CenterPoint().x-nArrowHeight; pPoint[2].y = rcUp.top+nArrowHeight*2; DrawArrow(pDC, pPoint, RGB( 0, 0, 0)); // draw down arrow pPoint[0].x = rcDown.CenterPoint().x; pPoint[0].y = rcDown.top+nArrowHeight*2; pPoint[1].x = rcDown.CenterPoint().x-nArrowHeight+1; pPoint[1].y = rcDown.top+nArrowHeight+1; pPoint[2].x = rcDown.CenterPoint().x+nArrowHeight-1; pPoint[2].y = rcDown.top+nArrowHeight+1; DrawArrow(pDC, pPoint, RGB( 0, 0, 0));}void RxTabCtrl::DrawArrow(CDC *pDC, POINT *point, COLORREF color){ CBrush brush, *pOldBrush; brush.CreateSolidBrush(color); pOldBrush = pDC->SelectObject(&brush); pDC->BeginPath(); pDC->MoveTo(point[0].x, point[0].y); pDC->LineTo(point[1].x, point[1].y); pDC->LineTo(point[2].x, point[2].y); pDC->EndPath(); pDC->FillPath(); pDC->SelectObject(pOldBrush); brush.DeleteObject();}void RxTabCtrl::RecalcWindow (int cx, int cy){ // get the last tab position if(m_ArrayTabText.GetSize() > 0) { int nTabPixelEnd; GetTabPixel(m_ArrayTabText.GetSize()-1, NULL, &nTabPixelEnd); int nScrollPixel; if(m_nSide == TC_SIDE_TOP || m_nSide == TC_SIDE_BOTTOM) nScrollPixel = cx; else nScrollPixel = cy; if(nTabPixelEnd > nScrollPixel) { m_bScrollShow = TRUE; GetTabPixel(m_nCurTab, NULL, &nTabPixelEnd); if(nTabPixelEnd > (nScrollPixel-m_nTabHeight*2)) m_nScrollPos = (nScrollPixel-m_nTabHeight*2) - nTabPixelEnd; } else { m_bScrollShow = FALSE; m_nScrollPos = 0; } Invalidate(); } else Invalidate();}void RxTabCtrl::OnSize (UINT nType, int cx, int cy) { CWnd::OnSize(nType, cx, cy); RecalcWindow(cx, cy);}void RxTabCtrl::OnLButtonDown (UINT nFlags, CPoint point) { SetCapture(); m_nOldHitTest = HitTest(point); if(m_nOldHitTest == TC_SCROLL_UP || m_nOldHitTest == TC_SCROLL_DOWN) Invalidate(); CWnd::OnLButtonDown(nFlags, point);}void RxTabCtrl::OnLButtonUp (UINT nFlags, CPoint point) { if(GetCapture() == this) { ReleaseCapture(); int nHitTest = HitTest(point); if(nHitTest > -1) { int nMovePixel = m_nTabWingSize+m_nTabWidth; if(nHitTest == TC_SCROLL_UP) { if(m_nScrollPos < 0) { if(m_nScrollPos > -nMovePixel) m_nScrollPos = 0; else m_nScrollPos += nMovePixel; } else m_nScrollPos = 0; m_nOldHitTest = -1; Invalidate(); } else if(nHitTest == TC_SCROLL_DOWN) { int nTabPixelEnd; GetTabPixel(m_ArrayTabText.GetSize()-1, NULL, &nTabPixelEnd); nTabPixelEnd += m_nScrollPos; int nScrollPixel; CRect rcClient; GetClientRect(rcClient); if(m_nSide == TC_SIDE_TOP || m_nSide == TC_SIDE_BOTTOM) nScrollPixel = rcClient.right - (m_nTabHeight*2); else nScrollPixel = rcClient.bottom - (m_nTabHeight*2); if(nTabPixelEnd > nScrollPixel) { if(nTabPixelEnd - nScrollPixel < nMovePixel) m_nScrollPos -= (nTabPixelEnd - nScrollPixel); else m_nScrollPos -= nMovePixel; } m_nOldHitTest = -1; Invalidate(); } else if(nHitTest == m_nOldHitTest) { int nOldTab = SetCurTab(nHitTest); NMTC_CTRL hdrTab; hdrTab.hdr.hwndFrom = m_hWnd; hdrTab.hdr.idFrom = GetDlgCtrlID(); hdrTab.hdr.code = TC_CHANGED; hdrTab.nOldTab = nOldTab; hdrTab.nCurTab = m_nCurTab; CWnd* pOwner = GetOwner(); if (pOwner) pOwner->SendMessage(WM_NOTIFY, hdrTab.hdr.idFrom, (LPARAM)&hdrTab); } } else if(m_nOldHitTest == TC_SCROLL_UP || m_nOldHitTest == TC_SCROLL_DOWN) { m_nOldHitTest = -1; Invalidate(); } } CWnd::OnLButtonUp(nFlags, point);}void RxTabCtrl::OnLButtonDblClk(UINT nFlags, CPoint point) { SendMessage(WM_LBUTTONDOWN, 0, MAKELPARAM(point.x, point.y));}/////////////////////////////////////////////////////////////////////////////void RxTabCtrl::SetTabSide (int nIndex){ m_nSide = nIndex;}void RxTabCtrl::AddTab (BOOL bFocusAndRedraw){ int iSize = m_ArrayTabText.GetSize(); CString strNum; strNum.Format(_T("%d"), iSize + 1); m_ArrayTabText.Add(strNum); if(bFocusAndRedraw) { m_nCurTab = m_ArrayTabText.GetSize()-1; CRect rcClient; GetClientRect(rcClient); RecalcWindow(rcClient.Width(), rcClient.Height()); }}void RxTabCtrl::AddTab (LPCTSTR text, BOOL bFocusAndRedraw){ m_ArrayTabText.Add(text); if(bFocusAndRedraw) { m_nCurTab = m_ArrayTabText.GetSize()-1; CRect rcClient; GetClientRect(rcClient); RecalcWindow(rcClient.Width(), rcClient.Height()); }}void RxTabCtrl::RemoveTab(int nIndex){ if (m_nCurTab == (m_ArrayTabText.GetSize() - 1)) m_nCurTab--; m_ArrayTabText.RemoveAt(nIndex); for (int i = nIndex; i < m_ArrayTabText.GetSize(); i++) m_ArrayTabText[i].Format(_T("%d"), i + 1); CRect rcClient; GetClientRect(rcClient); RecalcWindow(rcClient.Width(), rcClient.Height());}void RxTabCtrl::RemoveAllTab(){ m_ArrayTabText.RemoveAll(); m_bScrollShow = FALSE; m_nScrollPos = 0; m_nCurTab = -1; m_nOldHitTest = -1; Invalidate();}int RxTabCtrl::SetCurTab (int nIndex){ if(m_nCurTab == nIndex) return nIndex; int nOldTab; nOldTab = m_nCurTab; m_nCurTab = nIndex; Invalidate(); return nOldTab;}int RxTabCtrl::GetCurTab (){ return m_nCurTab;}/////////////////////////////////////////////////////////////////////////////void RxTabCtrl::GetTabPixel (int nIndex, int *pBegin, int *pEnd){ int nBegin = m_nLeftMargin + (m_nTabWingSize + m_nTabWidth) * nIndex; int nEnd = nBegin + m_nTabWingSize*2 + m_nTabWidth; if(pBegin != NULL) *pBegin = nBegin; if(pEnd != NULL) *pEnd = nEnd;}void RxTabCtrl::GetTabRegion (int nIndex, POINT *point, int nScrollPos){ CRect rcClient; GetClientRect(rcClient); int nStepBegin = (m_nLeftMargin + (m_nTabWingSize + m_nTabWidth) * nIndex) + nScrollPos; if(m_nSide == TC_SIDE_TOP) { point[0].x = nStepBegin; point[0].y = rcClient.bottom; point[1].x = nStepBegin+m_nTabWingSize; point[1].y = rcClient.bottom-m_nTabHeight; point[2].x = nStepBegin+m_nTabWingSize+m_nTabWidth; point[2].y = rcClient.bottom-m_nTabHeight; point[3].x = nStepBegin+m_nTabWingSize*2+m_nTabWidth; point[3].y = rcClient.bottom; } else if(m_nSide == TC_SIDE_BOTTOM) { point[0].x = nStepBegin; point[0].y = rcClient.top; point[1].x = nStepBegin+m_nTabWingSize; point[1].y = rcClient.top+m_nTabHeight; point[2].x = nStepBegin+m_nTabWingSize+m_nTabWidth; point[2].y = rcClient.top+m_nTabHeight; point[3].x = nStepBegin+m_nTabWingSize*2+m_nTabWidth; point[3].y = rcClient.top; } else if(m_nSide == TC_SIDE_LEFT) { point[0].x = rcClient.right; point[0].y = nStepBegin; point[1].x = rcClient.right-m_nTabHeight; point[1].y = nStepBegin+m_nTabWingSize; point[2].x = rcClient.right-m_nTabHeight; point[2].y = nStepBegin+m_nTabWingSize+m_nTabWidth; point[3].x = rcClient.right; point[3].y = nStepBegin+m_nTabWingSize*2+m_nTabWidth; } else if(m_nSide == TC_SIDE_RIGHT) { point[0].x = rcClient.left; point[0].y = nStepBegin; point[1].x = rcClient.left+m_nTabHeight; point[1].y = nStepBegin+m_nTabWingSize; point[2].x = rcClient.left+m_nTabHeight; point[2].y = nStepBegin+m_nTabWingSize+m_nTabWidth; point[3].x = rcClient.left; point[3].y = nStepBegin+m_nTabWingSize*2+m_nTabWidth; }}int RxTabCtrl::HitTest (CPoint point){ int nCount = m_ArrayTabText.GetSize(); // check scroll button if(m_bScrollShow) { CRect rcClient, rcTemp; GetClientRect(rcClient); if(m_nSide == TC_SIDE_TOP) { rcTemp.left = rcClient.right - m_nTabHeight*2; rcTemp.right = rcClient.right - m_nTabHeight; rcTemp.top = rcClient.bottom - m_nTabHeight; rcTemp.bottom = rcClient.bottom; if(rcTemp.PtInRect(point)) return TC_SCROLL_UP; rcTemp.left = rcClient.right - m_nTabHeight; rcTemp.right = rcClient.right; if(rcTemp.PtInRect(point)) return TC_SCROLL_DOWN; } else if(m_nSide == TC_SIDE_BOTTOM) { rcTemp.left = rcClient.right - m_nTabHeight*2; rcTemp.right = rcClient.right - m_nTabHeight; rcTemp.top = rcClient.top; rcTemp.bottom = rcClient.top + m_nTabHeight; if(rcTemp.PtInRect(point)) return TC_SCROLL_UP; rcTemp.left = rcClient.right - m_nTabHeight; rcTemp.right = rcClient.right; if(rcTemp.PtInRect(point)) return TC_SCROLL_DOWN; } else if(m_nSide == TC_SIDE_LEFT) { rcTemp.left = rcClient.right - m_nTabHeight; rcTemp.right = rcClient.right; rcTemp.top = rcClient.bottom - m_nTabHeight*2; rcTemp.bottom = rcClient.bottom - m_nTabHeight; if(rcTemp.PtInRect(point)) return TC_SCROLL_UP; rcTemp.top = rcClient.bottom - m_nTabHeight; rcTemp.bottom = rcClient.bottom; if(rcTemp.PtInRect(point)) return TC_SCROLL_DOWN; } else if(m_nSide == TC_SIDE_RIGHT) { rcTemp.left = rcClient.left - m_nTabHeight; rcTemp.right = rcClient.left + m_nTabHeight; rcTemp.top = rcClient.bottom - m_nTabHeight*2; rcTemp.bottom = rcClient.bottom - m_nTabHeight; if(rcTemp.PtInRect(point)) return TC_SCROLL_UP; rcTemp.top = rcClient.bottom - m_nTabHeight; rcTemp.bottom = rcClient.bottom; if(rcTemp.PtInRect(point)) return TC_SCROLL_DOWN; } } // check tab index POINT pPoint[4]; CRgn rgn; if(m_nCurTab > -1) { GetTabRegion(m_nCurTab, pPoint, m_nScrollPos); rgn.CreatePolygonRgn(pPoint, 4, WINDING); if(rgn.PtInRegion(point)) { rgn.DeleteObject(); return m_nCurTab; } rgn.DeleteObject(); } for(int i = nCount-1; i >= 0; i--) { if(m_nCurTab == i) continue; GetTabRegion(i, pPoint, m_nScrollPos); rgn.CreatePolygonRgn(pPoint, 4, WINDING); if(rgn.PtInRegion(point)) { rgn.DeleteObject(); return i; } rgn.DeleteObject(); } return -1;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -