📄 dockbarex.cpp
字号:
rcWnd.DeflateRect(m_cxEdge + 2, 2, 2, 6);
m_rectBorder.right = m_rectBorder.left + m_cxEdge;
break;
}
default:
{
m_rectBorder.SetRectEmpty();
break;
}
}
lpncsp->rgrc[0] = rcWnd;
SetBarStyle(dwBorderStyle);
}
void CDockBarEx::OnPaint()
{
CPaintDC dc(this); // device context for painting
DrawGripper(&dc);
}
void CDockBarEx::OnNcPaint()
{
// get window DC that is clipped to the non-client area
CWindowDC dc(this);
CRect rectWindow;
GetWindowRect(rectWindow);
ScreenToClient(rectWindow);
CRect rectClient;
GetClientRect(rectClient);
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);
// erase NC background the hard way
dc.FillRect(rectWindow, &CBrush(::GetSysColor(COLOR_BTNFACE)));
// paint the mobile edge
dc.Draw3dRect(m_rectBorder, ::GetSysColor(COLOR_BTNHIGHLIGHT),
::GetSysColor(COLOR_BTNSHADOW));
ReleaseDC(&dc);
}
UINT CDockBarEx::OnNcHitTest(CPoint point)
{
if (IsFloating())
return CControlBar::OnNcHitTest(point);
CRect rc;
GetWindowRect(rc);
point.Offset(-rc.left, -rc.top);
if (m_rectBorder.PtInRect(point))
return HTSIZE;
else
return HTCLIENT;
}
BOOL CDockBarEx::IsFloating()
{
return (!IsHorzDocked() && !IsVertDocked());
}
void CDockBarEx::StartTracking()
{
SetCapture();
// make sure no updates are pending
RedrawWindow(NULL, NULL, RDW_ALLCHILDREN | RDW_UPDATENOW);
m_pDockSite->LockWindowUpdate();
m_ptOld = m_rectBorder.CenterPoint();
m_bTracking = TRUE;
m_rectTracker = m_rectBorder;
if (!IsHorzDocked()) m_rectTracker.bottom -= 4;
OnInvertTracker(m_rectTracker);
}
void CDockBarEx::StopTracking(BOOL bAccept)
{
OnInvertTracker(m_rectTracker);
m_pDockSite->UnlockWindowUpdate();
m_bTracking = FALSE;
ReleaseCapture();
if (!bAccept)
return;
int maxsize, minsize, newsize;
CRect rcc;
m_pDockSite->GetClientRect(rcc);
newsize = IsHorzDocked() ? m_sizeHorz.cy : m_sizeVert.cx;
maxsize = newsize + (IsHorzDocked() ? rcc.Height() : rcc.Width());
minsize = IsHorzDocked() ? m_sizeMin.cy : m_sizeMin.cx;
CPoint point = m_rectTracker.CenterPoint();
switch (m_nDockBarID)
{
case AFX_IDW_DOCKBAR_TOP:
{
newsize += point.y - m_ptOld.y;
break;
}
case AFX_IDW_DOCKBAR_BOTTOM:
{
newsize += -point.y + m_ptOld.y;
break;
}
case AFX_IDW_DOCKBAR_LEFT:
{
newsize += point.x - m_ptOld.x;
break;
}
case AFX_IDW_DOCKBAR_RIGHT:
{
newsize += -point.x + m_ptOld.x;
break;
}
}
newsize = max(minsize, min(maxsize, newsize));
if (IsHorzDocked())
m_sizeHorz.cy = newsize;
else
m_sizeVert.cx = newsize;
m_pDockSite->RecalcLayout();
}
void CDockBarEx::OnInvertTracker(const CRect &rect)
{
ASSERT_VALID(this);
ASSERT(!rect.IsRectEmpty());
ASSERT(m_bTracking);
CRect rct = rect, rcc, rcf;
GetWindowRect(rcc);
m_pDockSite->GetWindowRect(rcf);
rct.OffsetRect(rcc.left - rcf.left, rcc.top - rcf.top);
rct.DeflateRect(1, 1);
CDC *pDC = m_pDockSite->GetDCEx(NULL,
DCX_WINDOW|DCX_CACHE|DCX_LOCKWINDOWUPDATE);
CBrush* pBrush = CDC::GetHalftoneBrush();
HBRUSH hOldBrush = NULL;
if (pBrush != NULL)
hOldBrush = (HBRUSH)SelectObject(pDC->m_hDC, pBrush->m_hObject);
pDC->PatBlt(rct.left, rct.top, rct.Width(), rct.Height(), PATINVERT);
if (hOldBrush != NULL)
SelectObject(pDC->m_hDC, hOldBrush);
m_pDockSite->ReleaseDC(pDC);
}
void CDockBarEx::OnCaptureChanged(CWnd *pWnd)
{
if (m_bTracking && pWnd != this)
StopTracking(FALSE); // cancel tracking
CControlBar::OnCaptureChanged(pWnd);
}
void CDockBarEx::OnNcLButtonDown(UINT nHitTest, CPoint point)
{
if (m_bTracking)
return;
if ((nHitTest == HTSIZE) && !IsFloating())
StartTracking();
else
CControlBar::OnNcLButtonDown(nHitTest, point);
}
void CDockBarEx::OnMouseMove(UINT nFlags, CPoint point)
{
if (IsFloating() || !m_bTracking) {
CControlBar::OnMouseMove(nFlags, point);
return;
}
CPoint cpt = m_rectTracker.CenterPoint();
ClientToWnd(point);
if (IsHorzDocked()) {
if (cpt.y != point.y) {
OnInvertTracker(m_rectTracker);
m_rectTracker.OffsetRect(0, point.y - cpt.y);
OnInvertTracker(m_rectTracker);
}
}
else {
if (cpt.x != point.x) {
OnInvertTracker(m_rectTracker);
m_rectTracker.OffsetRect(point.x - cpt.x, 0);
OnInvertTracker(m_rectTracker);
}
}
}
void CDockBarEx::OnLButtonDown(UINT nFlags, CPoint point)
{
if (m_pDockBar != NULL)
{
// start the drag
ASSERT(m_pDockContext != NULL);
ClientToScreen(&point);
m_pDockContext->StartDrag(point);
}
else
CControlBar::OnLButtonDown(nFlags, point);
}
void CDockBarEx::OnLButtonUp(UINT nFlags, CPoint point)
{
if (!m_bTracking)
CControlBar::OnLButtonUp(nFlags, point);
else
{
ClientToWnd(point);
StopTracking(TRUE);
}
}
void CDockBarEx::OnLButtonDblClk(UINT nFlags, CPoint point)
{
if (m_pDockBar != NULL)
{
// toggle docking
ASSERT(m_pDockContext != NULL);
m_pDockContext->ToggleDocking();
m_pDockSite->RecalcLayout();
}
else
CControlBar::OnLButtonDblClk(nFlags, point);
}
CPoint& CDockBarEx::ClientToWnd(CPoint &point)
{
point.Offset(2, 2);
if (m_nDockBarID == AFX_IDW_DOCKBAR_BOTTOM)
point.y += m_cxEdge;
else if (m_nDockBarID == AFX_IDW_DOCKBAR_RIGHT)
point.x += m_cxEdge;
return point;
}
CRect CDockBarEx::GetButtonRect()
{
CRect pRect;
GetClientRect(pRect);
pRect.OffsetRect(-pRect.left,-pRect.top);
if(IsHorzDocked()) {
pRect.top += 3;
pRect.bottom = pRect.top+12;
pRect.left += 2;
pRect.right = pRect.left+12;
}
else
{
pRect.right -= 19;
pRect.left = pRect.right-12;
pRect.top += 3;
pRect.bottom = pRect.top+12;
}
return pRect;
}
CRect CDockBarEx::GetGripperRect()
{
CRect pRect;
GetClientRect(pRect);
pRect.OffsetRect(-pRect.left,-pRect.top);
if(IsHorzDocked()) {
pRect.DeflateRect(3,3);
pRect.left += 1;
pRect.right = pRect.left+3;
pRect.bottom -= 1;
pRect.top += m_bButtons?30:1;
}
else {
pRect.DeflateRect(4,4);
pRect.top += 2;
pRect.bottom = pRect.top+3;
pRect.right -= m_bButtons?30:2;
}
return pRect;
}
void CDockBarEx::DrawGripper(CDC* pDC)
{
if (IsFloating())
return;
if (m_bGripper)
{
// draw the gripper.
CRect pRect(GetGripperRect());
pDC->Draw3dRect( pRect, ::GetSysColor(COLOR_BTNHIGHLIGHT),
::GetSysColor(COLOR_BTNSHADOW) );
if(IsHorzDocked())
pRect.OffsetRect(4,0);
else
pRect.OffsetRect(0,4);
pDC->Draw3dRect( pRect, ::GetSysColor(COLOR_BTNHIGHLIGHT),
::GetSysColor(COLOR_BTNSHADOW) );
}
m_pDockSite->RecalcLayout();
}
int CDockBarEx::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CControlBar::OnCreate(lpCreateStruct) == -1)
return -1;
if (m_bButtons)
{
ASSERT(m_ImageList);
if(!m_btnClose.Create(NULL, WS_CHILD | WS_VISIBLE | WS_TABSTOP |
BS_NOTIFY | BS_OWNERDRAW | BS_ICON,
CRect(0,0,0,0), this, IDC_BUTTON_HIDE ))
{
TRACE0("Unable to create CDockBarEx close button\n");
return -1;
}
if(!m_btnMinim.Create(NULL, WS_CHILD | WS_VISIBLE | WS_TABSTOP |
BS_NOTIFY | BS_OWNERDRAW | BS_ICON,
CRect(0,0,0,0), this, IDC_BUTTON_MINI ))
{
TRACE0("Unable to create CDockBarEx close button\n");
return -1;
}
m_btnClose.DisableFlatLook();
m_btnMinim.DisableFlatLook();
m_btnClose.SetIcon(m_ImageList.ExtractIcon(0),CSize(13,13));
// Create the ToolTip control.
m_ToolTip.Create(this);
m_ToolTip.Activate(TRUE);
m_ToolTip.AddTool (&m_btnClose, _T("Hide Docked Window"));
m_ToolTip.AddTool (&m_btnMinim, _T("Minimize Docked Window"));
}
return 0;
}
void CDockBarEx::OnButtonClose()
{
m_pDockSite->ShowControlBar(this, FALSE, FALSE);
m_pDockSite->RecalcLayout();
}
void CDockBarEx::OnUpdateButtonClose(CCmdUI* pCmdUI)
{
pCmdUI->Enable(true);
}
void CDockBarEx::OnButtonMinimize()
{
m_pDockContext->ToggleDocking();
m_pDockSite->RecalcLayout();
}
void CDockBarEx::OnUpdateButtonMinimize(CCmdUI* pCmdUI)
{
pCmdUI->Enable(true);
}
BOOL CDockBarEx::PreTranslateMessage(MSG* pMsg)
{
if( m_bButtons )
m_ToolTip.RelayEvent(pMsg);
return CControlBar::PreTranslateMessage(pMsg);
}
void CDockBarEx::SetMenuID(UINT nID)
{
m_menuID = nID;
}
UINT CDockBarEx::GetMenuID()
{
return m_menuID;
}
void CDockBarEx::OnContextMenu(CWnd* pWnd, CPoint point)
{
// if no menu, just return.
if (m_menuID == 0 ) {
TRACE0("No control bar menu defined.\n");
return;
}
if (point.x == -1 && point.y == -1)
{
//keystroke invocation
CRect rect;
GetClientRect(rect);
ClientToScreen(rect);
point = rect.TopLeft();
point.Offset(5, 5);
}
CMenu menu;
VERIFY(menu.LoadMenu(m_menuID));
CMenu* pPopup = menu.GetSubMenu(0);
ASSERT(pPopup != NULL);
CWnd* pWndPopupOwner = this;
while (pWndPopupOwner->GetStyle() & WS_CHILD)
pWndPopupOwner = pWndPopupOwner->GetParent();
pPopup->TrackPopupMenu(TPM_LEFTALIGN | TPM_RIGHTBUTTON, point.x, point.y,
pWndPopupOwner);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -