📄 toolbarex.cpp
字号:
if( ! bBtnEnabled )
// gray out that button
DrawDisabledButton(dc, rc);
cdrw.NotifyItemPostPaint(i);
}
}
dc.SelectObject(pOldFont);
if( ! m_bDeleteImgList )
imglist.Detach();
// last but not least: inform the parent for end of painting
cdrw.NotifyPostPaint();
} else
// classic mode (or couldn't receive imagelist)
CToolBar::OnPaint();
}
void CToolBarEx :: DrawDisabledButton( CDC & dc, const CRect & rc ) const
{
// create a monochrome memory DC
CDC ddc;
ddc.CreateCompatibleDC(0);
CBitmap bmp;
bmp.CreateCompatibleBitmap(&ddc, rc.Width(), rc.Height());
CBitmap * pOldBmp = ddc.SelectObject(&bmp);
// build a mask
ddc.PatBlt(0, 0, rc.Width(), rc.Height(), WHITENESS);
dc.SetBkColor(m_clrBtnFace);
ddc.BitBlt(0, 0, rc.Width(), rc.Height(), &dc, rc.left, rc.top, SRCCOPY);
dc.SetBkColor(m_clrBtnHilight);
ddc.BitBlt(0, 0, rc.Width(), rc.Height(), &dc, rc.left, rc.top, SRCPAINT);
// Copy the image from the toolbar into the memory DC
// and draw it (grayed) back into the toolbar.
dc.FillSolidRect(rc.left, rc.top, rc.Width(), rc.Height(), m_clrBtnFace);
dc.SetBkColor(RGB(0, 0, 0));
dc.SetTextColor(RGB(255, 255, 255));
CBrush brShadow, brHilight;
brHilight.CreateSolidBrush(m_clrBtnHilight);
brShadow.CreateSolidBrush(m_clrBtnShadow);
CBrush * pOldBrush = dc.SelectObject(&brHilight);
dc.BitBlt(rc.left+1, rc.top+1, rc.Width(), rc.Height(), &ddc, 0, 0, 0x00E20746L);
dc.SelectObject(&brShadow);
dc.BitBlt(rc.left, rc.top, rc.Width(), rc.Height(), &ddc, 0, 0, 0x00E20746L);
// reset DCs
dc.SelectObject(pOldBrush);
ddc.SelectObject(pOldBmp);
ddc.DeleteDC();
bmp.DeleteObject();
}
void CToolBarEx :: DrawSeparator( CDC & dc, CRect & rc ) const
{
BOOL bHorz = ((m_dwStyle & CBRS_ORIENT_HORZ) != 0) ? TRUE : FALSE;
// make sure, this separator is not a placeholder for
// another control.
if( rc.Width() <= 8 )
{
if( bHorz )
{
// draw the separator bar in the middle
int x = (rc.left + rc.right) / 2;
rc.left = x-1; rc.right = x+1;
dc.Draw3dRect(
rc,
m_clrBtnShadow,
m_clrBtnHilight
);
}
else
{
// draw the separator bar in the middle
rc.left = rc.left - m_sizeButton.cx;
rc.right = rc.left + m_sizeButton.cx;
rc.top = rc.bottom+1;
rc.bottom = rc.top+3;
int y = (rc.top+rc.bottom)/2;
rc.top = y-1; rc.bottom = y+1;
dc.Draw3dRect(
rc,
m_clrBtnShadow,
m_clrBtnHilight
);
}
}
}
void CToolBarEx :: DrawGripper( CDC & dc ) const
{
if( m_dwStyle & CBRS_FLOATING )
return; // no gripper if floating
CRect gripper;
GetWindowRect(gripper);
ScreenToClient(gripper);
gripper.OffsetRect(-gripper.left, -gripper.top);
if( m_dwStyle & CBRS_ORIENT_HORZ )
{
// gripper at left
gripper.DeflateRect(4, 4);
gripper.right = gripper.left+3;
dc.Draw3dRect(
gripper,
m_clrBtnHilight,
m_clrBtnShadow
);
gripper.OffsetRect(3, 0);
dc.Draw3dRect(
gripper,
m_clrBtnHilight,
m_clrBtnShadow
);
}
else
{
// gripper at top
gripper.DeflateRect(4, 4);
gripper.bottom = gripper.top+3;
dc.Draw3dRect(
gripper,
m_clrBtnHilight,
m_clrBtnShadow
);
gripper.OffsetRect(0, 3);
dc.Draw3dRect(
gripper,
m_clrBtnHilight,
m_clrBtnShadow
);
}
}
void CToolBarEx :: OnUpdateCmdUI( CFrameWnd* pTarget, BOOL bDisableIfNoHndler )
{
if( m_bFlatLook )
{
// save current styles
register const int nBtn = GetToolBarCtrl().GetButtonCount();
register int nIdx;
for( nIdx = 0; nIdx < nBtn; ++nIdx )
m_Styles.SetAtGrow(nIdx, GetButtonStyle(nIdx));
// do base class processing
CToolBar::OnUpdateCmdUI(pTarget,bDisableIfNoHndler);
//check wether styles have been changed
for( nIdx = 0; nIdx < nBtn; ++nIdx )
{
if( m_Styles[nIdx] != GetButtonStyle(nIdx) )
{
// invalidate that button
CRect rc;
GetItemRect(nIdx, rc);
InvalidateRect(rc);
}
}
}
else
// simply delegate
CToolBar::OnUpdateCmdUI(pTarget,bDisableIfNoHndler);
}
void CToolBarEx::OnSysColorChange()
{
CToolBar::OnSysColorChange();
m_clrBtnFace = ::GetSysColor(COLOR_BTNFACE);
m_clrBtnHilight = ::GetSysColor(COLOR_BTNHILIGHT);
m_clrBtnShadow = ::GetSysColor(COLOR_BTNSHADOW);
m_clrBtnLight = ::GetSysColor(COLOR_3DLIGHT);
if( m_clrBtnFace == m_clrBtnLight )
m_clrBtnLight = m_clrBtnHilight;
}
void CToolBarEx::OnNcCalcSize(BOOL bCalcValidRects, NCCALCSIZE_PARAMS FAR* lpncsp)
{
CToolBar::OnNcCalcSize(bCalcValidRects, lpncsp);
// adjust non-client area for gripper at left or top
if( m_dwStyle & CBRS_ORIENT_HORZ )
{
lpncsp->rgrc[0].left += 4;
lpncsp->rgrc[0].right += 4;
}
else
{
lpncsp->rgrc[0].top += 6;
lpncsp->rgrc[0].bottom += 6;
}
}
void CToolBarEx::OnMouseMove(UINT nFlags, CPoint point)
{
if( m_bFlatLook )
{
register const int nBtn = GetToolBarCtrl().GetButtonCount();
const int nLastBtn = m_nLastBtn;
m_nLastBtn = -1;
for( register int i = 0 ; i < nBtn ; ++i )
{
CRect rc;
GetItemRect(i, rc);
const BOOL bBtnEnabled = GetToolBarCtrl().IsButtonEnabled(int(GetItemID(i)));
const BOOL bSep = GetButtonStyle(i) & TBBS_SEPARATOR;
if( bSep || ! bBtnEnabled )
continue;
const BOOL bHasCursor = rc.PtInRect(point);
if( bHasCursor && bBtnEnabled )
{
if( nLastBtn != i )
{
// force a repaint of the button with the cursor on it
InvalidateRect(rc, FALSE);
}
m_nLastBtn = i;
}
else
{
if( !bHasCursor && i == nLastBtn )
{
// force a repaint of the last formed button
InvalidateRect(rc, FALSE);
}
}
}
// One problem occures with WM_MOUSEMOVE: we cannot detect
// that the mouse leaves the window. If the mouse moves quick
// enough, then the last formed button stays visible. To
// resolve this problem, we set a timer and check, wether
// the mouse is outside the window ...
KillTimer(m_uTimerEvent);
m_uTimerEvent = SetTimer(1, 250, 0);
}
CToolBar::OnMouseMove(nFlags, point);
}
void CToolBarEx::OnNcPaint()
{
if( m_bFlatLook )
{
CToolBar::EraseNonClient();
CWindowDC dc(this);
DrawGripper(dc);
}
else
CToolBar::OnNcPaint();
}
void CToolBarEx::OnTimer(UINT nIDEvent)
{
if( nIDEvent == m_uTimerEvent && m_nLastBtn >= 0 )
{
POINT pt;
::GetCursorPos(&pt);
CRect rc;
GetWindowRect(rc);
if( ! rc.PtInRect(pt) )
{
GetItemRect(m_nLastBtn, rc);
InvalidateRect(rc, FALSE);
m_nLastBtn = -1;
KillTimer(nIDEvent);
}
}
else
CToolBar::OnTimer(nIDEvent);
}
int CToolBarEx::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CToolBar::OnCreate(lpCreateStruct) == -1)
return -1;
// Save the parent at creation time. It may change, if
// the toolbar is floating; but we want to know of the
// "real" parent (for notification messages)!
m_hwndParent = lpCreateStruct->hwndParent;
return 0;
}
#define PADWIDTH(x) (((x)*8+31)&~31)/8
HIMAGELIST CToolBarEx :: GetImageList()
{
m_bDeleteImgList = FALSE;
HIMAGELIST hImg = 0;
#ifdef TB_GETIMAGELIST
// Some older versions of VC++ do not know of
// the TB_GETIMAGELIST macro (defined in commctrl.h).
hImg = HIMAGELIST(SendMessage(TB_GETIMAGELIST));
#ifdef _DEBUG
if( hImg == 0 )
{
TRACE0("CToolBarEx::OnPaint(): could not get image list\n");
}
#endif
#endif // TB_GETIMAGELIST
if( ! hImg )
{
// comctl32.dll version prior to 4.70 doesn't know
// anything of the TB_GETIMAGELIST message
if( m_hbmImageWell != 0 )
{
// Yup - we have a valid image.
// But beware: Do not use this bitmap directly.
// We make the copy by ourself. CopyImage() (for
// instace) produces inacceptable copies under
// some circumstances ...
CImageList imglist;
CBitmap bmp;
// retrieve the size of the bitmap
BITMAP bmHdr;
::GetObject(m_hbmImageWell, sizeof(BITMAP), &bmHdr);
DWORD dwWidth, dwHeight = bmHdr.bmHeight;
if (bmHdr.bmBitsPixel > 8)
dwWidth = PADWIDTH(bmHdr.bmWidth * 3);
else
dwWidth = PADWIDTH(bmHdr.bmWidth);
// copy the bitmap
CClientDC cdc(this);
CDC dc1, dc2;
dc1.CreateCompatibleDC(&cdc);
dc2.CreateCompatibleDC(&cdc);
bmp.CreateCompatibleBitmap(&cdc, dwWidth, dwHeight);
CBitmap * pOBmp = dc1.SelectObject(&bmp);
HGDIOBJ hOObj = ::SelectObject(dc2.GetSafeHdc(), m_hbmImageWell);
dc1.BitBlt(0,0,dwWidth,dwHeight,&dc2,0,0,SRCCOPY);
::SelectObject(dc2.GetSafeHdc(), hOObj);
dc1.SelectObject(pOBmp);
dc1.DeleteDC();
dc2.DeleteDC();
imglist.Create(m_sizeImage.cx, m_sizeImage.cy,TRUE,dwWidth/m_sizeImage.cx,1);
imglist.SetBkColor(m_clrBtnFace);
imglist.Add(&bmp,m_clrBtnFace);
hImg = imglist.Detach();
bmp.DeleteObject();
m_bDeleteImgList = TRUE;
}
}
return hImg;
}
/////////////////////////////////////////////////////////////////////////////
// Copyright (C) 1998 by Kirk Stowell
// All rights reserved
//
// BOOL CreateCombo( DWORD dwStyle, CComboBox *pComboBox, UINT nID, int nWidth, int nHeight )
//
// Return Value - Nonzero if successful; otherwise 0.
//
// Parameters
//
// dwStyle Specifies the combo box control's style. Apply any combination
// of combo box styles to the combo box.
//
// pComboBox Specifies the combo box object name.
//
// nID Specifies the combo box control's ID.
//
// nWidth Specifies width in pixels of the combo box.
//
// nHeigth Specifies height in pixels of the combo box.
//
/////////////////////////////////////////////////////////////////////////////
BOOL CToolBarEx::CreateCombo( DWORD dwStyle, CComboBox *pComboBox, UINT nID,
int nWidth, int nHeight )
{
// set the size of combo-control
CRect pRect(-nWidth, -nHeight, 0, 0);
pRect.right -= 2;
// make the button, that is selected to be the combo-control,
// a separator and resize that separator
ASSERT(CommandToIndex(nID) >= 0); // make sure the id is valid
SetButtonInfo( CommandToIndex( nID ), nID, TBBS_SEPARATOR, nWidth );
// create the combo-control itself, reposition it in the
// client-area and show it
if (!pComboBox->Create( dwStyle, pRect, this, nID ))
{
TRACE("Failed to create the combo-box %p .\n", nID);
return FALSE;
}
GetItemRect( CommandToIndex(nID), &pRect );
pRect.left += 2;
pRect.right = pRect.left + nWidth;
pComboBox->SetWindowPos(0, pRect.left, pRect.top, 0, 0,
SWP_NOZORDER | SWP_NOACTIVATE | SWP_NOSIZE | SWP_NOCOPYBITS );
//-----------------------------------------------
CFont font;
//font高13,这也是ComboBox的高度
font.CreateFont(13,0,0,0,100,FALSE,FALSE,
0,ANSI_CHARSET,OUT_DEFAULT_PRECIS,
CLIP_DEFAULT_PRECIS,DEFAULT_QUALITY,
FF_SWISS,"Arial");
//-----------------------------------------------
pComboBox->SetFont( &font );
pComboBox->ShowWindow( SW_SHOW );
return TRUE;
}
/////////////////////////////////////////////////////////////////////////////
// Copyright (C) 1998 by Kirk Stowell
// All rights reserved
//
// BOOL CreateButton( LPCTSTR lpCaption, DWORD dwStyle, CButton *pButton, UINT nID, int nWidth, int nHeight )
//
// Return Value - Nonzero if successful; otherwise 0.
//
// Parameters
//
// lpCaption Specifies the button control's text.
//
// dwStyle Specifies the button control's style. Apply any combination
// of button styles to the combo box.
//
// pButton Specifies the button object name.
//
// nID Specifies the button control's ID.
//
// nWidth Specifies width in pixels of the button.
//
// nHeigth Specifies height in pixels of the button.
//
/////////////////////////////////////////////////////////////////////////////
BOOL CToolBarEx::CreateButton( LPCTSTR lpCaption, DWORD dwStyle, CButton *pButton, UINT nID,
int nWidth, int nHeight )
{
// set the size of check-control
CRect pRect(-nWidth, -nHeight, 0, 0);
// make the button, that is selected to be the check-control,
// a separator and resize that separator
ASSERT(CommandToIndex(nID) >= 0); // make sure the id is valid
SetButtonInfo( CommandToIndex( nID ), nID, TBBS_SEPARATOR, nWidth );
// create the check-control itself, reposition it in the
// client-area and show it
if (!pButton->Create( lpCaption, dwStyle, pRect, this, nID ))
{
TRACE("Failed to create the check-box %p .\n", nID);
return FALSE;
}
GetItemRect( CommandToIndex(nID), &pRect );
pRect.left += 2;
pRect.right = pRect.left + nWidth;
pButton->SetWindowPos(0, pRect.left, pRect.top, 0, 0,
SWP_NOZORDER | SWP_NOACTIVATE | SWP_NOSIZE | SWP_NOCOPYBITS );
pButton->SetFont( &m_GuiFont );
pButton->ShowWindow( SW_SHOW );
return TRUE;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -