📄 bcgoutlookbar.cpp
字号:
//*******************************************************************************
// COPYRIGHT NOTES
// ---------------
// This source code is a part of BCGControlBar library.
// You may use, compile or redistribute it as part of your application
// for free. You cannot redistribute it as a part of a software development
// library without the agreement of the author. If the sources are
// distributed along with the application, you should leave the original
// copyright notes in the source code without any changes.
// This code can be used WITHOUT ANY WARRANTIES on your own risk.
//
// Stas Levin <stas@iet.co.il>
//*******************************************************************************
// bcgoutlookbar.cpp : implementation file
//
#include "stdafx.h"
#include "bcgoutlookbar.h"
#include "bcgbarres.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#define BORDER_SIZE 3
#define SCROLL_BUTTON_SIZE 10
#define MARGIN_SIZE 2
/////////////////////////////////////////////////////////////////////////////
// CBCGOutlookBar
IMPLEMENT_DYNAMIC(CBCGOutlookBar, CBCGToolBar)
CBCGOutlookBar::CBCGOutlookBar()
{
m_iScrollOffset = 0;
m_iFirstVisibleButton = 0;
m_bScrollDown = FALSE;
m_clrRegText = ::GetSysColor (COLOR_WINDOW);
m_clrSelText = ::GetSysColor (COLOR_WINDOWTEXT);
m_clrBackColor = ::GetSysColor (COLOR_3DSHADOW);
m_clrTransparentColor = RGB (255, 0, 255);
m_Images.SetTransparentColor (m_clrTransparentColor);
m_csImage = CSize (0, 0);
m_uiBackImageId = 0;
m_pBtnUp = new CBCGOutlookButton;
m_pBtnUp->m_nScrollBtn = -1;
m_pBtnDown = new CBCGOutlookButton;
m_pBtnDown->m_nScrollBtn = 1;
m_bDrawShadedHighlight = FALSE;
}
//*************************************************************************************
CBCGOutlookBar::~CBCGOutlookBar()
{
while (!m_Buttons.IsEmpty ())
{
delete m_Buttons.RemoveHead ();
}
}
BEGIN_MESSAGE_MAP(CBCGOutlookBar, CBCGToolBar)
//{{AFX_MSG_MAP(CBCGOutlookBar)
ON_WM_ERASEBKGND()
ON_WM_SIZE()
ON_WM_CREATE()
ON_WM_NCCALCSIZE()
ON_WM_SYSCOLORCHANGE()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CBCGOutlookBar message handlers
BOOL CBCGOutlookBar::Create(CWnd* pParentWnd, DWORD dwStyle, UINT nID)
{
// initialize common controls
VERIFY(AfxDeferRegisterClass(AFX_WNDCOMMCTLS_REG));
return CBCGToolBar::Create (pParentWnd, dwStyle | WS_CHILD | WS_VISIBLE, nID);
}
//*************************************************************************************
BOOL CBCGOutlookBar::OnEraseBkgnd(CDC* pDC)
{
CRect rectClient;
GetClientRect (rectClient);
if (m_bmpBack.GetCount () == 0)
{
pDC->FillSolidRect (rectClient, m_clrBackColor);
}
else
{
ASSERT (m_bmpBack.GetCount () == 1);
CBCGDrawState ds;
m_bmpBack.PrepareDrawImage (ds);
CSize sizeBack = m_bmpBack.GetImageSize ();
for (int x = 0; x < rectClient.Width (); x += sizeBack.cx)
{
for (int y = 0; y < rectClient.Height (); y += sizeBack.cy)
{
m_bmpBack.Draw (pDC, x, y, 0);
}
}
m_bmpBack.EndDrawImage (ds);
}
return TRUE;
}
//*************************************************************************************
BOOL CBCGOutlookBar::AddButton (UINT uiImage, UINT uiLabel, UINT iIdCommand)
{
CString strLable;
strLable.LoadString (uiLabel);
return AddButton (uiImage, strLable, iIdCommand);
}
//*************************************************************************************
BOOL CBCGOutlookBar::AddButton (UINT uiImage, LPCTSTR lpszLabel, UINT iIdCommand)
{
int iImageIndex = -1;
if (uiImage != 0)
{
CBitmap bmp;
if (!bmp.LoadBitmap (uiImage))
{
TRACE(_T("Can't load bitmap resource: %d"), uiImage);
return FALSE;
}
BITMAP bitmap;
bmp.GetBitmap (&bitmap);
CSize csImage = CSize (bitmap.bmWidth, bitmap.bmHeight);
if (m_Images.GetCount () == 0) // First image
{
m_csImage = csImage;
m_Images.SetImageSize (csImage);
}
else
{
ASSERT (m_csImage == csImage); // All buttons should be of the same size!
}
iImageIndex = m_Images.AddImage (bmp);
}
CBCGOutlookButton* pButton = new CBCGOutlookButton;
ASSERT (pButton != NULL);
pButton->m_nID = iIdCommand;
pButton->m_strText = (lpszLabel == NULL) ? _T("") : lpszLabel;
pButton->SetImage (iImageIndex);
pButton->m_bTextBelow = m_bTextLabels;
InsertButton (pButton, m_Buttons.GetCount () - 1);
AdjustLayout ();
return TRUE;
}
//**************************************************************************************
BOOL CBCGOutlookBar::RemoveButton (UINT iIdCommand)
{
for (POSITION pos = m_Buttons.GetHeadPosition (); pos != NULL;)
{
POSITION posSave = pos;
CBCGOutlookButton* pButton = (CBCGOutlookButton*) m_Buttons.GetNext (pos);
ASSERT (pButton != NULL);
if (pButton->m_nID == iIdCommand)
{
m_Buttons.RemoveAt (posSave);
delete pButton;
AdjustLayout ();
return TRUE;
}
}
return FALSE;
}
//*************************************************************************************
void CBCGOutlookBar::OnSize(UINT nType, int cx, int cy)
{
CBCGToolBar::OnSize(nType, cx, cy);
int iButtons = m_Buttons.GetCount ();
if (iButtons > 0)
{
AdjustLayout ();
POSITION posLast = m_Buttons.FindIndex (iButtons - 1);
CBCGOutlookButton* pButtonLast = (CBCGOutlookButton*) m_Buttons.GetAt (posLast);
ASSERT (pButtonLast != NULL);
while (m_iScrollOffset > 0 &&
pButtonLast->Rect ().bottom < cy)
{
ScrollUp ();
}
}
}
//*************************************************************************************
void CBCGOutlookBar::SetTextColor (COLORREF clrRegText, COLORREF clrSelText)
{
m_clrRegText = clrRegText;
m_clrSelText = clrSelText;
if (GetSafeHwnd () != NULL)
{
Invalidate ();
UpdateWindow ();
}
}
//*************************************************************************************
int CBCGOutlookBar::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CBCGToolBar::OnCreate(lpCreateStruct) == -1)
return -1;
SetBarStyle (m_dwStyle & ~(CBRS_BORDER_ANY | CBRS_GRIPPER));
m_cxLeftBorder = m_cxRightBorder = 0;
m_cyTopBorder = m_cyBottomBorder = 0;
//-------------------------------------------
// Adjust Z-order in the parent frame window:
//-------------------------------------------
BOOL bVert = (m_dwStyle & CBRS_ORIENT_HORZ) == 0;
if (bVert)
{
SetWindowPos(&wndBottom, 0,0,0,0,SWP_NOSIZE|SWP_NOMOVE);
}
else
{
CFrameWnd* pParent = GetParentFrame ();
ASSERT (pParent != NULL);
CWnd* pWndStatusBar = pParent->GetDlgItem (AFX_IDW_STATUS_BAR);
if (pWndStatusBar != NULL)
{
SetWindowPos(pWndStatusBar, 0,0,0,0,SWP_NOSIZE|SWP_NOMOVE);
}
}
InsertButton (m_pBtnUp, 0);
InsertButton (m_pBtnDown);
SetWindowText (_T("Shortcuts bar"));
return 0;
}
//*************************************************************************************
void CBCGOutlookBar::ScrollUp ()
{
ASSERT (m_iScrollOffset > 0);
ASSERT (m_iFirstVisibleButton > 0);
POSITION pos = m_Buttons.FindIndex (--m_iFirstVisibleButton);
CBCGOutlookButton* pButton = (CBCGOutlookButton*) m_Buttons.GetAt (pos);
ASSERT (pButton != NULL);
BOOL bVert = (m_dwStyle & CBRS_ORIENT_HORZ) == 0;
if (bVert)
{
m_iScrollOffset -= pButton->Rect ().Height ();
}
else
{
m_iScrollOffset -= pButton->Rect ().Width ();
}
if (m_iFirstVisibleButton == 0)
{
m_iScrollOffset = 0;
}
ASSERT (m_iScrollOffset >= 0);
AdjustLayout ();
}
//*************************************************************************************
void CBCGOutlookBar::ScrollDown ()
{
ASSERT (m_bScrollDown);
if (m_iFirstVisibleButton + 1 >= m_Buttons.GetCount ())
{
return;
}
POSITION pos = m_Buttons.FindIndex (++m_iFirstVisibleButton);
CBCGOutlookButton* pButton = (CBCGOutlookButton*) m_Buttons.GetAt (pos);
ASSERT (pButton != NULL);
BOOL bVert = (m_dwStyle & CBRS_ORIENT_HORZ) == 0;
if (bVert)
{
m_iScrollOffset += pButton->Rect ().Height ();
}
else
{
m_iScrollOffset += pButton->Rect ().Width ();
}
AdjustLayout ();
}
//*************************************************************************************
CSize CBCGOutlookBar::CalcFixedLayout (BOOL /*bStretch*/, BOOL bHorz)
{
int iMinSize = (bHorz) ?
m_csImage.cy + 2 * MARGIN_SIZE + 2 * BORDER_SIZE:
m_csImage.cx + 2 * MARGIN_SIZE + 2 * BORDER_SIZE;
if (m_bTextLabels)
{
CClientDC dc (this);
for (POSITION pos = m_Buttons.GetHeadPosition (); pos != NULL;)
{
CBCGOutlookButton* pButton = (CBCGOutlookButton*) m_Buttons.GetNext (pos);
ASSERT (pButton != NULL);
CRect rectText (0, 0, 1, 1);
dc.DrawText (pButton->m_strText, rectText, DT_CALCRECT | DT_WORDBREAK);
if (!bHorz)
{
iMinSize = max (iMinSize, rectText.Width ());
}
else
{
iMinSize = max (iMinSize, rectText.Height ());
}
}
}
CSize size = (bHorz) ? CSize (32767, iMinSize) : CSize (iMinSize, 32767);
return size;
}
//*************************************************************************************
void CBCGOutlookBar::OnNcCalcSize(BOOL /*bCalcValidRects*/, NCCALCSIZE_PARAMS FAR* lpncsp)
{
CRect rect;
rect.SetRectEmpty();
BOOL bHorz = (m_dwStyle & CBRS_ORIENT_HORZ) != 0;
CControlBar::CalcInsideRect(rect, bHorz);
// adjust non-client area for border space
lpncsp->rgrc[0].left += rect.left;
lpncsp->rgrc[0].top += rect.top;
lpncsp->rgrc[0].right += rect.right;
lpncsp->rgrc[0].bottom += rect.bottom;
}
//*************************************************************************************
void CBCGOutlookBar::SetBackImage (UINT uiImageID)
{
if (m_uiBackImageId == uiImageID)
{
return;
}
m_bDrawShadedHighlight = FALSE;
if (m_bmpBack.GetCount () > 0)
{
m_bmpBack.Clear ();
}
m_uiBackImageId = 0;
if (uiImageID != 0)
{
HBITMAP hbmp = (HBITMAP) ::LoadImage (
AfxGetResourceHandle (),
MAKEINTRESOURCE (uiImageID),
IMAGE_BITMAP,
0, 0,
LR_CREATEDIBSECTION | LR_LOADMAP3DCOLORS);
if (hbmp != NULL)
{
BITMAP bitmap;
::GetObject (hbmp, sizeof (BITMAP), (LPVOID) &bitmap);
m_bmpBack.SetImageSize (CSize (bitmap.bmWidth, bitmap.bmHeight));
m_bmpBack.AddImage (hbmp);
m_uiBackImageId = uiImageID;
}
CClientDC dc (NULL);
int nBitsPerPixel = dc.GetDeviceCaps (BITSPIXEL);
m_bDrawShadedHighlight = (nBitsPerPixel > 8); // For 16 bits or greater
}
if (GetSafeHwnd () != NULL)
{
Invalidate ();
UpdateWindow ();
}
}
//*************************************************************************************
void CBCGOutlookBar::SetBackColor (COLORREF color)
{
m_clrBackColor = color;
if (GetSafeHwnd () != NULL)
{
Invalidate ();
UpdateWindow ();
}
}
//*************************************************************************************
void CBCGOutlookBar::SetTransparentColor (COLORREF color)
{
m_clrTransparentColor = color;
if (GetSafeHwnd () != NULL)
{
m_Images.SetTransparentColor (m_clrTransparentColor);
Invalidate ();
UpdateWindow ();
}
}
//*************************************************************************************
void CBCGOutlookBar::OnSysColorChange()
{
CBCGToolBar::OnSysColorChange();
m_clrSelText = ::GetSysColor (COLOR_WINDOWTEXT);
m_clrBackColor = ::GetSysColor (COLOR_3DSHADOW);
if (m_uiBackImageId != 0)
{
int uiImage = m_uiBackImageId;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -