Ejemplo n.º 1
0
// 显示弹出菜单
void CMenuItem::ShowPopupMenu()
{
	// 如果已经显示了子菜单,则直接退出
	if(m_pPopupMenu)
	{
		return;
	}

	if(m_bIsPopup)
	{
		m_pPopupMenu = new CDuiMenu(DuiSystem::GetDefaultFont(), 12);
		m_pPopupMenu->SetAutoClose(FALSE);
		m_pPopupMenu->SetParent(this);
		m_pPopupMenu->m_clrRowHover = m_clrHover;	// 设置菜单菜单的背景色
		CPoint point;
		CRect rc = GetRect();
		point.SetPoint(rc.left + rc.Width(), rc.top);
		int nMenuWidth = rc.Width();

		CDlgBase* pParentDlg = GetParentDialog();
		// 如果菜单项定义了XML文件,则使用此菜单项定义的XML文件加载子菜单
		// 否则查找父菜单对象,找到对应的XML文件名,使用此XML文件名加载子菜单
		CString strXmlFile = _T("");		
		CDuiMenu* pParentMenu = GetParentMenu();
		if(pParentMenu)
		{
			if(!m_strMenuXml.IsEmpty())
			{
				// 使用菜单项定义的XML文件
				strXmlFile = m_strMenuXml;
			}else
			{
				// XML文件设置为父菜单对象的XML文件
				strXmlFile = pParentMenu->GetXmlFile();
			}
			// 必须将父菜单对象的自动关闭关掉,否则子菜单显示时候,父菜单失去焦点,会自动销毁
			pParentMenu->SetAutoClose(FALSE);
			// 转换子菜单的坐标
			::ClientToScreen(pParentMenu->GetSafeHwnd(), &point);

			// 将父菜单的预设值菜单项列表添加到子菜单中
			for (size_t i = 0; i < pParentMenu->m_vecMenuItemValue.size(); i++)
			{
				MenuItemValue& itemValue = pParentMenu->m_vecMenuItemValue.at(i);
				m_pPopupMenu->m_vecMenuItemValue.push_back(itemValue);
			}
		}

		if(!strXmlFile.IsEmpty())
		{
			BOOL bSucc = m_pPopupMenu->LoadXmlFile(strXmlFile, pParentDlg, point, WM_DUI_MENU, GetName());
			if(bSucc)	// 加载弹出菜单成功才显示弹出菜单
			{
				// 计算菜单的位置并显示
				CRect rc;
				m_pPopupMenu->GetWindowRect(&rc);
				// 如果超出屏幕右侧范围,则菜单窗口往左移动一些,移动到当前菜单的左侧
				int nScreenWidth= GetSystemMetrics(SM_CXFULLSCREEN);
				if(rc.right > nScreenWidth)
				{
					//rc.OffsetRect(nScreenWidth - rc.right -10, 0);	// 移动到屏幕最右侧
					rc.OffsetRect(-(nMenuWidth + rc.Width()), 0);	// 移动到当前菜单左侧
				}
				int nScreenHeight= GetSystemMetrics(SM_CYFULLSCREEN);
				if(rc.bottom > nScreenHeight)
				{
					rc.OffsetRect(0, -(rc.Height() - m_rc.Height()));	// 高度超出屏幕则改为下对齐方式
				}
				m_pPopupMenu->MoveWindow(rc);
				m_pPopupMenu->ShowWindow(SW_SHOW);
				m_pPopupMenu->SetAutoClose(TRUE);
			}else
			{
				// 弹出菜单加载失败,删除菜单对象
				delete m_pPopupMenu;
				m_pPopupMenu = NULL;
			}
		}
	}
}