如何定制一款12306抢票浏览器――用户界面(三)
rn GetElement( spElem, EID, cstrID, spResElem );
}
HRESULT CDeal12306WebPage::GetElementByClassName( CComPtr & spElem,
const CString & cstrClassName, CComPtr & spResElem )
{
return GetElement( spElem, ECLASSNAME, cstrClassName, spResElem );
}
HRESULT CDeal12306WebPage::GetElementByTagsName( CComPtr & spElem,
const CString & cstrTagName, CComPtr & spResElem )
{
return GetElement( spElem, ETAGNAME, cstrTagName, spResElem );
}
HRESULT CDeal12306WebPage::GetElementByIndex( CComPtr & spElem,
LONG lIndex, CComPtr & spResElem )
{
HRESULT hr = E_FAIL;
do {
CComPtr spElemCollecion;
hr = GetElementCollection( spElem, spElemCollecion);
CHECKHR(hr);
LONG lCollecionCount = 0;
hr = spElemCollecion->get_length(&lCollecionCount);
CHECKHR(hr);
if ( lCollecionCount < lIndex + 1) {
break;
}
CComVariant VarIndex = lIndex;
CComPtr spDisp;
hr = spElemCollecion->item(VarIndex, VarIndex, &spDisp);
CHECKHRPOINTER(hr,spDisp);
hr = spDisp->QueryInterface(IID_IHTMLElement, (LPVOID*)&spResElem);
} while (0);
return hr;
}
HRESULT CDeal12306WebPage::GetElement(
CComPtr & spElem,
EQUERYTYPE eType, const CString & cstrValue,
CComPtr & spResElem )
{
HRESULT hr = E_FAIL;
do {
CComPtr spElemCollection;
hr = GetElementCollection( spElem, spElemCollection);
CHECKHRPOINTER(hr,spElemCollection);
LONG lCollecionCount = 0;
hr = spElemCollection->
get_length(&lCollecionCount);
CHECKHR(hr);
for ( long i = 0; i < lCollecionCount; i++ ) {
CComVariant VarIndex = i;
CComPtr spDispatchElem;
hr = spElemCollection->item( VarIndex, VarIndex, &spDispatchElem );
CHECKHRPOINTER(hr,spDispatchElem);
CComPtr spElem;
hr = spDispatchElem->QueryInterface(IID_IHTMLElement, (LPVOID*)& spElem );
CHECKHRPOINTER(hr, spElem);
CComBSTR bstrValue;
switch (eType) {
case EID: {
hr = spElem->get_id(&bstrValue);
}break;
case ETAGNAME: {
hr = spElem->get_tagName(&bstrValue);
}break;
case ECLASSNAME: {
hr = spElem->get_className(&bstrValue);
}break;
default:
break;
}
CString cstrV((LPWSTR)bstrValue);
if ( 0 == cstrV.CompareNoCase( cstrValue )) {
spResElem = spElem;
break;
}
}
} while (0);
return hr;
}
在获取乘客和车次信息时用到的其他封装函数的实现是
[cpp]
HRESULT CDeal12306WebPage::GetPassengerInfo( CComPtr & spElem,
StSinglePassengerInfo & stSinglePassenger )
{
HRESULT hr = E_FAIL;
do {
CString cstrSeat;
hr = GetOptionValueHelper(spElem, L"seat", cstrSeat);
CHECKHR(hr);
stSinglePassenger.ListSeat.push_back(cstrSeat);
hr = GetOptionValueHelper(spElem, L"ticket", stSinglePassenger.cstrTicket );
CHECKHR(hr);
hr = GetOptionValueHelper(spElem, L"cardtype", stSinglePassenger.cstrCardt