📄 ctrlcoolbar.cpp
字号:
BOOL bRight = FALSE;
for ( POSITION pos = m_pItems.GetHeadPosition() ; pos ; )
{
CCoolBarItem* pItem = (CCoolBarItem*)m_pItems.GetNext( pos );
if ( pItem->m_nID == ID_RIGHTALIGN && ! bRight )
{
int nRight = 0;
bRight = TRUE;
for ( POSITION pos2 = pos ; pos2 ; )
{
CCoolBarItem* pRight = (CCoolBarItem*)m_pItems.GetNext( pos2 );
if ( pRight->m_bVisible ) nRight += pRight->m_nWidth;
}
if ( rcClient.right - rcItem.left >= nRight + MARGIN_WIDTH )
{
rcItem.left = rcClient.right - nRight - MARGIN_WIDTH;
}
}
else if ( pItem->m_bVisible )
{
rcItem.right = rcItem.left + pItem->m_nWidth;
rcCopy.CopyRect( &rcItem );
CWnd* pCtrl = ( pItem->m_nCtrlID ) ? GetDlgItem( pItem->m_nCtrlID ) : NULL;
pItem->Paint( pDC, rcCopy, m_pDown == pItem,
m_pHot == pItem || ( pCtrl && pCtrl == GetFocus() ),
m_bMenuGray, bTransparent );
if ( ! bTransparent ) pDC->ExcludeClipRect( &rcItem );
if ( pCtrl ) SmartMove( pCtrl, &rcCopy );
rcItem.OffsetRect( rcItem.Width(), 0 );
}
else if ( pItem->m_nCtrlID )
{
CWnd* pCtrl = GetDlgItem( pItem->m_nCtrlID );
if ( pCtrl && pCtrl->IsWindowVisible() ) pCtrl->ShowWindow( SW_HIDE );
}
}
pDC->SelectObject( pOldFont );
}
void CCoolBarCtrl::SmartMove(CWnd* pCtrl, CRect* pRect)
{
CRect rc;
pCtrl->GetWindowRect( &rc );
ScreenToClient( &rc );
if ( rc != *pRect )
{
pCtrl->SetWindowPos( NULL, pRect->left, pRect->top, pRect->Width(),
pRect->Height(), SWP_NOZORDER|SWP_SHOWWINDOW );
}
else if ( ! pCtrl->IsWindowVisible() )
{
pCtrl->ShowWindow( SW_SHOW );
}
}
HBRUSH CCoolBarCtrl::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
HBRUSH hbr = CControlBar::OnCtlColor( pDC, pWnd, nCtlColor );
if ( nCtlColor == CTLCOLOR_STATIC )
{
pDC->SetBkColor( CoolInterface.m_crMidtone );
if ( m_crBack != CoolInterface.m_crMidtone )
{
if ( m_brBack.m_hObject ) m_brBack.DeleteObject();
m_brBack.CreateSolidBrush( m_crBack = CoolInterface.m_crMidtone );
}
hbr = m_brBack;
}
return hbr;
}
void CCoolBarCtrl::OnUpdateCmdUI(CFrameWnd* pTarget, BOOL bDisableIfNoHndler)
{
UINT nIndex = 0;
BOOL bDirty = FALSE;
BOOL bLocked = FALSE;
if ( m_pSyncObject != NULL )
{
bLocked = m_pSyncObject->Lock( 200 );
}
for ( POSITION pos = m_pItems.GetHeadPosition() ; pos ; )
{
CCoolBarItem* pItem = (CCoolBarItem*)m_pItems.GetNext( pos );
if ( pItem->m_nID == ID_SEPARATOR ) continue;
if ( pItem->m_nCtrlID ) continue;
pItem->m_pOther = this;
pItem->m_nIndex = nIndex++;
pItem->m_nIndexMax = m_pItems.GetCount();
pItem->m_bDirty = FALSE;
BOOL bEnabled = pItem->m_bEnabled;
if ( ! CWnd::OnCmdMsg( pItem->m_nID, CN_UPDATE_COMMAND_UI, pItem, NULL ) )
{
pItem->DoUpdate( pTarget, bDisableIfNoHndler );
}
pItem->m_bDirty |= ( pItem->m_bEnabled != bEnabled );
bDirty |= pItem->m_bDirty;
}
if ( bLocked ) m_pSyncObject->Unlock();
if ( bDirty ) OnUpdated();
}
void CCoolBarCtrl::OnMouseMove(UINT nFlags, CPoint point)
{
CCoolBarItem* pItem = HitTest( point );
if ( m_pDown && m_pDown != pItem ) pItem = NULL;
if ( pItem != m_pHot )
{
m_pHot = pItem;
Invalidate();
}
if ( ! m_bTimer )
{
SetTimer( 1, 100, NULL );
m_bTimer = TRUE;
}
CControlBar::OnMouseMove( nFlags, point );
}
void CCoolBarCtrl::OnTimer(UINT nIDEvent)
{
if ( m_bRecalc )
{
CMDIFrameWnd* pOwner = (CMDIFrameWnd*)GetOwner();
if ( pOwner != NULL && pOwner->IsKindOf( RUNTIME_CLASS(CMDIFrameWnd) ) )
{
if ( ! pOwner->IsIconic() )
{
pOwner->RecalcLayout();
m_bRecalc = FALSE;
}
}
}
if ( nIDEvent == 1 && ( ! m_bMenuGray || m_pDown == NULL ) )
{
CRect rcWindow;
CPoint point;
GetCursorPos( &point );
ScreenToClient( &point );
GetClientRect( &rcWindow );
if ( rcWindow.PtInRect( point ) && GetTopLevelParent()->IsWindowEnabled() )
{
CCoolBarItem* pItem = HitTest( point );
if ( m_pDown && m_pDown != pItem ) pItem = NULL;
if ( pItem != m_pHot )
{
m_pHot = pItem;
Invalidate();
}
}
else
{
KillTimer( nIDEvent );
m_bTimer = FALSE;
if ( m_pHot )
{
m_pHot = NULL;
Invalidate();
}
}
}
CControlBar::OnTimer( nIDEvent );
}
void CCoolBarCtrl::OnLButtonDown(UINT nFlags, CPoint point)
{
CWnd* pFocus = GetFocus();
if ( pFocus && pFocus->GetParent() == this ) SetFocus();
CCoolBarItem* pItem = HitTest( point );
if ( pItem && pItem->m_bEnabled )
{
m_pDown = m_pHot = pItem;
SetCapture();
Invalidate();
return;
}
if ( m_bDragForward )
GetParent()->SendMessage( WM_LBUTTONDOWN, nFlags, MAKELONG( point.x, point.y ) );
else
CControlBar::OnLButtonDown( nFlags, point );
}
void CCoolBarCtrl::OnLButtonUp(UINT nFlags, CPoint point)
{
CCoolBarItem* pItem = HitTest( point );
if ( m_pDown != NULL )
{
BOOL bOn = ( m_pDown == pItem );
m_pDown = NULL;
m_pHot = pItem;
ReleaseCapture();
Invalidate();
if ( bOn ) GetOwner()->PostMessage( WM_COMMAND, pItem->m_nID );
return;
}
if ( m_bDragForward )
GetParent()->SendMessage( WM_LBUTTONUP, nFlags, MAKELONG( point.x, point.y ) );
else
CControlBar::OnRButtonUp( nFlags, point );
}
void CCoolBarCtrl::OnLButtonDblClk(UINT nFlags, CPoint point)
{
CCoolBarCtrl::OnLButtonDown( nFlags, point );
}
void CCoolBarCtrl::OnRButtonDown(UINT nFlags, CPoint point)
{
if ( m_pDown != NULL || m_pHot != NULL )
{
m_pDown = m_pHot = NULL;
ReleaseCapture();
Invalidate();
}
CControlBar::OnRButtonDown( nFlags, point );
}
/////////////////////////////////////////////////////////////////////////////
// CCoolBar message forwarding
BOOL CCoolBarCtrl::OnCommand(WPARAM wParam, LPARAM lParam)
{
if ( HIWORD( wParam ) == EN_SETFOCUS ||
HIWORD( wParam ) == EN_KILLFOCUS ) Invalidate();
GetParent()->SendMessage( WM_COMMAND, wParam, lParam );
return CControlBar::OnCommand( wParam, lParam );
}
BOOL CCoolBarCtrl::PreTranslateMessage(MSG* pMsg)
{
if ( pMsg->message == WM_KEYDOWN )
{
if ( pMsg->wParam == VK_RETURN )
{
GetOwner()->PostMessage( WM_COMMAND, MAKELONG( GetDlgCtrlID(), BN_CLICKED ), (LPARAM)GetSafeHwnd() );
}
else if ( pMsg->wParam == VK_ESCAPE )
{
GetOwner()->PostMessage( WM_COMMAND, MAKELONG( GetDlgCtrlID(), BN_DBLCLK ), (LPARAM)GetSafeHwnd() );
}
}
return CControlBar::PreTranslateMessage( pMsg );
}
void CCoolBarCtrl::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
{
GetParent()->SendMessage( WM_HSCROLL, MAKELONG( nSBCode, nPos ), (LPARAM)pScrollBar->GetSafeHwnd() );
}
/////////////////////////////////////////////////////////////////////////////
// CCoolBarItem construction
CCoolBarItem::CCoolBarItem(CCoolBarCtrl* pBar, UINT nID, int nImage)
{
m_pBar = pBar;
m_nID = nID;
m_nImage = nImage;
m_bVisible = TRUE;
m_bEnabled = TRUE;
m_bChecked = FALSE;
m_crText = 0xFFFFFFFF;
switch ( nID )
{
case ID_SEPARATOR:
m_nWidth = SEPARATOR_WIDTH;
break;
case ID_RIGHTALIGN:
m_nWidth = 0;
break;
default:
m_nWidth = IMAGEBUTTON_WIDTH;
break;
}
m_nCtrlID = 0;
m_nCtrlHeight = CONTROL_HEIGHT;
}
CCoolBarItem::CCoolBarItem(CCoolBarCtrl* pBar, CCoolBarItem* pCopy)
{
m_pBar = pBar;
m_nID = pCopy->m_nID;
m_nImage = pCopy->m_nImage;
m_sTip = pCopy->m_sTip;
m_bVisible = pCopy->m_bVisible;
m_bEnabled = pCopy->m_bEnabled;
m_bChecked = pCopy->m_bChecked;
m_crText = pCopy->m_crText;
m_nWidth = pCopy->m_nWidth;
m_nCtrlID = pCopy->m_nCtrlID;
m_nCtrlHeight = pCopy->m_nCtrlHeight;
/* if ( m_nImage < 0 ) */ m_nImage = CoolInterface.ImageForID( m_nID );
SetText( pCopy->m_sText );
}
CCoolBarItem::~CCoolBarItem()
{
}
/////////////////////////////////////////////////////////////////////////////
// CCoolBarItem CCmdUI handlers
void CCoolBarItem::Show(BOOL bOn)
{
if ( this == NULL ) return;
if ( m_bVisible == bOn ) return;
m_bVisible = bOn;
m_bDirty = TRUE;
}
void CCoolBarItem::Enable(BOOL bOn)
{
if ( this == NULL ) return;
m_bEnableChanged = TRUE;
m_bEnabled = bOn;
}
void CCoolBarItem::SetCheck(int nCheck)
{
if ( this == NULL ) return;
if ( m_bChecked == ( nCheck == 1 ) ) return;
m_bChecked = ( nCheck == 1 );
m_bDirty = TRUE;
}
void CCoolBarItem::SetText(LPCTSTR lpszText)
{
if ( this == NULL ) return;
if ( m_sText == lpszText ) return;
m_sText = lpszText;
m_bDirty = TRUE;
CDC dc;
dc.Attach( ::GetDC( 0 ) );
CFont* pOld = (CFont*)dc.SelectObject( m_pBar->m_bBold ? &CoolInterface.m_fntBold : &CoolInterface.m_fntNormal );
m_nWidth = BUTTON_WIDTH + dc.GetTextExtent( m_sText ).cx;
if ( m_nImage >= 0 ) m_nWidth += IMAGE_WIDTH + TEXT_GAP;
dc.SelectObject( pOld );
::ReleaseDC( 0, dc.Detach() );
}
void CCoolBarItem::SetTip(LPCTSTR pszTip)
{
if ( this == NULL ) return;
if ( m_sTip == pszTip ) return;
m_sTip = pszTip;
m_bDirty = TRUE;
}
void CCoolBarItem::SetTextColour(COLORREF crText)
{
if ( this == NULL ) return;
if ( m_crText == crText ) return;
m_crText = crText;
m_bDirty = TRUE;
}
void CCoolBarItem::SetImage(UINT nCommandID)
{
if ( this == NULL ) return;
int nImage = CoolInterface.ImageForID( nCommandID );
if ( nImage == m_nImage ) return;
m_nImage = nImage;
m_bDirty = TRUE;
}
CCoolBarItem* CCoolBarItem::FromCmdUI(CCmdUI* pUI)
{
if ( pUI->m_pOther == NULL ) return NULL;
if ( pUI->m_pOther->IsKindOf( RUNTIME_CLASS(CCoolBarCtrl) ) == FALSE ) return NULL;
return (CCoolBarItem*)pUI;
}
/////////////////////////////////////////////////////////////////////////////
// CCoolBarItem painting
void CCoolBarItem::Paint(CDC* pDC, CRect& rc, BOOL bDown, BOOL bHot, BOOL bMenuGray, BOOL bTransparent)
{
COLORREF crBackground;
if ( m_nID == ID_SEPARATOR )
{
if ( ! bTransparent ) pDC->FillSolidRect( rc.left, rc.top, 3, rc.Height(), CoolInterface.m_crMidtone );
pDC->Draw3dRect( rc.left + 3, rc.top, 1, rc.Height(), CoolInterface.m_crDisabled, CoolInterface.m_crDisabled );
if ( ! bTransparent ) pDC->FillSolidRect( rc.left + 4, rc.top, 3, rc.Height(), CoolInterface.m_crMidtone );
return;
}
if ( m_nCtrlID )
{
for ( int nShrink = rc.Height() - m_nCtrlHeight ; nShrink > 0 ; nShrink -= 2 )
{
if ( ! bTransparent ) pDC->Draw3dRect( &rc, CoolInterface.m_crMidtone, CoolInterface.m_crMidtone );
rc.DeflateRect( 0, 1 );
}
rc.DeflateRect( 1, 0 );
}
else
{
if ( ! bTransparent ) pDC->Draw3dRect( &rc, CoolInterface.m_crMidtone, CoolInterface.m_crMidtone );
rc.DeflateRect( 1, 1 );
}
// if ( ( m_bEnabled || m_nCtrlID ) && ( bHot || bDown || m_bChecked ) )
if ( m_bEnabled && ( bHot || bDown || m_bChecked ) )
{
if ( bMenuGray && bDown )
{
pDC->Draw3dRect( &rc, CoolInterface.m_crDisabled, CoolInterface.m_crDisabled );
}
else
{
pDC->Draw3dRect( &rc, CoolInterface.m_crBorder, CoolInterface.m_crBorder );
}
rc.DeflateRect( 1, 1 );
if ( bMenuGray && bDown )
{
crBackground = CoolInterface.m_crBackNormal;
}
else if ( m_bChecked )
{
crBackground = bHot ? CoolInterface.m_crBackCheckSel : CoolInterface.m_crBackCheck;
}
else
{
crBackground = bDown && bHot ? CoolInterface.m_crBackCheckSel : CoolInterface.m_crBackSel;
}
}
else
{
if ( bTransparent )
{
crBackground = CLR_NONE;
}
else
{
crBackground = CoolInterface.m_crMidtone;
pDC->Draw3dRect( &rc, crBackground, crBackground );
}
rc.DeflateRect( 1, 1 );
}
if ( m_nCtrlID )
{
if ( m_nCtrlHeight == CONTROL_HEIGHT )
{
pDC->Draw3dRect( &rc, CoolInterface.m_crWindow, CoolInterface.m_crWindow );
rc.DeflateRect( 1, 1 );
}
return;
}
if ( crBackground == CLR_NONE )
{
pDC->SetBkMode( TRANSPARENT );
}
else
{
pDC->SetBkMode( OPAQUE );
pDC->SetBkColor( crBackground );
}
if ( m_sText.GetLength() )
{
if ( m_crText != 0xFFFFFFFF )
pDC->SetTextColor( m_crText );
else if ( ! m_bEnabled )
pDC->SetTextColor( CoolInterface.m_crDisabled );
else if ( ( bHot || bDown || m_bChecked ) && ( ! bMenuGray || ! bDown ) )
pDC->SetTextColor( CoolInterface.m_crCmdTextSel );
else
pDC->SetTextColor( CoolInterface.m_crCmdText );
rc.left += ( m_nImage >= 0 ) ? 20 : 1;
int nY = ( rc.top + rc.bottom ) / 2 - pDC->GetTextExtent( m_sText ).cy / 2 - 1;
if ( crBackground == CLR_NONE )
pDC->ExtTextOut( rc.left + 2, nY, ETO_CLIPPED, &rc, m_sText, NULL );
else
pDC->ExtTextOut( rc.left + 2, nY, ETO_CLIPPED|ETO_OPAQUE, &rc, m_sText, NULL );
rc.right = rc.left;
rc.left -= ( m_nImage >= 0 ) ? 20 : 1;
}
if ( m_nImage >= 0 )
{
CPoint ptImage( rc.left + 3, ( rc.top + rc.bottom ) / 2 - 8 );
if ( ! m_bEnabled )
{
ImageList_DrawEx( CoolInterface.m_pImages.GetSafeHandle(), m_nImage, pDC->GetSafeHdc(),
ptImage.x, ptImage.y, 0, 0, crBackground, CoolInterface.m_crShadow, ILD_BLEND50 );
pDC->ExcludeClipRect( ptImage.x, ptImage.y, ptImage.x + 16, ptImage.y + 16 );
}
else if ( m_bChecked )
{
ImageList_DrawEx( CoolInterface.m_pImages.GetSafeHandle(), m_nImage, pDC->GetSafeHdc(),
ptImage.x, ptImage.y, 0, 0, crBackground, CLR_NONE, ILD_NORMAL );
pDC->ExcludeClipRect( ptImage.x, ptImage.y, ptImage.x + 16, ptImage.y + 16 );
}
else if ( ( bHot && ! bDown ) || ( bDown && ! bHot ) )
{
ptImage.Offset( 1, 1 );
pDC->SetTextColor( CoolInterface.m_crShadow );
ImageList_DrawEx( CoolInterface.m_pImages.GetSafeHandle(), m_nImage, pDC->GetSafeHdc(),
ptImage.x, ptImage.y, 0, 0, crBackground, CLR_NONE, ILD_MASK );
ptImage.Offset( -2, -2 );
if ( crBackground != CLR_NONE )
{
pDC->FillSolidRect( ptImage.x, ptImage.y, 18, 2, crBackground );
pDC->FillSolidRect( ptImage.x, ptImage.y + 2, 2, 16, crBackground );
}
ImageList_DrawEx( CoolInterface.m_pImages.GetSafeHandle(), m_nImage, pDC->GetSafeHdc(),
ptImage.x, ptImage.y, 0, 0, CLR_NONE, CLR_NONE, ILD_NORMAL );
pDC->ExcludeClipRect( ptImage.x, ptImage.y, ptImage.x + 18, ptImage.y + 18 );
}
else
{
ImageList_DrawEx( CoolInterface.m_pImages.GetSafeHandle(), m_nImage, pDC->GetSafeHdc(),
ptImage.x, ptImage.y, 0, 0, crBackground, CoolInterface.m_crBackNormal,
bDown ? ILD_NORMAL : ILD_BLEND25 );
pDC->ExcludeClipRect( ptImage.x, ptImage.y, ptImage.x + 16, ptImage.y + 16 );
}
}
if ( crBackground != CLR_NONE ) pDC->FillSolidRect( &rc, crBackground );
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -