📄 barcore.cpp
字号:
if (lpWndPos->flags & SWP_NOSIZE)
return;
// invalidate borders on the right
CRect rect;
GetWindowRect(&rect);
CSize sizePrev = rect.Size();
int cx = lpWndPos->cx;
int cy = lpWndPos->cy;
if (cx != sizePrev.cx && (m_dwStyle & CBRS_BORDER_RIGHT))
{
rect.SetRect(cx-afxData.cxBorder2, 0, cx, cy);
InvalidateRect(&rect);
rect.SetRect(sizePrev.cx-afxData.cxBorder2, 0, sizePrev.cx, cy);
InvalidateRect(&rect);
}
// invalidate borders on the bottom
if (cy != sizePrev.cy && (m_dwStyle & CBRS_BORDER_BOTTOM))
{
rect.SetRect(0, cy-afxData.cyBorder2, cx, cy);
InvalidateRect(&rect);
rect.SetRect(0, sizePrev.cy-afxData.cyBorder2, cx, sizePrev.cy);
InvalidateRect(&rect);
}
}
int CControlBar::OnCreate(LPCREATESTRUCT lpcs)
{
if (CWnd::OnCreate(lpcs) == -1)
return -1;
if (m_dwStyle & CBRS_TOOLTIPS)
EnableToolTips();
CFrameWnd *pFrameWnd = (CFrameWnd*)GetParent();
if (pFrameWnd->IsFrameWnd())
{
m_pDockSite = pFrameWnd;
m_pDockSite->AddControlBar(this);
}
return 0;
}
void CControlBar::OnDestroy()
{
_AFX_THREAD_STATE* pThreadState = AfxGetThreadState();
if (pThreadState->m_pLastStatus == this)
{
SetStatusText(-1);
ASSERT(pThreadState->m_pLastStatus == NULL);
}
if (m_pDockSite != NULL)
{
m_pDockSite->RemoveControlBar(this);
m_pDockSite = NULL;
}
CWnd::OnDestroy();
}
BOOL CControlBar::DestroyWindow()
{
if (m_hWnd != NULL && IsFloating())
return GetDockingFrame()->DestroyWindow();
else
return CWnd::DestroyWindow();
}
int CControlBar::OnMouseActivate(CWnd* pDesktopWnd, UINT nHitTest, UINT nMsg)
{
// call default when toolbar is not floating
if (!IsFloating())
return CWnd::OnMouseActivate(pDesktopWnd, nHitTest, nMsg);
// special behavior when floating
ActivateTopParent();
return MA_NOACTIVATE; // activation already done
}
void CControlBar::OnPaint()
{
// background is already filled in gray
CPaintDC dc(this);
// erase background now
if (IsVisible())
DoPaint(&dc); // delegate to paint helper
}
void CControlBar::EraseNonClient()
{
// get window DC that is clipped to the non-client area
CWindowDC dc(this);
CRect rectClient;
GetClientRect(rectClient);
CRect rectWindow;
GetWindowRect(rectWindow);
ScreenToClient(rectWindow);
rectClient.OffsetRect(-rectWindow.left, -rectWindow.top);
dc.ExcludeClipRect(rectClient);
// draw borders in non-client area
rectWindow.OffsetRect(-rectWindow.left, -rectWindow.top);
DrawBorders(&dc, rectWindow);
// erase parts not drawn
dc.IntersectClipRect(rectWindow);
SendMessage(WM_ERASEBKGND, (WPARAM)dc.m_hDC);
// draw gripper in non-client area
DrawGripper(&dc, rectWindow);
}
HBRUSH CControlBar::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
LRESULT lResult;
if (pWnd->SendChildNotifyLastMsg(&lResult))
return (HBRUSH)lResult; // eat it
// force black text on gray background all the time
if (!GrayCtlColor(pDC->m_hDC, pWnd->GetSafeHwnd(), nCtlColor,
afxData.hbrBtnFace, afxData.clrBtnText))
return (HBRUSH)Default();
return afxData.hbrBtnFace;
}
void CControlBar::OnLButtonDown(UINT nFlags, CPoint pt)
{
// only start dragging if clicked in "void" space
if (m_pDockBar != NULL && OnToolHitTest(pt, NULL) == -1)
{
// start the drag
ASSERT(m_pDockContext != NULL);
ClientToScreen(&pt);
m_pDockContext->StartDrag(pt);
}
else
{
CWnd::OnLButtonDown(nFlags, pt);
}
}
void CControlBar::OnLButtonDblClk(UINT nFlags, CPoint pt)
{
// only toggle docking if clicked in "void" space
if (m_pDockBar != NULL && OnToolHitTest(pt, NULL) == -1)
{
// start the drag
ASSERT(m_pDockContext != NULL);
m_pDockContext->ToggleDocking();
}
else
{
CWnd::OnLButtonDblClk(nFlags, pt);
}
}
LRESULT CControlBar::OnIdleUpdateCmdUI(WPARAM wParam, LPARAM)
{
// handle delay hide/show
BOOL bVis = GetStyle() & WS_VISIBLE;
UINT swpFlags = 0;
if ((m_nStateFlags & delayHide) && bVis)
swpFlags = SWP_HIDEWINDOW;
else if ((m_nStateFlags & delayShow) && !bVis)
swpFlags = SWP_SHOWWINDOW;
m_nStateFlags &= ~(delayShow|delayHide);
if (swpFlags != 0)
{
SetWindowPos(NULL, 0, 0, 0, 0, swpFlags|
SWP_NOMOVE|SWP_NOSIZE|SWP_NOZORDER|SWP_NOACTIVATE);
}
// the style must be visible and if it is docked
// the dockbar style must also be visible
if ((GetStyle() & WS_VISIBLE) &&
(m_pDockBar == NULL || (m_pDockBar->GetStyle() & WS_VISIBLE)))
{
CFrameWnd* pTarget = (CFrameWnd*)GetOwner();
if (pTarget == NULL || !pTarget->IsFrameWnd())
pTarget = GetParentFrame();
if (pTarget != NULL)
OnUpdateCmdUI(pTarget, (BOOL)wParam);
}
return 0L;
}
void CControlBar::OnInitialUpdate()
{
// update the indicators before becoming visible
OnIdleUpdateCmdUI(TRUE, 0L);
}
DWORD CControlBar::RecalcDelayShow(AFX_SIZEPARENTPARAMS* lpLayout)
{
ASSERT(lpLayout != NULL);
// resize and reposition this control bar based on styles
DWORD dwStyle = (m_dwStyle & (CBRS_ALIGN_ANY|CBRS_BORDER_ANY)) |
(GetStyle() & WS_VISIBLE);
// handle delay hide/show
if (m_nStateFlags & (delayHide|delayShow))
{
UINT swpFlags = 0;
if (m_nStateFlags & delayHide)
{
ASSERT((m_nStateFlags & delayShow) == 0);
if (dwStyle & WS_VISIBLE)
swpFlags = SWP_HIDEWINDOW;
}
else
{
ASSERT(m_nStateFlags & delayShow);
if ((dwStyle & WS_VISIBLE) == 0)
swpFlags = SWP_SHOWWINDOW;
}
if (swpFlags != 0)
{
// make the window seem visible/hidden
dwStyle ^= WS_VISIBLE;
if (lpLayout->hDWP != NULL)
{
// clear delay flags
m_nStateFlags &= ~(delayShow|delayHide);
// hide/show the window if actually doing layout
lpLayout->hDWP = ::DeferWindowPos(lpLayout->hDWP, m_hWnd, NULL,
0, 0, 0, 0, swpFlags|
SWP_NOMOVE|SWP_NOSIZE|SWP_NOZORDER|SWP_NOACTIVATE);
}
}
else
{
// clear delay flags -- window is already in correct state
m_nStateFlags &= ~(delayShow|delayHide);
}
}
return dwStyle; // return new style
}
LRESULT CControlBar::OnSizeParent(WPARAM, LPARAM lParam)
{
AFX_SIZEPARENTPARAMS* lpLayout = (AFX_SIZEPARENTPARAMS*)lParam;
DWORD dwStyle = RecalcDelayShow(lpLayout);
if ((dwStyle & WS_VISIBLE) && (dwStyle & CBRS_ALIGN_ANY) != 0)
{
// align the control bar
CRect rect;
rect.CopyRect(&lpLayout->rect);
CSize sizeAvail = rect.Size(); // maximum size available
// get maximum requested size
DWORD dwMode = lpLayout->bStretch ? LM_STRETCH : 0;
if ((m_dwStyle & CBRS_SIZE_DYNAMIC) && m_dwStyle & CBRS_FLOATING)
dwMode |= LM_HORZ | LM_MRUWIDTH;
else if (dwStyle & CBRS_ORIENT_HORZ)
dwMode |= LM_HORZ | LM_HORZDOCK;
else
dwMode |= LM_VERTDOCK;
CSize size = CalcDynamicLayout(-1, dwMode);
size.cx = min(size.cx, sizeAvail.cx);
size.cy = min(size.cy, sizeAvail.cy);
if (dwStyle & CBRS_ORIENT_HORZ)
{
lpLayout->sizeTotal.cy += size.cy;
lpLayout->sizeTotal.cx = max(lpLayout->sizeTotal.cx, size.cx);
if (dwStyle & CBRS_ALIGN_TOP)
lpLayout->rect.top += size.cy;
else if (dwStyle & CBRS_ALIGN_BOTTOM)
{
rect.top = rect.bottom - size.cy;
lpLayout->rect.bottom -= size.cy;
}
}
else if (dwStyle & CBRS_ORIENT_VERT)
{
lpLayout->sizeTotal.cx += size.cx;
lpLayout->sizeTotal.cy = max(lpLayout->sizeTotal.cy, size.cy);
if (dwStyle & CBRS_ALIGN_LEFT)
lpLayout->rect.left += size.cx;
else if (dwStyle & CBRS_ALIGN_RIGHT)
{
rect.left = rect.right - size.cx;
lpLayout->rect.right -= size.cx;
}
}
else
{
ASSERT(FALSE); // can never happen
}
rect.right = rect.left + size.cx;
rect.bottom = rect.top + size.cy;
// only resize the window if doing layout and not just rect query
if (lpLayout->hDWP != NULL)
AfxRepositionWindow(lpLayout, m_hWnd, &rect);
}
return 0;
}
void CControlBar::DelayShow(BOOL bShow)
{
m_nStateFlags &= ~(delayHide|delayShow);
if (bShow && (GetStyle() & WS_VISIBLE) == 0)
m_nStateFlags |= delayShow;
else if (!bShow && (GetStyle() & WS_VISIBLE) != 0)
m_nStateFlags |= delayHide;
}
BOOL CControlBar::IsVisible() const
{
if (m_nStateFlags & delayHide)
return FALSE;
if ((m_nStateFlags & delayShow) || ((GetStyle() & WS_VISIBLE) != 0))
return TRUE;
return FALSE;
}
void CControlBar::DoPaint(CDC* pDC)
{
ASSERT_VALID(this);
ASSERT_VALID(pDC);
// paint inside client area
CRect rect;
GetClientRect(rect);
DrawBorders(pDC, rect);
DrawGripper(pDC, rect);
}
void CControlBar::DrawBorders(CDC* pDC, CRect& rect)
{
ASSERT_VALID(this);
ASSERT_VALID(pDC);
DWORD dwStyle = m_dwStyle;
if (!(dwStyle & CBRS_BORDER_ANY))
return;
// prepare for dark lines
ASSERT(rect.top == 0 && rect.left == 0);
CRect rect1, rect2;
rect1 = rect;
rect2 = rect;
COLORREF clr = afxData.bWin4 ? afxData.clrBtnShadow : afxData.clrWindowFrame;
// draw dark line one pixel back/up
if (dwStyle & CBRS_BORDER_3D)
{
rect1.right -= CX_BORDER;
rect1.bottom -= CY_BORDER;
}
if (dwStyle & CBRS_BORDER_TOP)
rect2.top += afxData.cyBorder2;
if (dwStyle & CBRS_BORDER_BOTTOM)
rect2.bottom -= afxData.cyBorder2;
// draw left and top
if (dwStyle & CBRS_BORDER_LEFT)
pDC->FillSolidRect(0, rect2.top, CX_BORDER, rect2.Height(), clr);
if (dwStyle & CBRS_BORDER_TOP)
pDC->FillSolidRect(0, 0, rect.right, CY_BORDER, clr);
// draw right and bottom
if (dwStyle & CBRS_BORDER_RIGHT)
pDC->FillSolidRect(rect1.right, rect2.top, -CX_BORDER, rect2.Height(), clr);
if (dwStyle & CBRS_BORDER_BOTTOM)
pDC->FillSolidRect(0, rect1.bottom, rect.right, -CY_BORDER, clr);
if (dwStyle & CBRS_BORDER_3D)
{
// prepare for hilite lines
clr = afxData.clrBtnHilite;
// draw left and top
if (dwStyle & CBRS_BORDER_LEFT)
pDC->FillSolidRect(1, rect2.top, CX_BORDER, rect2.Height(), clr);
if (dwStyle & CBRS_BORDER_TOP)
pDC->FillSolidRect(0, 1, rect.right, CY_BORDER, clr);
// draw right and bottom
if (dwStyle & CBRS_BORDER_RIGHT)
pDC->FillSolidRect(rect.right, rect2.top, -CX_BORDER, rect2.Height(), clr);
if (dwStyle & CBRS_BORDER_BOTTOM)
pDC->FillSolidRect(0, rect.bottom, rect.right, -CY_BORDER, clr);
}
if (dwStyle & CBRS_BORDER_LEFT)
rect.left += afxData.cxBorder2;
if (dwStyle & CBRS_BORDER_TOP)
rect.top += afxData.cyBorder2;
if (dwStyle & CBRS_BORDER_RIGHT)
rect.right -= afxData.cxBorder2;
if (dwStyle & CBRS_BORDER_BOTTOM)
rect.bottom -= afxData.cyBorder2;
}
#define CX_GRIPPER 3
#define CY_GRIPPER 3
#define CX_BORDER_GRIPPER 2
#define CY_BORDER_GRIPPER 2
void CControlBar::DrawGripper(CDC* pDC, const CRect& rect)
{
// only draw the gripper if not floating and gripper is specified
if ((m_dwStyle & (CBRS_GRIPPER|CBRS_FLOATING)) == CBRS_GRIPPER)
{
// draw the gripper in the border
if (m_dwStyle & CBRS_ORIENT_HORZ)
{
pDC->Draw3dRect(rect.left+CX_BORDER_GRIPPER,
rect.top+m_cyTopBorder,
CX_GRIPPER, rect.Height()-m_cyTopBorder-m_cyBottomBorder,
afxData.clrBtnHilite, afxData.clrBtnShadow);
}
else
{
pDC->Draw3dRect(rect.left+m_cyTopBorder,
rect.top+CY_BORDER_GRIPPER,
rect.Width()-m_cyTopBorder-m_cyBottomBorder, CY_GRIPPER,
afxData.clrBtnHilite, afxData.clrBtnShadow);
}
}
}
// input CRect should be client rectangle size
void CControlBar::CalcInsideRect(CRect& rect, BOOL bHorz) const
{
ASSERT_VALID(this);
DWORD dwStyle = m_dwStyle;
if (dwStyle & CBRS_BORDER_LEFT)
rect.left += afxData.cxBorder2;
if (dwStyle & CBRS_BORDER_TOP)
rect.top += afxData.cyBorder2;
if (dwStyle & CBRS_BORDER_RIGHT)
rect.right -= afxData.cxBorder2;
if (dwStyle & CBRS_BORDER_BOTTOM)
rect.bottom -= afxData.cyBorder2;
// inset the top and bottom.
if (bHorz)
{
rect.left += m_cxLeftBorder;
rect.top += m_cyTopBorder;
rect.right -= m_cxRightBorder;
rect.bottom -= m_cyBottomBorder;
if ((m_dwStyle & (CBRS_GRIPPER|CBRS_FLOATING)) == CBRS_GRIPPER)
rect.left += CX_BORDER_GRIPPER+CX_GRIPPER+CX_BORDER_GRIPPER;
}
else
{
rect.left += m_cyTopBorder;
rect.top += m_cxLeftBorder;
rect.right -= m_cyBottomBorder;
rect.bottom -= m_cxRightBorder;
if ((m_dwStyle & (CBRS_GRIPPER|CBRS_FLOATING)) == CBRS_GRIPPER)
rect.top += CY_BORDER_GRIPPER+CY_GRIPPER+CY_BORDER_GRIPPER;
}
}
/////////////////////////////////////////////////////////////////////////////
// CControlBar diagnostics
#ifdef _DEBUG
void CControlBar::AssertValid() const
{
CWnd::AssertValid();
ASSERT(m_nCount == 0 || m_pData != NULL);
ASSERT((m_dwStyle & CBRS_ALL) == m_dwStyle);
}
void CControlBar::Dump(CDumpContext& dc) const
{
CWnd::Dump(dc);
dc << "\nm_cxLeftBorder = " << m_cxLeftBorder;
dc << "\nm_cxRightBorder = " << m_cxRightBorder;
dc << "\nm_cyTopBorder = " << m_cyTopBorder;
dc << "\nm_cyBottomBorder = " << m_cyBottomBorder;
dc << "\nm_cxDefaultGap = " << m_cxDefaultGap;
dc << "\nm_nCount = " << m_nCount;
dc << "\nm_bAutoDelete = " << m_bAutoDelete;
dc << "\n";
}
#endif
/////////////////////////////////////////////////////////////////////////////
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -