DHtml中的TabCtrl模拟比toolbar模拟简单得多
也就是简单的CSS即可实现
本文使用了3个css来实现
.tab_current
{
cursor:hand;
border-bottom:1px solid #406DB0;
font:normal 9px 'Trebuchet MS','Arial';
height:23px;
padding:2px 6px 0 6px;
line-height:19px;
text-align:center;
color:#ffffff;
}
.tab_normal
{
cursor:hand;
font:normal 9px 'Trebuchet MS','Arial';
height:23px;
padding:2px 6px 0 6px;
line-height:19px;
text-align:center;
border-bottom: 1px solid #CAE2FD;
color:#aeaeae;
}
.tab_hover
{
cursor:hand;
border-bottom:1px solid #CAE2FD;
font:normal 9px 'Trebuchet MS','Arial';
height:23px;
padding:2px 6px 0 6px;
line-height:19px;
text-align:center;
color:#ffffff;
}
函数集
int m_currentTab;
void AddTabElem(CString text,CString title,int id);
void ClearTabElem();
void SwitchTabElem(int id);
int GetTabElemCount();
- int CDHtmlUIDemoDlg::GetTabElemCount()
- {
- cHtmlTable tb(this->GetElem("tbTab"));
- if(!tb.Valid()||tb.GetRowCount()<=0)
- return 0;
- return tb.GetRow(0).GetCellCount();
- }
- void CDHtmlUIDemoDlg::AddTabElem(CString text,CString title,int id)
- {
- cHtmlTable tb(this->GetElem("tbTab"));
- if(!tb.Valid())
- return;
- cHtmlTableRow tr(0);
- if(tb.GetRowCount()<=0)
- tr = tb.InsertRow(-1,0);
- else
- tr = tb.GetRow(0);
- CString css;
- if(tr.GetCellCount()==0)
- css = "tab1";
- else
- css = "tab2";
- cHtmlTableCell tc = tr.InsertColumn(-1);
- tc.mi_Elem->put_className(css.AllocSysString());
- stringstream ss;
- ss << "<div id='tabelem_" << id << "' class='tab_normal'>" << text << "</div>";
- tc.SetInnerHtml(ss.str().c_str());
- }
- void CDHtmlUIDemoDlg::ClearTabElem()
- {
- cHtmlTable tb(this->GetElem("tbTab"));
- if(!tb.Valid())
- return;
- if(tb.GetRowCount()>0)
- tb.GetRow(0).Remove();
- }
- void CDHtmlUIDemoDlg::SwitchTabElem(int id)
- {
- char sid[100];
- sprintf_s(sid,sizeof(sid),"tabelem_%d",id);
- cHtmlElement elem = this->GetElem(sid);
- if(elem.Valid())
- {
- elem.mi_Elem->put_className(CString("tab_current").AllocSysString());
- }
- if(this->m_currentTab>=0)
- {
- sprintf_s(sid,sizeof(sid),"tabelem_%d",this->m_currentTab);
- cHtmlElement elem = this->GetElem(sid);
- if(elem.Valid())
- elem.mi_Elem->put_className(CString("tab_normal").AllocSysString());
- }
- this->m_currentTab = id;
- }
添加标签的代码
this->AddTabElem("标签1","我的标签1",TAB_1);
this->AddTabElem("标签2","我的标签2",TAB_2);
this->SwitchTabElem(TAB_2);
由于只是演示而已,所以只要标签添加的函数,并没有标签管理的函数,读者可以自行添加。
处理事件也很简单,使用DHTML_EVENT_CLASS+CSS即可
- BEGIN_DHTML_EVENT_MAP(CDHtmlUIDemoDlg)
- DHTML_EVENT_CLASS(DISPID_HTMLELEMENTEVENTS_ONMOUSEOVER, _T("tab_normal"), OnMouseOverTab)
- DHTML_EVENT_CLASS(DISPID_HTMLELEMENTEVENTS_ONMOUSEOUT, _T("tab_hover"), OnMouseOutTab)
- DHTML_EVENT_CLASS(DISPID_HTMLELEMENTEVENTS_ONCLICK, _T("tab_hover"), OnClickTab)
- END_DHTML_EVENT_MAP()
- HRESULT CDHtmlUIDemoDlg::OnMouseOverTab(IHTMLElement *phtmlElement)
- {
- phtmlElement->put_className(CString("tab_hover").AllocSysString());
- return S_OK;
- }
- HRESULT CDHtmlUIDemoDlg::OnClickTab(IHTMLElement *phtmlElement)
- {
- BSTR bstr;
- phtmlElement->get_id(&bstr);
- string sid = (LPCTSTR)CString(bstr);
- int pos = sid.find('_');
- int id = atoi(sid.substr(pos+1,sid.length()-pos-1).c_str());
- CString fmt;
- fmt.Format("Tab ID:%d click",id);
- AfxMessageBox(fmt);
- this->SwitchTabElem(id);
- return S_OK;
- }
- HRESULT CDHtmlUIDemoDlg::OnMouseOutTab(IHTMLElement *phtmlElement)
- {
- phtmlElement->put_className(CString("tab_normal").AllocSysString());
- return S_OK;
- }