📄 bcgtoolbar.cpp
字号:
nHit = (pButton->m_nID == (UINT) -1) ? 0 : pButton->m_nID;
}
return nHit;
}
//*************************************************************************************
int CBCGToolBar::HitTest(CPoint point) // in window relative coords
{
int iButton = 0;
for (POSITION pos = m_Buttons.GetHeadPosition (); pos != NULL; iButton ++)
{
CBCGToolbarButton* pButton = (CBCGToolbarButton*) m_Buttons.GetNext (pos);
ASSERT (pButton != NULL);
if (pButton->Rect ().PtInRect (point) && !pButton->IsHidden ())
{
return (pButton->m_nStyle & TBBS_SEPARATOR) ? -1 : iButton;
}
}
return -1; // nothing hit
}
/////////////////////////////////////////////////////////////////////////////
// CBCGToolBar message handlers
BEGIN_MESSAGE_MAP(CBCGToolBar, CControlBar)
ON_WM_CONTEXTMENU()
//{{AFX_MSG_MAP(CBCGToolBar)
ON_WM_MOUSEMOVE()
ON_WM_CANCELMODE()
ON_WM_SYSCOLORCHANGE()
ON_WM_CREATE()
ON_WM_DESTROY()
ON_WM_LBUTTONDOWN()
ON_COMMAND(ID_BCGBARRES_TOOLBAR_APPEARANCE, OnToolbarAppearance)
ON_COMMAND(ID_BCGBARRES_TOOLBAR_DELETE, OnToolbarDelete)
ON_COMMAND(ID_BCGBARRES_TOOLBAR_IMAGE, OnToolbarImage)
ON_COMMAND(ID_BCGBARRES_TOOLBAR_IMAGE_AND_TEXT, OnToolbarImageAndText)
ON_COMMAND(ID_BCGBARRES_TOOLBAR_START_GROUP, OnToolbarStartGroup)
ON_COMMAND(ID_BCGBARRES_TOOLBAR_TEXT, OnToolbarText)
ON_WM_LBUTTONUP()
ON_WM_WINDOWPOSCHANGED()
ON_WM_CTLCOLOR()
ON_WM_SETCURSOR()
ON_WM_SIZE()
ON_WM_LBUTTONDBLCLK()
ON_COMMAND(ID_BCGBARRES_TOOLBAR_RESET, OnBcgbarresToolbarReset)
ON_WM_NCCALCSIZE()
ON_WM_NCPAINT()
ON_WM_NCHITTEST()
ON_COMMAND(ID_BCGBARRES_COPY_IMAGE, OnBcgbarresCopyImage)
ON_WM_SETFOCUS()
ON_COMMAND(ID_BCGBARRES_TOOLBAR_NEW_MENU, OnBcgbarresToolbarNewMenu)
ON_WM_WINDOWPOSCHANGING()
ON_WM_ERASEBKGND()
//}}AFX_MSG_MAP
ON_MESSAGE(WM_MOUSELEAVE, OnMouseLeave)
ON_MESSAGE(WM_HELPHITTEST, OnHelpHitTest)
END_MESSAGE_MAP()
void CBCGToolBar::OnLButtonDown(UINT nFlags, CPoint point)
{
int iButton = HitTest(point);
if (iButton < 0) // nothing hit
{
m_iButtonCapture = -1;
if (IsCustomizeMode () && !m_bLocked)
{
int iSelected = m_iSelected;
m_iSelected = -1;
if (iSelected != -1)
{
InvalidateButton (iSelected);
UpdateWindow ();
}
OnChangeHot (-1);
}
CControlBar::OnLButtonDown(nFlags, point);
return;
}
CBCGToolbarButton* pButton = GetButton(iButton);
if (pButton == NULL)
{
ASSERT (FALSE);
return;
}
ASSERT(!(pButton->m_nStyle & TBBS_SEPARATOR));
//-----------------------------------------------------------------
// Check for "Alt-customizible mode" (when ALT key is holded down):
//-----------------------------------------------------------------
m_bAltCustomizeMode = FALSE;
if (m_bAltCustomization &&
!m_bCustomizeMode &&
GetAsyncKeyState (VK_MENU) & 0x8000) // ALT is pressed
{
m_bAltCustomizeMode = TRUE;
}
if ((!IsCustomizeMode () && !m_bAltCustomizeMode) || m_bLocked)
{
m_iButtonCapture = iButton;
// update the button before checking for disabled status
UpdateButton(m_iButtonCapture);
if (pButton->m_nStyle & TBBS_DISABLED)
{
m_iButtonCapture = -1;
return; // don't press it
}
pButton->m_nStyle |= TBBS_PRESSED;
InvalidateButton (iButton);
UpdateWindow(); // immediate feedback
ShowCommandMessageString (pButton->m_nID);
if (pButton->OnClick (this, FALSE /* No delay*/))
{
pButton->m_nStyle &= ~TBBS_PRESSED;
m_iButtonCapture = -1;
m_iHighlighted = -1;
OnChangeHot (m_iHighlighted);
InvalidateButton (iButton);
UpdateWindow(); // immediate feedback
}
else
{
m_pWndLastCapture = SetCapture ();
}
}
else
{
int iSelected = m_iSelected;
m_iSelected = iButton;
CRect rect;
GetItemRect (iButton, &rect);
if (iSelected != -1)
{
InvalidateButton (iSelected);
}
m_pDragButton = GetButton (m_iSelected);
ASSERT (m_pDragButton != NULL);
m_bIsDragCopy = (nFlags & MK_CONTROL);
if (!m_pDragButton->IsEditable ())
{
m_iSelected = -1;
m_pDragButton = NULL;
if (iSelected != -1)
{
InvalidateButton (iSelected);
}
return;
}
InvalidateButton (iButton);
UpdateWindow(); // immediate feedback
if (m_pDragButton->CanBeStretched () &&
abs (point.x - rect.right) <= STRETCH_DELTA &&
!m_bAltCustomizeMode)
{
m_bStretchButton = TRUE;
m_rectTrack = m_pDragButton->Rect ();
if (m_pDragButton->GetHwnd () != NULL)
{
m_rectTrack.InflateRect (2, 2);
}
m_pWndLastCapture = SetCapture ();
::SetCursor (globalData.m_hcurStretch);
}
else if (m_pDragButton->CanBeStored () &&
m_pDragButton->OnBeforeDrag ())
{
COleDataSource srcItem;
m_pDragButton->PrepareDrag (srcItem);
ShowCommandMessageString (pButton->m_nID);
m_DropSource.m_bDragStarted = FALSE;
m_ptStartDrag = point;
HWND hwndSaved = m_hWnd;
if (m_bAltCustomizeMode)
{
m_bCustomizeMode = TRUE;
}
DROPEFFECT dropEffect = srcItem.DoDragDrop
(DROPEFFECT_COPY|DROPEFFECT_MOVE, &rect, &m_DropSource);
if (!::IsWindow (hwndSaved))
{
if (m_bAltCustomizeMode)
{
m_bCustomizeMode = FALSE;
m_bAltCustomizeMode = FALSE;
}
return;
}
CPoint ptDrop;
::GetCursorPos (&ptDrop);
ScreenToClient (&ptDrop);
if (m_DropSource.m_bDragStarted &&
!rect.PtInRect (ptDrop))
{
if (dropEffect != DROPEFFECT_COPY &&
m_pDragButton != NULL &&
!m_DropSource.m_bEscapePressed)
{
//---------------------
// Remove source button:
//---------------------
RemoveButton (ButtonToIndex (m_pDragButton));
AdjustLayout ();
Invalidate ();
}
else if (m_pDragButton != NULL)
{
InvalidateRect (m_pDragButton->Rect ());
}
}
else
{
m_iHighlighted = iButton;
OnChangeHot (m_iHighlighted);
}
m_pDragButton = NULL;
m_ptStartDrag = CPoint (-1, -1);
}
else
{
m_pDragButton = NULL;
}
}
if (m_bAltCustomizeMode)
{
m_bAltCustomizeMode = FALSE;
SetCustomizeMode (FALSE);
Invalidate ();
UpdateWindow();
}
}
//**************************************************************************************
void CBCGToolBar::OnMouseMove(UINT /*nFlags*/, CPoint point)
{
if (IsCustomizeMode () && !m_bLocked)
{
if (m_bStretchButton)
{
ASSERT_VALID (m_pDragButton);
if (point.x - m_pDragButton->Rect ().left >= BUTTON_MIN_WIDTH)
{
CClientDC dc (this);
CRect rectTrackOld = m_rectTrack;
m_rectTrack.right = point.x;
dc.DrawDragRect (&m_rectTrack, CSize (2, 2), &rectTrackOld, CSize (2, 2));
}
::SetCursor (globalData.m_hcurStretch);
}
return;
}
if (m_ptLastMouse != CPoint (-1, -1) &&
abs (m_ptLastMouse.x - point.x) < 1 &&
abs (m_ptLastMouse.y - point.y) < 1)
{
m_ptLastMouse = point;
return;
}
m_ptLastMouse = point;
int iPrevHighlighted = m_iHighlighted;
m_iHighlighted = HitTest (point);
if (m_iHighlighted == -1 && GetFocus () == this)
{
m_iHighlighted = iPrevHighlighted;
return;
}
CBCGToolbarButton* pButton = m_iHighlighted == -1 ?
NULL : GetButton (m_iHighlighted);
if (pButton != NULL &&
(pButton->m_nStyle & TBBS_SEPARATOR ||
(pButton->m_nStyle & TBBS_DISABLED/* && !AllowSelectDisabled ()*/)))
{
m_iHighlighted = -1;
}
if (!m_bTracked)
{
m_bTracked = TRUE;
TRACKMOUSEEVENT trackmouseevent;
trackmouseevent.cbSize = sizeof(trackmouseevent);
trackmouseevent.dwFlags = TME_LEAVE;
trackmouseevent.hwndTrack = GetSafeHwnd();
trackmouseevent.dwHoverTime = HOVER_DEFAULT;
::BCGTrackMouse (&trackmouseevent);
}
if (iPrevHighlighted != m_iHighlighted)
{
BOOL bNeedUpdate = FALSE;
if (m_iButtonCapture != -1)
{
CBCGToolbarButton* pTBBCapt = GetButton (m_iButtonCapture);
ASSERT (pTBBCapt != NULL);
ASSERT (!(pTBBCapt->m_nStyle & TBBS_SEPARATOR));
UINT nNewStyle = (pTBBCapt->m_nStyle & ~TBBS_PRESSED);
if (m_iHighlighted == m_iButtonCapture)
{
nNewStyle |= TBBS_PRESSED;
}
if (nNewStyle != pTBBCapt->m_nStyle)
{
SetButtonStyle (m_iButtonCapture, nNewStyle);
bNeedUpdate = TRUE;
}
}
if ((m_bMenuMode || m_iButtonCapture == -1 ||
iPrevHighlighted == m_iButtonCapture) &&
iPrevHighlighted != -1)
{
InvalidateButton (iPrevHighlighted);
bNeedUpdate = TRUE;
}
if ((m_bMenuMode || m_iButtonCapture == -1 ||
m_iHighlighted == m_iButtonCapture) &&
m_iHighlighted != -1)
{
InvalidateButton (m_iHighlighted);
bNeedUpdate = TRUE;
}
if (bNeedUpdate)
{
UpdateWindow ();
}
if (m_iHighlighted != -1 &&
(m_bMenuMode || m_iHighlighted == m_iButtonCapture || m_iButtonCapture == -1))
{
ASSERT (pButton != NULL);
ShowCommandMessageString (pButton->m_nID);
}
else if ((m_iButtonCapture == -1 || (m_bMenuMode && m_iHighlighted == -1))
&& m_hookMouseHelp == NULL)
{
GetOwner()->SendMessage (WM_SETMESSAGESTRING, AFX_IDS_IDLEMESSAGE);
}
OnChangeHot (m_iHighlighted);
}
}
//*************************************************************************************
void CBCGToolBar::OnLButtonUp(UINT nFlags, CPoint point)
{
if (IsCustomizeMode () && !m_bLocked)
{
if (m_bStretchButton)
{
ASSERT_VALID (m_pDragButton);
CRect rect = m_pDragButton->Rect ();
rect.right = point.x;
if (rect.Width () >= BUTTON_MIN_WIDTH &&
abs (m_pDragButton->Rect ().right - point.x) > STRETCH_DELTA)
{
m_pDragButton->OnSize (rect.Width ());
AdjustLayout ();
}
m_rectTrack.SetRectEmpty ();
m_pDragButton = NULL;
m_bStretchButton = FALSE;
Invalidate ();
::ReleaseCapture ();
if (m_pWndLastCapture != NULL)
{
m_pWndLastCapture->SetCapture ();
m_pWndLastCapture = NULL;
}
}
return;
}
if (m_iButtonCapture == -1)
{
CControlBar::OnLButtonUp(nFlags, point);
m_ptLastMouse = CPoint (-1, -1);
OnMouseMove (0, point);
return; // not captured
}
::ReleaseCapture();
if (m_pWndLastCapture != NULL)
{
m_pWndLastCapture->SetCapture ();
m_pWndLastCapture = NULL;
}
m_iHighlighted = HitTest (point);
CBCGToolbarButton* pButton = GetButton(m_iButtonCapture);
if (pButton == NULL)
{
ASSERT (FALSE);
return;
}
ASSERT(!(pButton->m_nStyle & TBBS_SEPARATOR));
UINT nIDCmd = 0;
UINT nNewStyle = (pButton->m_nStyle & ~TBBS_PRESSED);
if (m_iButtonCapture == m_iHighlighted)
{
// we did not lose the capture
if (HitTest(point) == m_iButtonCapture)
{
// give button a chance to update
UpdateButton(m_iButtonCapture);
// then check for disabled state
if (!(pButton->m_nStyle & TBBS_DISABLED))
{
// pressed, will send command notification
nIDCmd = pButton->m_nID;
if (pButton->m_nStyle & TBBS_CHECKBOX)
{
// auto check: three state => down
if (nNewStyle & TBBS_INDETERMINATE)
nNewStyle &= ~TBBS_INDETERMINATE;
nNewStyle ^= TBBS_CHECKED;
}
}
}
}
if (m_hookMouseHelp == NULL)
{
GetOwner()->SendMessage(WM_SETMESSAGESTRING, AFX_IDS_IDLEMESSAGE);
}
int iButtonCapture = m_iButtonCapture;
m_iButtonCapture = -1;
m_iHighlighted = -1;
HWND hwndSaved = m_hWnd;
RestoreFocus ();
if (HitTest(point) == iButtonCapture &&
!OnSendCommand (pButton) &&
nIDCmd != 0 && nIDCmd != (UINT) -1)
{
InvalidateButton (iButtonCapture);
UpdateWindow(); // immediate feedback
AddCommandUsage (nIDCmd);
GetOwner()->SendMessage (WM_COMMAND, nIDCmd); // send command
}
if (::IsWindow (hwndSaved) && // "This" may be destoyed now!
iButtonCapture < m_Buttons.GetCount ()) // Button may disappear now!
{
SetButtonStyle(iButtonCapture, nNewStyle);
UpdateButton(iButtonCapture);
InvalidateButton (iButtonCapture);
UpdateWindow(); // immediate feedback
m_ptLastMouse = CPoint (-1, -1);
OnMouseMove (0, point);
}
}
//*************************************************************************************
void CBCGToolBar::OnCancelMode()
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -