📄 toolbarex.cpp
字号:
if( bBtnDown ) {
// draw a pressed button
dc.Draw3dRect(rc, m_clrBtnShadow, m_clrBtnHilight);
if( ! bHasCursor ) {
// draw an invisible frame around the hilighted area
CRect rcCheck = rc;
rcCheck.DeflateRect(1,1);
dc.Draw3dRect(rcCheck, m_clrBtnFace, m_clrBtnFace);
// On all systems I've tested was COLOR_BTNFACE == COLOR_3DLIGHT ...
if( m_clrBtnFace == m_clrBtnLight ) {
// merge m_clrBtnLight with m_clrBtnHilight
for( register int i = 2; i < rc.Width()-2; ++i )
for( register int j = 2; j < rc.Height()-2; ++j )
if( (i+j) % 2 == 0 )
dc.SetPixel(rc.TopLeft().x + i,
rc.TopLeft().y + j,
m_clrBtnHilight); }
}
} else if( bHasCursor && ! bBtnDown && bBtnEnabled )
// draw a normal button
dc.Draw3dRect(rc, m_clrBtnHilight, m_clrBtnShadow);
else if( ! bBtnDown && bBtnEnabled )
// Draw an invisible rect around the button.
// This prevents us from erasing the background
// if the button was formed before
// (that would cause the button to flicker ...)
dc.Draw3dRect(rc, m_clrBtnFace, m_clrBtnFace);
dc.SetBkColor(clrBk);
// the point where to start with the image
CPoint pt(rc.left + m_sizeOffset.cx + bBtnDown,
rc.top + m_sizeOffset.cy + bBtnDown);
imglist.Draw(&dc, nBitmap, pt, ILD_TRANSPARENT);
CString strText = GetButtonText(i);
if( strText.GetLength() ) {
CRect rectText(
rc.left+3+bBtnDown,
rc.top+m_sizeOffset.cy+m_sizeImage.cy+1+bBtnDown,
rc.right-3+bBtnDown,
rc.bottom-3+bBtnDown
);
int nBkMode = dc.SetBkMode(TRANSPARENT) ;
dc.DrawText(strText, rectText, DT_CENTER|DT_VCENTER|DT_NOCLIP);
dc.SetBkMode(nBkMode) ;
}
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
InvalidateButton(nIdx);
}
} 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);
}
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_bDragging ) {
DragMove();
return;
}
if( m_bFlatLook && IsTopParentActive() && GetTopLevelParent()->IsWindowEnabled()) {
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 ) {
#ifdef _MEMDC_H_
// You're using Keith Rule's CMemDC. In this case
// we have to do most of the things, normally done
// in CControlBar::EraseNonClient(), by ourself.
// Especially we have to make sure that WM_ERASEBKGND
// will not be sent.
// get window DC that is clipped to the non-client area
CWindowDC dc(this);
CRect rectClient;
GetClientRect(rectClient);
CRect rectWindow;
GetWindowRect(rectWindow);
ScreenToClient(rectWindow);
rectClient.OffsetRect(-rectWindow.left, -rectWindow.top);
dc.ExcludeClipRect(rectClient);
// draw borders in non-client area
rectWindow.OffsetRect(-rectWindow.left, -rectWindow.top);
DrawBorders(&dc, rectWindow);
dc.FillSolidRect(rectWindow, m_clrBtnFace);
#else
CToolBar::EraseNonClient();
CWindowDC dc(this);
#endif
DrawGripper(dc);
} else
CToolBar::OnNcPaint();
}
void CToolBarEx::OnTimer(UINT nIDEvent)
{
if( nIDEvent == m_uTimerEvent ) {
if( m_nLastBtn >= 0 ) {
POINT pt;
::GetCursorPos(&pt);
CRect rc;
GetWindowRect(rc);
if( ! rc.PtInRect(pt) ) {
InvalidateButton(m_nLastBtn);
m_nLastBtn = -1;
}
}
if( m_nLastBtn < 0 )
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;
}
void CToolBarEx::OnWindowPosChanging(WINDOWPOS FAR* lpwndpos)
{
CToolBar::OnWindowPosChanging(lpwndpos);
// If moved just force a redraw. This stops the partial redraw bug.
if( !(lpwndpos->flags & SWP_NOMOVE) )
Invalidate(FALSE);
}
#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::GetImageList(): 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 ) {
// Yep - 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;
}
void CToolBarEx::InvalidateButton(int nIndex) {
if( nIndex < 0 || nIndex >= GetToolBarCtrl().GetButtonCount() )
return;
CRect rc;
GetItemRect(nIndex, rc);
InvalidateRect(rc, FALSE);
}
BOOL CToolBarEx :: IsSeparator(int nIndex) const {
if( nIndex >= 0 && nIndex < GetToolBarCtrl().GetButtonCount() )
if( GetButtonStyle(nIndex) == TBBS_SEPARATOR ) {
// make sure this is a "real" separator
CRect rc;
GetItemRect(nIndex, rc);
if( rc.Width() <= 8 )
return TRUE;
}
return FALSE;
}
BOOL CToolBarEx :: IsControl(int nIndex) const {
if( nIndex >= 0 && nIndex < GetToolBarCtrl().GetButtonCount() )
if( GetButtonStyle(nIndex) == TBBS_SEPARATOR ) {
// make sure this is a placeholder for a control
CRect rc;
GetItemRect(nIndex, rc);
if( rc.Width() > 8 )
return TRUE;
}
return FALSE;
}
CWnd * CToolBarEx :: GetControl(int idx, BOOL bIdxIsID) const {
UINT uID = bIdxIsID ? UINT(idx) : GetItemID(idx);
for( CWnd * pWnd = GetWindow(GW_CHILD); pWnd; pWnd = pWnd->GetNextWindow() )
if( UINT(::GetWindowLong(pWnd->GetSafeHwnd(), GWL_ID)) == uID )
return pWnd;
return 0;
}
CWnd * CToolBarEx :: CtrlReplace(
CRuntimeClass * pClass,
CRect & rc,
UINT ID,
DWORD dwStyle ) {
int nIndex = CommandToIndex(ID);
if(nIndex < 0) {
TRACE1("CToolBarEx::CtrlReplace(): 0x%x is not a valid ID for this toolbar.\n,", ID);
return 0;
}
int nWidth = rc.Width();
if( nWidth < 0 )
nWidth = -nWidth;
SetButtonInfo(nIndex, ID, TBBS_SEPARATOR, nWidth);
CWnd * pCtrl = CreateControl(pClass, rc, ID, dwStyle);
return pCtrl;
}
CWnd * CToolBarEx :: CtrlInsert(
CRuntimeClass * pClass,
CRect & rc,
UINT ID,
int nBefore,
DWORD dwStyle) {
BOOL bAppend = FALSE;
CToolBarCtrl & wndToolBarCtrl = GetToolBarCtrl();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -