📄 bcgpcaptionbar.cpp
字号:
// BCGPCaptionBar.cpp: implementation of the CBCGPCaptionBar class.
//
// This is a part of the BCGPro Library
// Copyright (C) 1998-2001 BCGSoft Ltd.
// All rights reserved.
//
// This source code can be used, distributed or modified
// only under terms and conditions
// of the accompanying license agreement.
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "BCGPCaptionBar.h"
#include "BCGPVisualManager.h"
#include "BCGPToolBar.h"
#include "trackmouse.h"
#include "BCGPFrameWnd.h"
#include "BCGPMDIFrameWnd.h"
#include "BCGPOleIPFrameWnd.h"
#include "BCGPOleDocIPFrameWnd.h"
#include "BCGPMDIChildWnd.h"
#include "BCGPGlobalUtils.h"
const int nMenuArrowWidth = 10;
IMPLEMENT_DYNCREATE(CBCGPCaptionBar, CBCGPControlBar)
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CBCGPCaptionBar::CBCGPCaptionBar()
{
m_clrBarText = (COLORREF)-1;
m_clrBarBackground = (COLORREF)-1;
m_clrBarBorder = (COLORREF)-1;
m_clrTransparent = (COLORREF)-1;
m_nBorderSize = 4;
m_nMargin = 4;
m_nHorzElementOffset = 4;
m_hIcon = NULL;
m_hFont = NULL;
m_nDefaultHeight = -1;
m_nCurrentHeight = 0;
m_btnAlignnment = ALIGN_LEFT;
m_iconAlignment = ALIGN_LEFT;
m_textAlignment = ALIGN_LEFT;
m_bStretchImage = FALSE;
m_bFlatBorder = FALSE;
m_uiBtnID = 0;
m_bIsBtnPressed = FALSE;
m_bIsBtnForcePressed = FALSE;
m_bIsBtnHighlighted = FALSE;
m_bTracked = FALSE;
m_bBtnEnabled = TRUE;
m_rectImage.SetRectEmpty ();
m_rectText.SetRectEmpty ();
m_rectDrawText.SetRectEmpty ();
m_rectButton.SetRectEmpty ();
}
//*****************************************************************************
CBCGPCaptionBar::~CBCGPCaptionBar()
{
}
//*****************************************************************************
BOOL CBCGPCaptionBar::Create (DWORD dwStyle, CWnd* pParentWnd, UINT uID, int nHeight)
{
SetBarStyle (CBRS_ALIGN_TOP);
m_nDefaultHeight = nHeight;
if (!CBCGPControlBar::Create (NULL, dwStyle, CRect (0, 0, 0, 0),
pParentWnd, uID, 0))
{
return FALSE;
}
// register with parent frames' dock manager!!!
if (pParentWnd->IsKindOf (RUNTIME_CLASS (CBCGPFrameWnd)))
{
((CBCGPFrameWnd*) pParentWnd)->AddControlBar (this);
}
else if (pParentWnd->IsKindOf (RUNTIME_CLASS (CBCGPMDIFrameWnd)))
{
((CBCGPMDIFrameWnd*) pParentWnd)->AddControlBar (this);
}
else if (pParentWnd->IsKindOf (RUNTIME_CLASS (CBCGPOleIPFrameWnd)))
{
((CBCGPOleIPFrameWnd*) pParentWnd)->AddControlBar (this);
}
else if (pParentWnd->IsKindOf (RUNTIME_CLASS (CBCGPOleDocIPFrameWnd)))
{
((CBCGPOleIPFrameWnd*) pParentWnd)->AddControlBar (this);
}
else if (pParentWnd->IsKindOf (RUNTIME_CLASS (CBCGPMDIChildWnd)))
{
((CBCGPMDIChildWnd*) pParentWnd)->AddControlBar (this);
}
else if (pParentWnd->IsKindOf (RUNTIME_CLASS (CDialog)))
{
globalUtils.m_bDialogApp = TRUE;
}
else
{
ASSERT (FALSE);
}
return TRUE;
}
BEGIN_MESSAGE_MAP(CBCGPCaptionBar, CBCGPControlBar)
//{{AFX_MSG_MAP(CBCGPCaptionBar)
ON_WM_CREATE()
ON_WM_PAINT()
ON_WM_ERASEBKGND()
ON_WM_SIZE()
ON_WM_SYSCOLORCHANGE()
ON_WM_NCCALCSIZE()
ON_WM_NCPAINT()
ON_WM_LBUTTONDOWN()
ON_WM_LBUTTONUP()
ON_WM_MOUSEMOVE()
ON_WM_RBUTTONUP()
//}}AFX_MSG_MAP
ON_MESSAGE(WM_SETFONT, OnSetFont)
ON_MESSAGE(WM_GETFONT, OnGetFont)
ON_MESSAGE(WM_MOUSELEAVE, OnMouseLeave)
END_MESSAGE_MAP()
void CBCGPCaptionBar::OnUpdateCmdUI(CFrameWnd* /*pTarget*/, BOOL /*bDisableIfNoHndler*/)
{
}
//*****************************************************************************
int CBCGPCaptionBar::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CBCGPControlBar::OnCreate (lpCreateStruct) == -1)
return -1;
return 0;
}
//*****************************************************************************
void CBCGPCaptionBar::OnSize(UINT nType, int cx, int cy)
{
CBCGPControlBar::OnSize(nType, cx, cy);
RecalcLayout ();
InvalidateRect (NULL);
}
//*****************************************************************************
void CBCGPCaptionBar::OnNcCalcSize(BOOL /*bCalcValidRects*/, NCCALCSIZE_PARAMS FAR* lpncsp)
{
lpncsp->rgrc[0].bottom -= m_nBorderSize;
lpncsp->rgrc[0].top += m_nBorderSize;
}
//*****************************************************************************
void CBCGPCaptionBar::OnPaint()
{
CPaintDC dc(this);
int nOldBkMode = dc.SetBkMode (TRANSPARENT);
COLORREF clrOldText =
dc.SetTextColor (m_clrBarText != (COLORREF) -1 ? m_clrBarText :
CBCGPVisualManager::GetInstance ()->GetCaptionBarTextColor (this));
CFont* pOldFont = dc.SelectObject (
m_hFont == NULL ? &globalData.fontRegular : CFont::FromHandle (m_hFont));
OnDrawButton (&dc, m_rectButton, m_strBtnText, m_bBtnEnabled);
OnDrawText (&dc, m_rectDrawText, m_strText);
OnDrawImage (&dc, m_rectImage);
dc.SelectObject (pOldFont);
dc.SetTextColor (clrOldText);
dc.SetBkMode (nOldBkMode);
}
//*****************************************************************************
void CBCGPCaptionBar::OnNcPaint()
{
CWindowDC dcWin (this);
CRect rectClient;
GetClientRect(rectClient);
CRect rectWindow;
GetWindowRect(rectWindow);
CRect rectBorder = rectWindow;
ScreenToClient(rectWindow);
rectClient.OffsetRect(-rectWindow.left, -rectWindow.top);
dcWin.ExcludeClipRect (rectClient);
rectBorder.OffsetRect(-rectBorder.left, -rectBorder.top);
int nTop = rectBorder.top;
rectBorder.top = rectBorder.bottom - m_nBorderSize;
OnDrawBorder (&dcWin, rectBorder);
rectBorder.top = nTop;
rectBorder.bottom = rectBorder.top + m_nBorderSize;
OnDrawBorder (&dcWin, rectBorder);
dcWin.SelectClipRgn (NULL);
}
//*****************************************************************************
void CBCGPCaptionBar::OnDrawBackground (CDC* pDC, CRect rect)
{
ASSERT_VALID (pDC);
CBCGPVisualManager::GetInstance ()->OnFillBarBackground (pDC, this,
rect, rect);
}
//*****************************************************************************
void CBCGPCaptionBar::OnDrawBorder (CDC* pDC, CRect rect)
{
ASSERT_VALID (pDC);
rect.InflateRect (2, 0);
CBCGPVisualManager::GetInstance ()->OnDrawCaptionBarBorder (
pDC, this, rect, m_clrBarBorder, m_bFlatBorder);
}
//*****************************************************************************
void CBCGPCaptionBar::OnDrawButton (CDC* pDC, CRect rect,
const CString& strButton, BOOL bEnabled)
{
ASSERT_VALID (pDC);
CRect rectText = rect;
rectText.DeflateRect (m_nHorzElementOffset, 0);
if (m_uiBtnID != 0 && bEnabled)
{
rectText.right -= nMenuArrowWidth;
}
pDC->DrawText (strButton, rectText,
DT_END_ELLIPSIS | DT_SINGLELINE | DT_VCENTER);
if (m_uiBtnID != 0 && bEnabled)
{
if (m_uiBtnID != 0)
{
// Draw menu triangle:
CRect rectArrow = rect;
rectArrow.bottom -= m_nMargin;
rectArrow.top = rectArrow.bottom - nMenuArrowWidth;
rectArrow.left = rectText.right;
int iXMiddle = rectArrow.left + rectArrow.Width () / 2;
rectArrow.DeflateRect (0, rectArrow.Height () / 3);
rectArrow.DeflateRect (rectArrow.Height () / 3, rectArrow.Height () / 3);
rectArrow.left = iXMiddle - rectArrow.Height () - 1;
rectArrow.right = iXMiddle + rectArrow.Height () + 1;
int iHalfWidth = (rectArrow.Width () % 2 != 0) ?
(rectArrow.Width () - 1) / 2 :
rectArrow.Width () / 2;
CPoint pts [3];
pts[0].x = rectArrow.left;
pts[0].y = rectArrow.top;
pts[1].x = rectArrow.right;
pts[1].y = rectArrow.top;
pts[2].x = rectArrow.left + iHalfWidth;
pts[2].y = rectArrow.bottom + 1;
CBrush brArrow (pDC->GetTextColor ());
CPen* pOldPen = (CPen*) pDC->SelectStockObject (NULL_PEN);
CBrush* pOldBrush = (CBrush*) pDC->SelectObject(&brArrow);
pDC->SetPolyFillMode (WINDING);
pDC->Polygon (pts, 3);
pDC->SelectObject (pOldBrush);
pDC->SelectObject (pOldPen);
}
if (m_bIsBtnPressed || m_bIsBtnForcePressed)
{
pDC->Draw3dRect (rect, globalData.clrBarDkShadow, globalData.clrBarHilite);
}
else if (m_bIsBtnHighlighted)
{
pDC->Draw3dRect (rect, globalData.clrBarHilite, globalData.clrBarDkShadow);
}
}
}
//*****************************************************************************
void CBCGPCaptionBar::OnDrawText (CDC* pDC, CRect rect, const CString& strText)
{
ASSERT_VALID (pDC);
pDC->DrawText (strText, rect,
DT_END_ELLIPSIS | DT_SINGLELINE | DT_VCENTER);
}
//*****************************************************************************
void CBCGPCaptionBar::OnDrawImage (CDC* pDC, CRect rect)
{
ASSERT_VALID (pDC);
if (m_hIcon != NULL)
{
DrawIconEx (pDC->GetSafeHdc(), rect.left, rect.top, m_hIcon,
rect.Width (), rect.Height (), NULL, (HBRUSH)NULL,
DI_NORMAL) ;
}
else if (m_Bitmap.GetCount () > 0)
{
CSize sizeDest;
if (m_bStretchImage)
{
sizeDest = rect.Size ();
}
else
{
sizeDest = m_rectImage.Size ();
}
CBCGPDrawState ds;
m_Bitmap.PrepareDrawImage (ds, sizeDest);
m_Bitmap.Draw (pDC, rect.left, rect.top, 0);
m_Bitmap.EndDrawImage (ds);
}
}
//*****************************************************************************
void CBCGPCaptionBar::OnSysColorChange()
{
}
//*****************************************************************************
CSize CBCGPCaptionBar::CalcFixedLayout(BOOL /*bStretch*/, BOOL /*bHorz*/)
{
RecalcLayout ();
return CSize (32767, m_nCurrentHeight);
}
//*****************************************************************************
void CBCGPCaptionBar::SetButton (LPCTSTR lpszLabel, UINT uiCmdUI, BarElementAlignment btnAlignmnet)
{
ASSERT (lpszLabel != NULL);
m_strBtnText = lpszLabel;
m_uiBtnID = uiCmdUI;
m_btnAlignnment = btnAlignmnet;
AdjustLayout ();
}
//*****************************************************************************
void CBCGPCaptionBar::EnableButton (BOOL bEnable)
{
m_bBtnEnabled = bEnable;
if (GetSafeHwnd () != NULL)
{
CRect rectButton = m_rectButton;
RecalcLayout ();
InvalidateRect (rectButton);
InvalidateRect (m_rectButton);
UpdateWindow ();
}
}
//*****************************************************************************
void CBCGPCaptionBar::SetButtonPressed (BOOL bPresed)
{
m_bIsBtnForcePressed = bPresed;
if (GetSafeHwnd () != NULL)
{
InvalidateRect (m_rectButton);
UpdateWindow ();
}
}
//*****************************************************************************
void CBCGPCaptionBar::RemoveButton ()
{
m_strBtnText.Empty ();
AdjustLayout ();
}
//*****************************************************************************
void CBCGPCaptionBar::SetIcon (HICON hIcon, BarElementAlignment iconAlignment)
{
m_Bitmap.Clear ();
m_hIcon = hIcon;
m_iconAlignment = iconAlignment;
AdjustLayout ();
}
//*****************************************************************************
void CBCGPCaptionBar::RemoveIcon ()
{
m_hIcon = NULL;
AdjustLayout ();
}
//*****************************************************************************
void CBCGPCaptionBar::SetBitmap (HBITMAP hBitmap, COLORREF clrTransparent,
BOOL bStretch, BarElementAlignment bmpAlignment)
{
ASSERT (hBitmap != NULL);
m_hIcon = NULL;
m_Bitmap.Clear ();
BITMAP bmp;
::GetObject (hBitmap, sizeof (BITMAP), (LPVOID) &bmp);
m_Bitmap.SetImageSize (CSize (bmp.bmWidth, bmp.bmHeight));
m_clrTransparent = clrTransparent;
m_Bitmap.SetTransparentColor (m_clrTransparent);
m_Bitmap.AddImage (hBitmap);
m_bStretchImage = bStretch;
m_iconAlignment = bmpAlignment;
AdjustLayout ();
}
//*****************************************************************************
void CBCGPCaptionBar::RemoveBitmap ()
{
m_Bitmap.Clear ();
AdjustLayout ();
}
//*****************************************************************************
void CBCGPCaptionBar::SetText (const CString& strText, BarElementAlignment textAlignment)
{
m_strText = strText;
m_textAlignment = textAlignment;
AdjustLayout ();
}
//*****************************************************************************
void CBCGPCaptionBar::RemoveText ()
{
m_strText.Empty ();
AdjustLayout ();
}
//*****************************************************************************
afx_msg LRESULT CBCGPCaptionBar::OnSetFont (WPARAM wParam, LPARAM /*lParam*/)
{
m_hFont = (HFONT) wParam;
AdjustLayout ();
return 0;
}
//*****************************************************************************
afx_msg LRESULT CBCGPCaptionBar::OnGetFont (WPARAM, LPARAM)
{
return (LRESULT) m_hFont;
}
//*****************************************************************************
CBCGPCaptionBar::BarElementAlignment CBCGPCaptionBar::GetAlignment (BarElement elem)
{
switch (elem)
{
case ELEM_BUTTON:
return m_btnAlignnment;
case ELEM_TEXT:
return m_textAlignment;
case ELEM_ICON:
return m_iconAlignment;
}
ASSERT (FALSE);
return ALIGN_INVALID;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -