| | |
| | | BEGIN_MESSAGE_MAP(CJobSlotGrid, CWnd) |
| | | ON_WM_PAINT() |
| | | ON_WM_ERASEBKGND() |
| | | ON_WM_MOUSEMOVE() |
| | | ON_WM_MOUSELEAVE() |
| | | ON_WM_LBUTTONDOWN() |
| | | ON_WM_LBUTTONUP() |
| | | END_MESSAGE_MAP() |
| | | |
| | | CJobSlotGrid::CJobSlotGrid() { |
| | |
| | | m_nRows = nRows; |
| | | m_nCols = nCols; |
| | | m_vSlotStatus.assign(nRows, std::vector<bool>(nCols, false)); |
| | | m_vSlotClickable.assign(nRows, std::vector<bool>(nCols, false)); |
| | | |
| | | // 初始化文本数组 |
| | | m_vSlotText.assign(nRows, std::vector<CString>(nCols)); |
| | |
| | | Invalidate(); |
| | | } |
| | | |
| | | void CJobSlotGrid::SetSlotClickable(int nRow, int nCol, bool bClickable) |
| | | { |
| | | if (nRow >= 0 && nRow < m_nRows && nCol >= 0 && nCol < m_nCols) { |
| | | m_vSlotClickable[nRow][nCol] = bClickable; |
| | | } |
| | | } |
| | | |
| | | bool CJobSlotGrid::IsSlotClickable(int nRow, int nCol) const |
| | | { |
| | | if (nRow >= 0 && nRow < m_nRows && nCol >= 0 && nCol < m_nCols) { |
| | | return m_vSlotClickable[nRow][nCol]; |
| | | } |
| | | return false; |
| | | } |
| | | |
| | | void CJobSlotGrid::SetSlotClickCallback(SlotClickCallback fnCallback) |
| | | { |
| | | m_fnSlotClickCallback = fnCallback; |
| | | } |
| | | |
| | | BOOL CJobSlotGrid::OnEraseBkgnd(CDC* pDC) { |
| | | return TRUE; |
| | | } |
| | | |
| | | void CJobSlotGrid::OnMouseMove(UINT nFlags, CPoint point) |
| | | { |
| | | TRACKMOUSEEVENT tme = { sizeof(TRACKMOUSEEVENT), TME_LEAVE, m_hWnd }; |
| | | ::TrackMouseEvent(&tme); |
| | | |
| | | CRect rect; |
| | | GetClientRect(&rect); |
| | | int nCellWidth = rect.Width() / m_nCols; |
| | | int nCellHeight = rect.Height() / m_nRows; |
| | | |
| | | int nCol = point.x / nCellWidth; |
| | | int nRow = point.y / nCellHeight; |
| | | |
| | | if (nRow != m_ptHover.y || nCol != m_ptHover.x) { |
| | | m_ptHover = CPoint(nCol, nRow); |
| | | Invalidate(); |
| | | } |
| | | |
| | | CWnd::OnMouseMove(nFlags, point); |
| | | } |
| | | |
| | | void CJobSlotGrid::OnMouseLeave() |
| | | { |
| | | m_ptHover = CPoint(-1, -1); |
| | | Invalidate(); |
| | | |
| | | CWnd::OnMouseLeave(); |
| | | } |
| | | |
| | | void CJobSlotGrid::OnLButtonDown(UINT nFlags, CPoint point) |
| | | { |
| | | m_bLButtonDown = true; |
| | | Invalidate(); |
| | | |
| | | CWnd::OnLButtonDown(nFlags, point); |
| | | } |
| | | |
| | | void CJobSlotGrid::OnLButtonUp(UINT nFlags, CPoint point) |
| | | { |
| | | m_bLButtonDown = false; |
| | | Invalidate(); |
| | | |
| | | // 保持原有逻辑不变 |
| | | CRect rect; |
| | | GetClientRect(&rect); |
| | | int nCellWidth = rect.Width() / m_nCols; |
| | | int nCellHeight = rect.Height() / m_nRows; |
| | | |
| | | int nCol = point.x / nCellWidth; |
| | | int nRow = point.y / nCellHeight; |
| | | |
| | | if (IsSlotClickable(nRow, nCol)) { |
| | | if (m_fnSlotClickCallback) { |
| | | m_fnSlotClickCallback(nRow, nCol); |
| | | } |
| | | } |
| | | |
| | | CWnd::OnLButtonUp(nFlags, point); |
| | | } |
| | | |
| | | void CJobSlotGrid::OnPaint() { |
| | |
| | | for (int j = 0; j < m_nCols; ++j) { |
| | | CRect cellRect(j * nCellWidth, i * nCellHeight, (j + 1) * nCellWidth, (i + 1) * nCellHeight); |
| | | |
| | | // 背景 |
| | | CBrush* pBrush = m_vSlotStatus[i][j] ? &m_brushHasJob : &m_brushNoJob; |
| | | pDC->FillRect(&cellRect, pBrush); |
| | | // 判断状态:悬停 / 按下 |
| | | bool bIsHover = (m_ptHover.x == j && m_ptHover.y == i); |
| | | bool bIsClicking = bIsHover && m_bLButtonDown; |
| | | |
| | | // 选择颜色 |
| | | COLORREF fillColor; |
| | | if (bIsClicking) { |
| | | fillColor = RGB(0, 120, 215); // 鼠标按下色 |
| | | } |
| | | else if (bIsHover) { |
| | | fillColor = RGB(200, 230, 255); // 悬停高亮 |
| | | } |
| | | else { |
| | | fillColor = m_vSlotStatus[i][j] ? m_colorHasJob : m_colorNoJob; |
| | | } |
| | | |
| | | // 画背景 |
| | | CBrush brush(fillColor); |
| | | pDC->FillRect(&cellRect, &brush); |
| | | |
| | | // 边框 |
| | | pDC->DrawEdge(&cellRect, EDGE_SUNKEN, BF_RECT); |
| | | |
| | | // 文字(居中) |
| | | // 文本 |
| | | pDC->SetBkMode(TRANSPARENT); |
| | | pDC->SetTextColor(RGB(0, 0, 0)); |
| | | pDC->DrawText(m_vSlotText[i][j], &cellRect, DT_CENTER | DT_VCENTER | DT_SINGLELINE); |
| | |
| | | } |
| | | |
| | | pDC->SelectObject(pOldFont); |
| | | } |
| | | } |