📄 toolbarex.cpp
字号:
if ( nHotItem >= 0 )
{
TBBUTTON tbinfo;
VERIFY( m_tbCtrl.GetButton( nHotItem, &tbinfo ) );
if ( tbinfo.idCommand != 0 )
{
SendMessage( WM_COMMAND, tbinfo.idCommand );
break;
}
}
SendMessage( WM_TB_ENDMODALLOOP );
break;
}
default:
break;
}
}
LRESULT CALLBACK CToolBarPopup::KeyboardProc( int code, WPARAM wParam, LPARAM lParam )
{
ASSERT( m_pPopup != 0 );
if ( code == HC_ACTION )
{
CWnd* pCapture = GetCapture();
if ( ( pCapture == m_pPopup ) ||
( pCapture == &m_pPopup->m_tbCtrl ) )
{
if ( !( HIWORD( lParam ) & KF_UP ) )
{
m_pPopup->OnKeyDown( wParam );
}
return 1;
}
}
return ::CallNextHookEx( m_hKeyboardHook, code, wParam, lParam );
}
/////////////////////////////////////////////////////////////////////////////
// Overrides
BOOL CToolBarPopup::OnCommand( WPARAM wParam, LPARAM /*lParam*/ )
{
// Dismiss menu
SendMessage( WM_TB_ENDMODALLOOP );
// Forward command to the original toolbar window
m_pToolBar->PostMessage( WM_COMMAND, LOWORD( wParam ), 0 );
return TRUE; // command was processed
}
BOOL CToolBarPopup::OnNotify( WPARAM wParam, LPARAM lParam, LRESULT* pResult )
{
NMHDR* pNMHDR = ( NMHDR* )lParam;
if ( pNMHDR->hwndFrom == m_tbCtrl.m_hWnd )
{
// Handle certain notifications from embedded toolbar control
switch ( pNMHDR->code )
{
case NM_RELEASEDCAPTURE:
SetCapture();
*pResult = 0;
break;
case NM_CUSTOMDRAW:
*pResult = m_pToolBar->DoCustomDraw( pNMHDR, &m_tbCtrl );
break;
case TBN_GETINFOTIP:
if ( !m_bTextLabels )
{
NMTBGETINFOTIP* lptbgit = ( NMTBGETINFOTIP* )pNMHDR;
CString strTip;
m_pToolBar->GetButtonTip( lptbgit->iItem, strTip );
_tcsncpy( lptbgit->pszText, strTip, lptbgit->cchTextMax );
}
*pResult = 0;
break;
default: // forward message to the parent of the original toolbar
*pResult = m_pToolBar->GetParent()->SendMessage( WM_NOTIFY, wParam, lParam );
break;
}
return TRUE;
}
return CWnd::OnNotify( wParam, lParam, pResult );
}
LRESULT CToolBarPopup::WindowProc( UINT message, WPARAM wParam, LPARAM lParam )
{
if ( WM_MOUSEFIRST <= message && message <= WM_MOUSELAST )
{
DWORD dwPos = ::GetMessagePos();
CPoint ptScreen( LOWORD( dwPos ), HIWORD( dwPos ) );
CWnd* pWnd = WindowFromPoint( ptScreen );
if ( pWnd != 0 )
{
CPoint ptClient( ptScreen );
pWnd->ScreenToClient( &ptClient );
switch ( message )
{
case WM_MOUSEMOVE:
{
// Check if hot item should be changed
bool bOverTbCtrl = ( pWnd == &m_tbCtrl );
if ( bOverTbCtrl )
{
int nHit = m_tbCtrl.HitTest( &ptClient );
m_tbCtrl.SetHotItem( nHit );
// Let tooltip control process mouse event
CToolTipCtrl* pTtCtrl = m_tbCtrl.GetToolTips();
if ( pTtCtrl != 0 )
{
MSG msg;
msg.hwnd = m_tbCtrl.m_hWnd;
msg.message = WM_MOUSEMOVE;
msg.wParam = wParam;
msg.lParam = MAKELPARAM( ptClient.x, ptClient.y );
msg.pt = ptScreen;
msg.time = ::GetMessageTime();
pTtCtrl->RelayEvent( &msg );
}
}
else if ( m_bOverTbCtrl )
{
m_tbCtrl.SetHotItem( -1 );
}
m_bOverTbCtrl = bOverTbCtrl;
return 0L;
}
case WM_LBUTTONDOWN:
if ( pWnd != this )
{
// Dismiss menu if user has clicked outside the window
if ( pWnd != &m_tbCtrl )
{
SendMessage( WM_TB_ENDMODALLOOP );
}
// Forward this mouse event to the window that was clicked
LPARAM nPosition = MAKELPARAM( ptScreen.x, ptScreen.y );
WPARAM nHitTest = pWnd->SendMessage( WM_NCHITTEST, 0, nPosition );
if ( nHitTest == HTCLIENT )
{
nPosition = MAKELPARAM( ptClient.x, ptClient.y );
pWnd->PostMessage( WM_LBUTTONDOWN, wParam, nPosition );
}
else
{
pWnd->PostMessage( WM_NCLBUTTONDOWN, nHitTest, nPosition );
}
}
return 0L;
default:
break;
}
}
}
return CWnd::WindowProc( message, wParam, lParam );
}
/////////////////////////////////////////////////////////////////////////
// CToolBarPopup message handlers
BEGIN_MESSAGE_MAP(CToolBarPopup, CWnd)
//{{AFX_MSG_MAP(CToolBarPopup)
ON_WM_CAPTURECHANGED()
ON_WM_CREATE()
ON_WM_MOUSEACTIVATE()
//}}AFX_MSG_MAP
ON_MESSAGE_VOID( WM_TB_ENDMODALLOOP, OnEndModalLoop )
END_MESSAGE_MAP()
void CToolBarPopup::OnEndModalLoop()
{
EndModalLoop( 0 );
}
void CToolBarPopup::OnCaptureChanged( CWnd* pWnd )
{
if ( ( pWnd != this ) && ( pWnd != &m_tbCtrl ) && ContinueModal() )
{
PostMessage( WM_TB_ENDMODALLOOP ); // dismiss menu
}
CWnd::OnCaptureChanged( pWnd );
}
int CToolBarPopup::OnCreate( LPCREATESTRUCT lpCreateStruct )
{
if ( CWnd::OnCreate( lpCreateStruct ) == -1 )
{
return -1;
}
// Create embedded toolbar control
if ( !m_tbCtrl.Create(
TBSTYLE_FLAT | TBSTYLE_WRAPABLE | TBSTYLE_LIST | TBSTYLE_TOOLTIPS |
CCS_NODIVIDER | CCS_NOPARENTALIGN | CCS_NORESIZE |
WS_CHILD | WS_VISIBLE | WS_CLIPSIBLINGS | WS_CLIPCHILDREN,
CRect( 0, 0, 0, 0 ), this, m_pToolBar->GetDlgCtrlID() ) )
{
return -1;
}
m_tbCtrl.SetExtendedStyle( TBSTYLE_EX_DRAWDDARROWS | TBSTYLE_EX_MIXEDBUTTONS );
VERIFY( m_tbCtrl.GetToolTips()->ModifyStyle( 0, TTS_ALWAYSTIP ) );
// There could be only two modes, depending on text options
// of the original toolbar: with or without text labels.
ETextOptions eTextOptions = m_pToolBar->GetTextOptions();
m_bTextLabels =
( eTextOptions == toTextLabels ) ||
( eTextOptions == toTextOnRight );
// Copy all required information from the original toolbar
CToolBarCtrl& tbCtrl = m_pToolBar->GetToolBarCtrl();
m_tbCtrl.SetImageList( tbCtrl.GetImageList() );
m_tbCtrl.SetHotImageList( tbCtrl.GetHotImageList() );
m_tbCtrl.SetDisabledImageList( tbCtrl.GetDisabledImageList() );
CRect rcItem, rcClient;
tbCtrl.GetClientRect( rcClient );
TBBUTTON tbinfo;
int nMaxWidth = 0;
int nButtons = tbCtrl.GetButtonCount();
int nIndex;
for ( nIndex = 0; nIndex < nButtons; nIndex++ )
{
tbCtrl.GetItemRect( nIndex, rcItem );
if ( rcItem.right > rcClient.right )
{
VERIFY( tbCtrl.GetButton( nIndex, &tbinfo ) );
if ( !( tbinfo.fsStyle & TBSTYLE_SEP ) && !( tbinfo.fsState & TBSTATE_HIDDEN ) )
{
CString strButtonText;
m_pToolBar->GetButtonText( tbinfo.idCommand, strButtonText );
CString strToAdd( strButtonText, strButtonText.GetLength() + 1 );
tbinfo.iString = m_tbCtrl.AddStrings( strToAdd );
tbinfo.fsStyle |= TBSTYLE_AUTOSIZE | ( m_bTextLabels ? BTNS_SHOWTEXT : 0 );
VERIFY( m_tbCtrl.AddButtons( 1, &tbinfo ) );
VERIFY( m_tbCtrl.GetItemRect( m_tbCtrl.CommandToIndex( tbinfo.idCommand ), rcItem ) );
nMaxWidth = max( nMaxWidth, rcItem.Width() );
}
}
}
nButtons = m_tbCtrl.GetButtonCount();
if ( nButtons == 0 )
{
ASSERT( false ); // this should never happen
return -1;
}
if ( m_bTextLabels )
{
TBBUTTONINFO tbbi;
tbbi.cbSize = sizeof( tbbi );
tbbi.dwMask = TBIF_SIZE | TBIF_STYLE;
for ( nIndex = 0; nIndex < nButtons; nIndex++ )
{
VERIFY( m_tbCtrl.GetButton( nIndex, &tbinfo ) );
tbbi.cx = ( WORD )nMaxWidth;
tbbi.fsStyle = ( BYTE )( tbinfo.fsStyle & ~TBSTYLE_AUTOSIZE );
VERIFY( m_tbCtrl.SetButtonInfo( tbinfo.idCommand, &tbbi ) );
}
}
m_tbCtrl.AutoSize();
// Calc toolbar size
if ( nButtons > 1 )
{
m_tbCtrl.SetRows( nButtons, m_bTextLabels, rcClient );
}
else
{
VERIFY( m_tbCtrl.GetItemRect( 0, rcClient ) );
}
m_tbCtrl.MoveWindow( rcClient );
return 0;
}
int CToolBarPopup::OnMouseActivate( CWnd* /*pDesktopWnd*/, UINT /*nHitTest*/, UINT /*message*/ )
{
return MA_NOACTIVATEANDEAT; // just in case
}
void CToolBarEx::AddButton(int index,TBBUTTONEX lpButtons, CString text)
{
CToolBarCtrl& tbCtrl = GetToolBarCtrl();
if ( lpButtons.bInitiallyVisible )
{
VERIFY( tbCtrl.AddButtons( 1, &lpButtons.tbinfo) );
}
m_aButtons.Add( lpButtons);
SetTextOptions(index,toTextOnRight,text);
m_itemName.AddTail(text);
}
void CToolBarEx::SetTextOptions(int index,ETextOptions eTextOptions, CString text)
{
ASSERT( ::IsWindow( m_hWnd ) );
ASSERT( GetStyle() & TBSTYLE_TOOLTIPS );
ASSERT( !( GetBarStyle() & CBRS_TOOLTIPS ) );
ASSERT( IsTextOptionAvailable( eTextOptions ) );
m_eTextOptions = eTextOptions;
// Modify toolbar style according to new text options
ModifyStyle(
( eTextOptions == toTextOnRight ) ? 0 : TBSTYLE_LIST,
( eTextOptions == toTextOnRight ) ? TBSTYLE_LIST : 0 );
CToolBarCtrl& tbCtrl = GetToolBarCtrl();
DWORD dwStyleEx = tbCtrl.GetExtendedStyle();
tbCtrl.SetExtendedStyle(
( eTextOptions == toTextOnRight ) ?
( dwStyleEx | TBSTYLE_EX_MIXEDBUTTONS ) :
( dwStyleEx & ~TBSTYLE_EX_MIXEDBUTTONS ) );
VERIFY( tbCtrl.SetMaxTextRows(
( eTextOptions == toNoTextLabels ) ? 0 : 1 ) );
// Modify all (even currently hidden ones) buttons in internal cache
// for ( nIndex = 0; nIndex <= m_aButtons.GetUpperBound(); nIndex++ )
{
TBBUTTON& tbinfo = m_aButtons[ index ].tbinfo;
if ( !( tbinfo.fsStyle & TBSTYLE_SEP ) )
{
CString strButtonText=text;
CString strToAdd( strButtonText, strButtonText.GetLength() + 1 );
tbinfo.iString = tbCtrl.AddStrings( strToAdd );
switch ( eTextOptions )
{
case toTextLabels:
tbinfo.fsStyle &= ~( TBSTYLE_AUTOSIZE | BTNS_SHOWTEXT );
break;
case toTextOnRight:
tbinfo.fsStyle |= ( TBSTYLE_AUTOSIZE |
( HasButtonText( tbinfo.idCommand ) ? BTNS_SHOWTEXT : 0 ) );
break;
case toNoTextLabels:
tbinfo.fsStyle &= ~BTNS_SHOWTEXT;
tbinfo.fsStyle |= TBSTYLE_AUTOSIZE;
break;
}
}
}
// If requested, reflect changes immediately
// if ( bUpdate )
//// {
ReloadButtons();
UpdateParentBandInfo();
// }
}
void CToolBarEx::SetCustomizeMode(bool customize)
{
m_customize=customize;
}
bool CToolBarEx::AddGloomIcon(CImageList *imagelist, HICON hIcon)
{
ICONINFO iconInfo;
ZeroMemory(&iconInfo,sizeof(iconInfo));
if(!GetIconInfo(hIcon,&iconInfo))
return FALSE;
CSize size = ( m_eIconOptions == ioSmallIcons ) ? szImageSmall : szImageLarge;
CDC myDC;
myDC.CreateCompatibleDC(0);
CBitmap bmColor;
bmColor.Attach(iconInfo.hbmColor);
CBitmap bmMask;
bmMask.Attach(iconInfo.hbmMask);
CBitmap* pOldBitmap = myDC.SelectObject(&bmColor);
COLORREF crPixel;
for(int i=0;i<size.cx;++i)
{
for(int j=0;j<size.cy;++j)
{
crPixel = myDC.GetPixel(i,j);
myDC.SetPixel(i,j,DarkenColor(25,crPixel));
}
}
myDC.SelectObject(pOldBitmap);
imagelist->Add(&bmColor,&bmMask);
return TRUE;
}
COLORREF CToolBarEx::DarkenColor(long lScale, COLORREF lColor)
{
long R = MulDiv(GetRValue(lColor),(255-lScale),255);
long G = MulDiv(GetGValue(lColor),(255-lScale),255);
long B = MulDiv(GetBValue(lColor),(255-lScale),255);
return RGB(R, G, B);
}
bool CToolBarEx::AddGrayIcon(CImageList *imagelist, HICON hIcon)
{
ICONINFO iconInfo;
ZeroMemory(&iconInfo,sizeof(iconInfo));
if(!GetIconInfo(hIcon,&iconInfo))
return FALSE;
CSize size = ( m_eIconOptions == ioSmallIcons ) ? szImageSmall : szImageLarge;
CDC myDC;
myDC.CreateCompatibleDC(0);
CBitmap bmColor;
bmColor.Attach(iconInfo.hbmColor);
CBitmap bmMask;
bmMask.Attach(iconInfo.hbmMask);
CBitmap* pOldBitmap = myDC.SelectObject(&bmColor);
COLORREF crMenu = GetSysColor(COLOR_MENU);
COLORREF crPixel;
for(int i=0;i<size.cx;++i)
{
for(int j=0;j<size.cy;++j)
{
crPixel = myDC.GetPixel(i,j);
//myDC.SetPixel(i,j,GrayColor(crPixel));
//myDC.SetPixel(i,j,MidColor(GrayColor(crPixel),crMenu));
myDC.SetPixel(i,j,MixedColor(LightenColor(100,GrayColor(crPixel)),crMenu));
}
}
myDC.SelectObject(pOldBitmap);
imagelist->Add(&bmColor,&bmMask);
return TRUE;
}
COLORREF CToolBarEx::MixedColor(COLORREF ColA, COLORREF ColB)
{
// ( 86a + 14b ) / 100
int Red = MulDiv(86,GetRValue(ColA),100) + MulDiv(14,GetRValue(ColB),100);
int Green = MulDiv(86,GetGValue(ColA),100) + MulDiv(14,GetGValue(ColB),100);
int Blue = MulDiv(86,GetBValue(ColA),100) + MulDiv(14,GetBValue(ColB),100);
return RGB( Red,Green,Blue);
}
COLORREF CToolBarEx::LightenColor(long lScale, COLORREF lColor)
{
long R = MulDiv(255-GetRValue(lColor),lScale,255)+GetRValue(lColor);
long G = MulDiv(255-GetGValue(lColor),lScale,255)+GetGValue(lColor);
long B = MulDiv(255-GetBValue(lColor),lScale,255)+GetBValue(lColor);
return RGB(R, G, B);
}
COLORREF CToolBarEx::GrayColor(COLORREF crColor)
{
int Gray = (int)(((int)GetRValue(crColor)*0.3) + GetGValue(crColor)*0.59 + GetBValue(crColor)*0.11);
return RGB( Gray,Gray,Gray);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -