重複使用現有的物件指標
在此案例中,伺服器會每次使用相同的 IAccessible 介面指標來回應 OBJID_CLIENT 要求。
在下列範例程式代碼中,會從額外的視窗數據擷取控件物件,並呼叫控件的方法來擷取輔助功能伺服器物件(應用程式定義的 AccServer 類別),如果有的話。 如果輔助功能伺服器尚未存在,則會建立它。
建立輔助功能伺服器物件時,其引用計數為 1。 LresultFromObject 會多次遞增引用計數,但當客戶端完成使用該物件時,這些引用將會被釋放。 當控制窗口被銷毀時,伺服器會釋放其參考。
case WM_GETOBJECT:
{
// Return the IAccessible object.
if ((DWORD)lParam == (DWORD)OBJID_CLIENT)
{
// Get the control.
CustomListControl* pCustomList = (CustomListControl*)(LONG_PTR)GetWindowLongPtr(hwnd, 0);
// Create the accessible object.
AccServer* pAccServer = pCustomList->GetAccServer();
if (pAccServer == NULL)
{
pAccServer = new AccServer(hwnd, pCustomList);
pCustomList->SetAccServer(pAccServer);
}
if (pAccServer != NULL) // NULL if out of memory.
{
LRESULT Lresult = LresultFromObject(IID_IAccessible, wParam, pAccServer);
return Lresult;
}
else return 0;
}
break;
}
case WM_DESTROY:
{
CustomListControl* pCustomList = (CustomListControl*)(LONG_PTR)GetWindowLongPtr(hwnd, 0);
AccServer* pAccServer = pCustomList->GetAccServer();
if (pAccServer!= NULL)
{
// Notify the accessibility object that the control no longer exists.
pAccServer->SetControlIsAlive(false);
// Release the reference created in WM_GETOBJECT.
pAccServer->Release();
}
// Destroy the control.
delete pCustomList;
break;
}