//
查找自定义类MHPToolBar窗口句柄
HWND hwnd = ::FindWindow(_T(
"TdxW_MainFrame_Class"
), NULL);
if
(hwnd)
{
OFindWnd oWnd(hwnd, _T(
"MHPToolBar"
), _T(
"MainViewBar"
));
hwnd = oWnd.m_hWnd;
}
if
(!::IsWindow(hwnd))
return
FALSE;
//
枚举并查找
"买入"
控件
IAccessible *pIAcc = NULL;
HRESULT hr = AccessibleObjectFromWindow(hwnd, OBJID_WINDOW, IID_IAccessible, (void**)&pIAcc);
if
(SUCCEEDED(hr) && pIAcc)
{
VARIANT varChild;
VariantInit(&varChild);
IAccessible *pIAccChild = NULL;
g_sSubPrefix =
""
;
if
(CXMSAALib::FindAccessible(pIAcc, _T(
"买入"
), _T(
"按下按钮"
), _T(
"MHPToolBar"
), &pIAccChild, &varChild))
{
//find
DEBUGTRACE(
"find"
);
}
else
{
//not
find
DEBUGTRACE(
"not find"
);
}
}
BOOL CXMSAALib::FindAccessible(IAccessible* accParent,
LPTSTR szName,
LPTSTR szRole,
LPTSTR szClass,
IAccessible** paccChild,
VARIANT* pvarChild)
{
BOOL found =
false
;
TCHAR szObjName[MAX_PATH], szObjRole[MAX_PATH], szObjClass[MAX_PATH], szObjState[MAX_PATH];
IAccessible* pCAcc = NULL;
VARIANT* vt_output = NULL;
if
(accParent == NULL)
return
FALSE;
long lChildCount = 0;
HRESULT hr = accParent->get_accChildCount(&lChildCount);
if
(FAILED(hr) || (lChildCount == 0))
return
FALSE;
DEBUGTRACE(
"%s obj=0x%p, numChildren=%d"
, g_sSubPrefix.c_str(), accParent, lChildCount);
vt_output = new VARIANT[lChildCount];
for
(int i = 0; i < lChildCount; i++)
VariantInit(&vt_output[i]);
long lNewChildCount = 0;
hr = AccessibleChildren(accParent, 0, lChildCount, vt_output, &lNewChildCount);
if
(FAILED(hr))
goto
exit
;
for
(int j = 0; j < lNewChildCount && !found; j++)
{
VARIANT varChild;
VariantInit(&varChild);
IDispatch* disp = NULL;
pCAcc = NULL;
//If
the vt member of an array element is VT_DISPATCH,
//then
the pdispVal member
for
that element is the address of the child object's IDispatch interface.
if
(vt_output[j].vt == VT_DISPATCH)
{
disp = vt_output[j].pdispVal;
}
//If
the vt member of an array element is VT_I4,
then
the lVal member
for
that element is the child ID.
else
if
(vt_output[j].vt == VT_I4)
{
hr = accParent->get_accChild(vt_output[j], &disp);
//If
a child is an element, get_accChild returns S_FALSE, and the parent will provide information
for
that child.
if
(!SUCCEEDED(hr) || !disp)
{
//element
deal
varChild.vt = VT_I4;
varChild.lVal = vt_output[j].lVal;
*paccChild = accParent;
}
}
else
//
未发现有进入
{
continue
;
}
if
(disp)
{
hr = disp->QueryInterface(IID_IAccessible, (void**)&pCAcc);
if
(FAILED(hr))
continue
;
varChild.vt = VT_I4;
varChild.lVal = CHILDID_SELF;
*paccChild = pCAcc;
}
//
Skip invisible and unavailable objects and their children
GetObjectStateString(*paccChild, &varChild, szObjState, MAX_PATH);
//
if
(NULL != _tcsstr(szObjState, _T(
"unavailable"
)))
//
{
//
if
(pCAcc)
//
pCAcc->Release();
//
continue
;
//
}
UINT nState = 0;
GetObjectState(*paccChild, &varChild, nState);
//
check
if
object is available
if
((nState & STATE_SYSTEM_UNAVAILABLE /*STATE_SYSTEM_INVISIBLE*/ /*| STATE_SYSTEM_OFFSCREEN*/))
{
SAFE_RELEASE(pCAcc);
continue
;
}
GetObjectName(*paccChild, &varChild, szObjName, MAX_PATH);
GetObjectRoleString(*paccChild, &varChild, szObjRole, MAX_PATH);
//GetObjectClass
(*paccChild, szObjClass, sizeof(szObjClass));
memset(szObjClass, 0, sizeof(szObjClass));
HWND hwndChild = 0;
WindowFromAccessibleObject(*paccChild, &hwndChild);
if
(hwndChild)
GetClassName(hwndChild, szObjClass, MAX_PATH);
if
((!szName || !_tcscmp(szName, szObjName)) && (!szRole || !_tcscmp(szRole, szObjRole)) && (!szClass || !_tcscmp(szClass, szObjClass)))
{
found =
true
;
*pvarChild = varChild;
break
;
}
if
(pCAcc)
DEBUGTRACE(
"%s name=%s, role=%s, class=%s, state=%s obj=0x%p,idx=0x%p"
, g_sSubPrefix.c_str(), szObjName, szObjRole, szObjClass, szObjState, pCAcc, varChild.lVal);
else
DEBUGTRACE(
"%s name=%s, role=%s, class=%s, state=%s elem,idx=0x%p"
, g_sSubPrefix.c_str(), szObjName, szObjRole, szObjClass, szObjState, varChild.lVal);
if
(!found && pCAcc)
{
string sParentPrefix = g_sSubPrefix;
g_sSubPrefix +=
" "
;
//
Go deeper
found = FindAccessible(pCAcc, szName, szRole, szClass, paccChild, pvarChild);
if
(*paccChild != pCAcc)
pCAcc->Release();
g_sSubPrefix = sParentPrefix;
}
}
exit
:
if
(vt_output)
{
for
(int k = 0; k < lChildCount; k++)
VariantClear(&vt_output[k]);
delete vt_output;
}
return
found;
}