Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1851636
  • 博文数量: 909
  • 博客积分: 4000
  • 博客等级: 上校
  • 技术积分: 12260
  • 用 户 组: 普通用户
  • 注册时间: 2008-05-06 20:50
文章分类

全部博文(909)

文章存档

2008年(909)

我的朋友

分类:

2008-05-06 21:30:33

一起学习
在对话框程序中插入DialogBar
作者:徐建鹏

下载本文示例源代码

程序运行效果图如下:


在基于MainFrm程序中加入DialogBar很方便,大家都知道,DialogBar有着工具条无法比拟的优越性,它可以任意放置控件,轻松实现如WPS OFFICE的导航功能。但是在基于Dialog的程序怎么实现这种导航功能呢?本文拟针对这个问题提出解决的办法,将困扰本人很久的问题的解决方法和大家一同分享。
大家都知道,CDialogBar不能在Dialog程序中产生的原因是Dialog中没有MainFrm,所以我就从CMiniFrameWnd产生了自己的一个类CMyMiniFrm。
BOOL CMyMiniFrm::OnCmdMsg(UINT nID, int nCode, void* pExtra, AFX_CMDHANDLERINFO* pHandlerInfo) 

{

	// pass those up in the dialog to leave the OnUpdateUI mechanism to flow

	BOOL br = GetParent()->OnCmdMsg(nID, nCode, pExtra, pHandlerInfo);

	// leave the default proc to handles the tooltip updating mechanism

	CMiniFrameWnd::OnCmdMsg(nID, nCode, pExtra, pHandlerInfo);

	return br;	// return what the parent returns

}

然后,我们可以从Cstatic产生自己的生成Cdialog类了,其中创建CDialogBar的代码如下
void CStTbar::PreSubclassWindow() 

{

	CStatic::PreSubclassWindow();

	

	RECT rt; GetWindowRect(&rt);

	GetParent()->ScreenToClient(&rt);

	// hide the place holder, no not destro it I need it to rerout the messages

	ShowWindow(SW_HIDE);

	// make it on the heap as long  CMyMiniFrm::OnNcDestroy call ''delete this'' 

	// save me to map one more message

	m_minifrm = new CMyMiniFrm();

	m_minifrm->Create( AfxRegisterWndClass(0,0,0),

				"",WS_VISIBLE|WS_CHILD,rt,GetParent()/*of placeholder*/);



	{

		// Initialize dialog bar m_wndMyDialogBar

		if (!m_wndMyDialogBar.Create(m_minifrm, GetDlgCtrlID(),

			CBRS_RIGHT | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_HIDE_INPLACE,

			CG_ID_VIEW_MYDIALOGBAR))

		{

			TRACE0("Failed to create dialog bar m_wndMyDialogBar\n");

			return;		// fail to create

		}



		m_wndMyDialogBar.EnableDocking(CBRS_ALIGN_LEFT | CBRS_ALIGN_RIGHT);

		m_minifrm->EnableDocking(CBRS_ALIGN_ANY);

		if(rt.right-rt.left > rt.bottom-rt.top)

			m_minifrm->DockControlBar(&m_wndMyDialogBar,CBRS_ALIGN_LEFT);	

		else

			// dock verically

			m_minifrm->DockControlBar(&m_wndMyDialogBar,CBRS_ALIGN_RIGHT);

		m_minifrm->RecalcLayout();

		//m_minifrm->DockControlBar(&m_wndMyDialogBar);



	}

}
然后,我们还要解决消息传递问题。
void CStTbar::PreTranslate(MSG* pMsg)

{

	// forward this to the toolbar

	// 

	if(m_wndMyDialogBar.IsWindowVisible())

			m_wndMyDialogBar.OnUpdateCmdUI(m_minifrm,TRUE);

}


这样一个在对话框中产生DialogBar的类基本完成。
然后在要使用DialogBar的对话框中增加一个STATIC控件,定义成和准备当作DialogBar使用的Dialog一样的ID号,再给STATIC控件增加CstTbar型的Contrl变量,就可以使用了。

另外,我们只要为该DialogBar创建一个类,在该类中就可以像操作普通对话框一样在ClassWizard中对各控件进行操作或处理各种消息。

下载本文示例代码


在对话框程序中插入DialogBar在对话框程序中插入DialogBar在对话框程序中插入DialogBar在对话框程序中插入DialogBar在对话框程序中插入DialogBar在对话框程序中插入DialogBar在对话框程序中插入DialogBar在对话框程序中插入DialogBar在对话框程序中插入DialogBar在对话框程序中插入DialogBar在对话框程序中插入DialogBar在对话框程序中插入DialogBar
阅读(567) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~