// UI初始化,此函数在窗口的OnCreate函数中调用 void CDuiMenu::InitUI(CRect rcClient) { // 如果有菜单项的预设置值,则设置相应的值到控件 if(m_vecMenuItemValue.size() > 0) { for (size_t i = 0; i < m_vecMenuItemValue.size(); i++) { MenuItemValue& itemValue = m_vecMenuItemValue.at(i); CControlBase* pControlBase = GetControl(itemValue.strName); if(pControlBase) { if(!itemValue.strTitle.IsEmpty()) { ((CControlBaseFont*)pControlBase)->SetTitle(itemValue.strTitle); } if(!itemValue.bVisible) { pControlBase->SetVisible(FALSE); } if(itemValue.bDisable) { pControlBase->SetDisable(TRUE); } if(itemValue.nCheck != -1) { ((CMenuItem*)pControlBase)->SetCheck(itemValue.nCheck); } } } // 刷新菜单项位置信息 SetMenuPoint(); } }
// 添加菜单分隔 int CDuiMenu::AddSeparator(int nIndex) { // 可以使用矩形控件,也可以使用图片控件 CControlBase * pControlBase = new CRectangle(GetSafeHwnd(),this, -1, CRect(0, 0, 0, 0), Color(254, 227, 229, 230)); if(nIndex >= 0 && nIndex < (int)m_vecControl.size()) { m_vecControl.insert(m_vecControl.begin() + nIndex, pControlBase); } else { m_vecControl.push_back(pControlBase); } SetMenuPoint(); return m_vecControl.size(); }
int CDuiMenu::AddMenu(CString strText, UINT uMenuID, CString strImage, BOOL bSelect, int nIndex) { CControlBase * pControlBase = NULL; BSTR bsFont = m_strFont.AllocSysString(); FontFamily fontFamily(bsFont); Font font(&fontFamily, (REAL)m_nFontWidth, m_fontStyle, UnitPixel); ::SysFreeString(bsFont); StringFormat strFormat; strFormat.SetAlignment(StringAlignmentNear); strFormat.SetFormatFlags( StringFormatFlagsNoWrap | StringFormatFlagsMeasureTrailingSpaces); Size size = GetTextBounds(font, strFormat, strText); if(size.Width > m_nWidth - m_nLeft - 4) { m_nWidth = size.Width + m_nLeft + 4; } pControlBase = new CMenuItem(GetSafeHwnd(),this, uMenuID, CRect(0, 0, 0, 0), strText, m_nLeft, bSelect); ((CControlBaseFont*)pControlBase)->SetFont(m_strFont, m_nFontWidth, m_fontStyle); if(!strImage.IsEmpty()) { ((CMenuItem *)pControlBase)->SetBitmap(strImage); } if(nIndex >= 0 && nIndex < (int)m_vecControl.size()) { m_vecControl.insert(m_vecControl.begin() + nIndex, pControlBase); } else { m_vecControl.push_back(pControlBase); } SetMenuPoint(); return m_vecControl.size(); }
// 重载加载XML节点函数,加载下层的Menu item信息 BOOL CDuiMenu::Load(DuiXmlNode pXmlElem, BOOL bLoadSubControl) { SetRect(CRect(0, 0, m_nWidth, m_nHeight)); __super::Load(pXmlElem, bLoadSubControl); if(pXmlElem == NULL) { return FALSE; } if(!bLoadSubControl) { // 不加载子控件 return TRUE; } // 菜单窗口宽度设置为popup窗口的宽度 m_nWidth = m_size.cx; // 创建窗口 Create(m_pParent, m_point, m_uMessageID); // 加载下层的item节点信息(正常情况下都使用DlgPopup的Load控件方式加载菜单项,下面的解析比较少用到) int nIdIndex = 100; for (DuiXmlNode pItemElem = pXmlElem.child(_T("item")); pItemElem; pItemElem=pItemElem.next_sibling()) { CString strId = pItemElem.attribute(_T("id")).value(); int nId = nIdIndex; if(strId != _T("")) { nId = _ttoi(strId); } CString strType = pItemElem.attribute(_T("type")).value(); CString strName = pItemElem.attribute(_T("name")).value(); CString strImage = pItemElem.attribute(_T("image")).value(); CString strTitle = pItemElem.attribute(_T("title")).value(); if(strType == _T("separator")) { // 分隔线也可以用图片的方式 AddSeparator(); continue; } CString strTitleU = strTitle; if(strImage.Find(_T(".")) != -1) // 加载图片文件 { CString strImgFile = strImage; AddMenu(strTitleU, nIdIndex, strImgFile); }else if(!strImage.IsEmpty()) { UINT nResourceID = _ttoi(strImage); AddMenu(strTitleU, nIdIndex, nResourceID); }else { AddMenu(strTitleU, nIdIndex); } nIdIndex++; } // 刷新各菜单控件的位置 SetMenuPoint(); m_bInit = TRUE; return TRUE; }
// 重载加载XML节点函数,加载下层的Menu item信息 BOOL CDuiMenu::Load(TiXmlElement* pXmlElem, BOOL bLoadSubControl) { SetRect(CRect(0, 0, m_nWidth, m_nHeight)); __super::Load(pXmlElem, bLoadSubControl); if(pXmlElem == NULL) { return FALSE; } if(!bLoadSubControl) { // 不加载子控件 return TRUE; } // 菜单窗口宽度设置为popup窗口的宽度 m_nWidth = m_size.cx; // 创建窗口 Create(m_pParent, m_point, m_uMessageID); // 加载下层的item节点信息(正常情况下都使用DlgPopup的Load控件方式加载菜单项,下面的解析比较少用到) int nIdIndex = 100; TiXmlElement* pItemElem = NULL; for (pItemElem = pXmlElem->FirstChildElement("item"); pItemElem != NULL; pItemElem=pItemElem->NextSiblingElement()) { CStringA strId = pItemElem->Attribute("id"); int nId = nIdIndex; if(strId != "") { nId = atoi(strId); } CStringA strType = pItemElem->Attribute("type"); CStringA strName = pItemElem->Attribute("name"); CStringA strImage = pItemElem->Attribute("image"); CStringA strTitle = pItemElem->Attribute("title"); if(strType == "separator") { // 分隔线也可以用图片的方式 AddSeparator(); continue; } CString strTitleU = CA2T(strTitle, CP_UTF8); if(strImage.Find(".") != -1) // 加载图片文件 { CString strImgFile = CA2T(strImage, CP_UTF8); AddMenu(strTitleU, nIdIndex, strImgFile); }else if(!strImage.IsEmpty()) { UINT nResourceID = atoi(strImage); AddMenu(strTitleU, nIdIndex, nResourceID); }else { AddMenu(strTitleU, nIdIndex); } nIdIndex++; } // 刷新各菜单控件的位置 SetMenuPoint(); m_bInit = TRUE; return TRUE; }