?? flatbar.cpp
字號:
#include "stdafx.h"
#include "flatbar.h"
#include "resource.h"
#ifdef _DEBUG
#undef THIS_FILE
#define new DEBUG_NEW
static char BASED_CODE THIS_FILE[] = __FILE__;
#endif
#ifndef TBSTYLE_FLAT
#define TBSTYLE_FLAT 0x0800 // (in case you don't have the new commctrl.h)
#endif
#ifndef TB_GETIMAGELIST
#define TB_GETIMAGELIST (WM_USER + 49)
#endif // TB_GETIMAGELIST
#ifndef TB_SETINDENT
#define TB_SETINDENT (WM_USER + 47)
#endif
BEGIN_MESSAGE_MAP(CFlatToolBar, CToolBar)
ON_WM_CONTEXTMENU()
//{{AFX_MSG_MAP(CFlatToolBar)
ON_WM_WINDOWPOSCHANGING()
ON_WM_CREATE()
ON_WM_PAINT()
ON_WM_NCPAINT()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
IMPLEMENT_DYNAMIC(CFlatToolBar,CToolBar)
BOOL CFlatToolBar::IsAvailable()
{
return (SendMessage(TB_GETIMAGELIST) != 0);
}
void CFlatToolBar::ShowFlat(BOOL bFlat)
{
if (bFlat)
{
BOOL bAvailable = (SendMessage(TB_GETIMAGELIST) != 0);
if (bAvailable)
{
m_bFlat = TRUE;
ModifyStyle(0, TBSTYLE_FLAT); // make it flat
EraseNonClient();
Invalidate();
}
}
else
{
m_bFlat = FALSE;
ModifyStyle(TBSTYLE_FLAT, 0); // revert to usual look
EraseNonClient();
Invalidate();
}
}
// Because buttons are transparent, we need to repaint the background
void CFlatToolBar::RepaintBackground()
{
CRect rc; GetWindowRect(&rc); // get rect for toolbar
CWnd* pParent = GetParent(); // get parent windows
pParent->ScreenToClient(&rc); // convert to parent coords
pParent->InvalidateRect(&rc); // paint rectangle underneath
}
// Because buttons are transparaent, we need to repaint background if style changes
void CFlatToolBar::OnUpdateCmdUI(CFrameWnd* pTarget, BOOL bDisableIfNoHndler)
{
static CUIntArray styles;
// save styles
int nIndexMax = (int)DefWindowProc(TB_BUTTONCOUNT, 0, 0);
int nIndex;
for (nIndex = 0; nIndex < nIndexMax; nIndex++)
{
UINT dwStyle = GetButtonStyle(nIndex);
styles.SetAtGrow(nIndex,dwStyle);
}
// default processing
CToolBar::OnUpdateCmdUI(pTarget,bDisableIfNoHndler);
// make checked button appear pushed in
for (nIndex = 0; nIndex < nIndexMax; nIndex++)
{
UINT dwStyle = GetButtonStyle(nIndex);
if (dwStyle & TBBS_DISABLED)
{
// don't touch if disabled (avoids flicker)
}
else if (dwStyle & TBBS_CHECKBOX)
{
UINT dwStyleWas = dwStyle;
// if checked, make it pressed, else not pressed
if (dwStyle & TBBS_CHECKED)
{
dwStyle |= TBBS_PRESSED;
}
else if (!(styles[nIndex]&TBBS_CHECKED) && (styles[nIndex]&TBBS_PRESSED))
{
dwStyle |= TBBS_PRESSED;
}
else
{
dwStyle &= ~TBBS_PRESSED;
}
// set new style if changed
if (dwStyleWas != dwStyle)
{
SetButtonStyle(nIndex,dwStyle);
}
}
}
// check for changes to style (buttons presssed/released)
for (nIndex = 0; nIndex < nIndexMax; nIndex++)
{
UINT dwStyle = GetButtonStyle(nIndex);
if (styles[nIndex] != dwStyle)
{
//RepaintBackground(); // need to take care of button background
//Invalidate(); // repaint toolbar (not just this button)
//PJ: Optimized code above to only redraw the button
CRect rect;
if (DefWindowProc(TB_GETITEMRECT, nIndex, (LPARAM)&rect))
InvalidateRect(rect, TRUE); // + erase the background
break;
}
}
}
// Because buttons are transparent, we need to repaint background on size or move
void CFlatToolBar::OnWindowPosChanging(LPWINDOWPOS lpwp)
{
// default processing
CToolBar::OnWindowPosChanging(lpwp);
// repaint background if size or move
RepaintBackground();
PostMessage(WM_NCPAINT);
}
int CFlatToolBar::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CToolBar::OnCreate(lpCreateStruct) == -1)
return -1;
//Make the toolbar flatstyle
ModifyStyle(0, TBSTYLE_FLAT);
return 0;
}
// Draw the separators in the client area
void CFlatToolBar::DrawSeparators()
{
CClientDC dc(this); // get a dc for the client area
DrawSeparators(&dc); // draw the separators on it
}
// Draw the separators
void CFlatToolBar::DrawSeparators(CClientDC* pDC)
{
// horizontal vs vertical
BOOL ishorz = (m_dwStyle & CBRS_ORIENT_HORZ) != 0;
// get number of buttons
int nIndexMax = (int)DefWindowProc(TB_BUTTONCOUNT, 0, 0);
int nIndex;
// try each button
for (nIndex = 0; nIndex < nIndexMax; nIndex++)
{
UINT dwStyle = GetButtonStyle(nIndex);
UINT wStyle = LOWORD(dwStyle);
// if it is a separator
if (wStyle == TBBS_SEPARATOR)
{
// get it's rectangle and width
CRect rect;
GetItemRect(nIndex,rect);
// if small enough to be a true separator
int w = rect.Width();
if (w <= 8)
{
if (ishorz)
{
// draw the separator bar in the middle
CRect rectbar = rect;
int x = (rectbar.left+rectbar.right)/2;
rectbar.left = x-1; rectbar.right = x+1;
rectbar.top += 2; rectbar.bottom -= 2;
pDC->Draw3dRect(rectbar,::GetSysColor(COLOR_3DSHADOW), ::GetSysColor(COLOR_3DHILIGHT));
}
else
{
// draw the separator bar in the middle
CRect rectbar = rect;
rectbar.left = rectbar.left - m_sizeButton.cx;
rectbar.right = rectbar.left + m_sizeButton.cx;
rectbar.top = rectbar.bottom+1;
rectbar.bottom = rectbar.top+3;
int y = (rectbar.top+rectbar.bottom)/2;
rectbar.top = y-1; rectbar.bottom = y+1;
rectbar.left += 2; rectbar.right -= 2;
pDC->Draw3dRect(rectbar,::GetSysColor(COLOR_3DSHADOW), ::GetSysColor(COLOR_3DHILIGHT));
}
}
}
}
}
// Paint the toolbar
void CFlatToolBar::OnPaint()
{
// standard toolbar
CToolBar::OnPaint();
// plus separators if required
if (m_bFlat)
DrawSeparators();
}
// Erase the non-client area (borders) - copied from MFC implementation
void CFlatToolBar::EraseNonClient()
{
// 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);
// erase parts not drawn
dc.IntersectClipRect(rectWindow);
SendMessage(WM_ERASEBKGND, (WPARAM)dc.m_hDC);
if (m_bFlat)
DrawGripper(&dc, rectWindow); // <-- my addition to draw gripper
}
// Draw the gripper at left or top
void CFlatToolBar::DrawGripper(CWindowDC *pDC, CRect& rectWindow)
{
CRect gripper = rectWindow;
gripper.DeflateRect(1,1);
if (m_dwStyle & CBRS_FLOATING)
{
// no grippers
}
else if (m_dwStyle & CBRS_ORIENT_HORZ)
{
// gripper at left
gripper.left++;
gripper.right = gripper.left+3;
gripper.top += 2; gripper.bottom -= 2;
pDC->Draw3dRect(gripper,::GetSysColor(COLOR_3DHIGHLIGHT), ::GetSysColor(COLOR_3DSHADOW));
}
else
{
// gripper at top
gripper.top++;
gripper.bottom = gripper.top+3;
gripper.left += 2; gripper.right -= 2;
pDC->Draw3dRect(gripper,::GetSysColor(COLOR_3DHIGHLIGHT), ::GetSysColor(COLOR_3DSHADOW));
}
}
// Paint the non-client area - copied from MFC implementatios
void CFlatToolBar:: OnNcPaint()
{
EraseNonClient();
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -