改变CMFCRibbonBar高度

本文作者:无幻
测试环境:Windows XP
开发环境:Visual Studio 2010

      要改变CMFCRibbonBar的高度,需要重载CalcFixedLayout函数,例如如下:

头文件:

#pragma once

//
// CKofMFCRibbonQuickAccessToolBar

class CKofMFCRibbonQuickAccessToolBar : public CMFCRibbonQuickAccessToolBar
{
	friend class CKofMFCRibbonBar;
};

//
// CKofMFCRibbonBar

class CKofMFCRibbonBar : public CMFCRibbonBar
{
	DECLARE_DYNAMIC(CKofMFCRibbonBar)

public:
	CKofMFCRibbonBar(BOOL bReplaceFrameCaption = TRUE);
	virtual ~CKofMFCRibbonBar();

protected:
	virtual CSize CalcFixedLayout(BOOL bStretch, BOOL bHorz);
	DECLARE_MESSAGE_MAP()
};
实现文件:
#include "stdafx.h"
#include "KofMFCRibbonBar.h"

static const int nXTabMargin = 14;
static const int nYTabMargin = 5;

static const int nXMargin = 2;
static const int nYMargin = 2;
//
// CKofMFCRibbonBar

IMPLEMENT_DYNAMIC(CKofMFCRibbonBar, CMFCRibbonBar)

CKofMFCRibbonBar::CKofMFCRibbonBar(BOOL bReplaceFrameCaption /*= TRUE*/)
: CMFCRibbonBar(bReplaceFrameCaption)
{
}

CKofMFCRibbonBar::~CKofMFCRibbonBar()
{
}

BEGIN_MESSAGE_MAP(CKofMFCRibbonBar, CMFCRibbonBar)
END_MESSAGE_MAP()

CSize CKofMFCRibbonBar::CalcFixedLayout( BOOL bStretch, BOOL bHorz )
{
	ASSERT_VALID(this);

	CClientDC dc(this);

	CFont* pOldFont = dc.SelectObject(GetFont());
	ENSURE(pOldFont != NULL);

	TEXTMETRIC tm;
	dc.GetTextMetrics(&tm);

	//“标题”的高度
	m_nCaptionHeight = 0;

	if (m_bReplaceFrameCaption)
	{
		m_nCaptionHeight = GetSystemMetrics(SM_CYCAPTION) + 1;

		if (afxGlobalData.DwmIsCompositionEnabled())
		{
			m_nCaptionHeight += GetSystemMetrics(SM_CYSIZEFRAME);
		}
	}

	int cy = 0;

	CSize sizeMainButton = m_sizeMainButton;
	double scale = afxGlobalData.GetRibbonImageScale();
	if (scale > 1.)
	{
		sizeMainButton.cx = (int)(.5 + scale * sizeMainButton.cx);
		sizeMainButton.cy = (int)(.5 + scale * sizeMainButton.cy);
	}

	if (m_dwHideFlags & AFX_RIBBONBAR_HIDE_ALL)
	{
		cy = m_nCaptionHeight;
	}
	else
	{
		if (m_bRecalcCategoryHeight)
		{
			m_nCategoryHeight = 0;
		}

		//“选项卡”的高度
		m_nTabsHeight = tm.tmHeight + 2 * nYTabMargin;

		if (m_bRecalcCategoryHeight)
		{
			for (int i = 0; i < m_arCategories.GetSize(); i++)
			{
				CMFCRibbonCategory* pCategory = m_arCategories [i];
				ASSERT_VALID(pCategory);

				m_nCategoryHeight = max(m_nCategoryHeight, pCategory->GetMaxHeight(&dc));
			}
			m_bRecalcCategoryHeight = FALSE;
		}

		CKofMFCRibbonQuickAccessToolBar *pQAToolbar = (CKofMFCRibbonQuickAccessToolBar *)&m_QAToolbar;
		const CSize sizeAQToolbar = pQAToolbar->GetRegularSize(&dc);

		if (IsQuickAccessToolbarOnTop())
		{
			m_nCaptionHeight = max(m_nCaptionHeight, sizeAQToolbar.cy + (IsWindows7Look() ? 0 : (2 * nYMargin)));
		}

		//“快速访问工具栏”的高度
		const int nQuickAceesToolbarHeight = IsQuickAccessToolbarOnTop() ? 0 : sizeAQToolbar.cy;
		//“分类”的高度
		const int nCategoryHeight = (m_dwHideFlags & AFX_RIBBONBAR_HIDE_ELEMENTS) ? 0 : m_nCategoryHeight;

		cy = nQuickAceesToolbarHeight + nCategoryHeight + max( m_nCaptionHeight + m_nTabsHeight, m_sizeMainButton.cy + nYMargin);
	}

	if (afxGlobalData.DwmIsCompositionEnabled())
	{
		if (GetParent()->IsZoomed() && m_bReplaceFrameCaption)
		{
			cy += ::GetSystemMetrics(SM_CYSIZEFRAME) - 2;
		}
	}

	dc.SelectObject(pOldFont);
	return CSize(32767, cy);
}
在MainFrm.h文件里面修改如下:
#include "KofMFCRibbonBar.h"

class CMainFrame : public CMDIFrameWndEx
{
	//...
protected: 
	//CMFCRibbonBar m_wndRibbonBar;
	CKofMFCRibbonBar m_wndRibbonBar;
}
这样子,运行结果还是原样,如下图所示:

现在试着改变高度,修改上面的代码,改动82行为如下:

m_nCategoryHeight = max(m_nCategoryHeight, pCategory->GetMaxHeight(&dc) + 80); 
改动98行为如下:
const int nCategoryHeight = (m_dwHideFlags & AFX_RIBBONBAR_HIDE_ELEMENTS) ? 0 : m_nCategoryHeight + 20
重新编译运行,效果如下图所示:



评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值