{***************************************************************************} { } { DelphiUIAutomation } { } { Copyright 2015-16 JHC Systems Limited } { } {***************************************************************************} { } { Licensed under the Apache License, Version 2.0 (the "License"); } { you may not use this file except in compliance with the License. } { You may obtain a copy of the License at } { } { http://www.apache.org/licenses/LICENSE-2.0 } { } { Unless required by applicable law or agreed to in writing, software } { distributed under the License is distributed on an "AS IS" BASIS, } { WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. } { See the License for the specific language governing permissions and } { limitations under the License. } { } {***************************************************************************} unit DelphiUIAutomation.Base; interface uses types, DelphiUIAutomation.Condition, UIAutomationClient_TLB; type IAutomationBase = interface ['{E858CFB6-C144-444F-9743-5F2F1919EC8D}'] function GetName: string; function GetBoundingRectangle : TRect; /// /// Gets the name of the element /// property Name : string read getName; /// /// Gets the bounding rectangle of the element /// property BoundingRectangle : TRect read GetBoundingRectangle; /// /// Gets the window handle of the Window Element /// function GetHandle : Pointer; end; /// /// The base class for automation objects /// TAutomationBase = class (TInterfacedObject, IAutomationBase) protected FElement : IUIAutomationElement; function getName: string; virtual; function GetBoundingRectangle : TRect; function GetControlType: Integer; // Γί°‘ 24_0910 17:49:37 kku protected /// /// Gets the window control pattern /// function GetWindowPattern : IUIAutomationWindowPattern; function GetSelectionItemPattern: IUIAutomationSelectionItemPattern; function GetValuePattern: IUIAutomationValuePattern; function GetSelectionPattern: IUIAutomationSelectionPattern; function GetInvokePattern: IUIAutomationInvokePattern; function GetExpandCollapsePattern: IUIAutomationExpandCollapsePattern; function GetTogglePattern: IUIAutomationTogglePattern; function GetGridPattern: IUIAutomationGridPattern; function GetTablePattern: IUIAutomationTablePattern; /// /// Finds the elements /// // function FindAll : IUIAutomationElementArray; overload; /// /// Finds the elements, based on scope /// function FindAll (scope : TreeScope) : IUIAutomationElementArray; overload; /// /// Finds the elements, based on scope and condition /// function FindAll (scope : TreeScope; condition : ICondition) : IUIAutomationElementArray; overload; public function FindAll : IUIAutomationElementArray; overload; /// /// Gets the name of the element /// property Name : string read getName; property CtrlType: Integer read GetControlType; /// /// Gets the bounding rectangle of the element /// property BoundingRectangle : TRect read GetBoundingRectangle; /// /// Constructor for the element. /// constructor Create(element : IUIAutomationElement); virtual; /// /// Gets the window handle of the Window Element /// function GetHandle : Pointer; end; implementation uses DelphiUIAutomation.PatternIDs, DelphiUIAutomation.Exception, DelphiUIAutomation.Automation; constructor TAutomationBase.Create(element: IUIAutomationElement); begin inherited create; Felement := element; end; function TAutomationBase.FindAll: IUIAutomationElementArray; begin result := self.FindAll (TreeScope_Children); end; function TAutomationBase.FindAll(scope: TreeScope): IUIAutomationElementArray; var condition : ICondition; collection : IUIAutomationElementArray; begin condition := TUIAuto.CreateTrueCondition; // Find the elements self.FElement.FindAll(scope, condition.getCondition, collection); result := collection; end; function TAutomationBase.FindAll(scope: TreeScope; condition: ICondition): IUIAutomationElementArray; var collection : IUIAutomationElementArray; begin // Find the elements self.FElement.FindAll(scope, condition.getCondition, collection); result := collection; end; function TAutomationBase.GetBoundingRectangle: TRect; var rect : tagRect; outRect : TRect; begin Felement.Get_CurrentBoundingRectangle(rect); outRect.Top := rect.top; outRect.Left := rect.left; outRect.Width := rect.right; outRect.Height := rect.bottom; result := outRect; end; function TAutomationBase.GetTablePattern : IUIAutomationTablePattern; var inter: IInterface; pattern : IUIAutomationTablePattern; begin fElement.GetCurrentPattern(UIA_TablePatternId, inter); if (inter <> nil) then begin if Inter.QueryInterface(IID_IUIAutomationTablePattern, pattern) <> S_OK then begin raise EDelphiAutomationException.Create('Unable to initialise control pattern'); end; end; result := pattern; end; function TAutomationBase.GetGridPattern : IUIAutomationGridPattern; var inter: IInterface; pattern : IUIAutomationGridPattern; begin fElement.GetCurrentPattern(UIA_GridPatternId, inter); if (inter <> nil) then begin if Inter.QueryInterface(IID_IUIAutomationGridPattern, pattern) <> S_OK then begin raise EDelphiAutomationException.Create('Unable to initialise control pattern'); end; end; result := pattern; end; function TAutomationBase.GetSelectionItemPattern : IUIAutomationSelectionItemPattern; var inter: IInterface; pattern : IUIAutomationSelectionItemPattern; begin fElement.GetCurrentPattern(UIA_SelectionItemPatternId, inter); if (inter <> nil) then begin if Inter.QueryInterface(IID_IUIAutomationSelectionItemPattern, pattern) <> S_OK then begin raise EDelphiAutomationException.Create('Unable to initialise control pattern'); end; end; result := pattern; end; function TAutomationBase.GetSelectionPattern: IUIAutomationSelectionPattern; var inter: IInterface; pattern: IUIAutomationSelectionPattern; begin fElement.GetCurrentPattern(UIA_SelectionPatternId, inter); if (inter <> nil) then begin if Inter.QueryInterface(IID_IUIAutomationSelectionPattern, pattern) <> S_OK then begin raise EDelphiAutomationException.Create('Unable to initialise control pattern'); end; end; result := pattern; end; function TAutomationBase.GetValuePattern : IUIAutomationValuePattern; var inter: IInterface; pattern : IUIAutomationValuePattern; begin fElement.GetCurrentPattern(UIA_ValuePatternId, inter); if (inter <> nil) then begin if Inter.QueryInterface(IID_IUIAutomationValuePattern, pattern) <> S_OK then begin raise EDelphiAutomationException.Create('Unable to initialise value pattern'); end; end; result := pattern; end; function TAutomationBase.GetExpandCollapsePattern : IUIAutomationExpandCollapsePattern; var inter: IInterface; pattern : IUIAutomationExpandCollapsePattern; begin self.fElement.GetCurrentPattern(UIA_ExpandCollapsePatternId, inter); if (inter <> nil) then begin if inter.QueryInterface(IID_IUIAutomationExpandCollapsePattern, pattern) <> S_OK then begin raise EDelphiAutomationException.Create('Unable to initialise control pattern'); end; end; result := pattern; end; function TAutomationBase.GetInvokePattern : IUIAutomationInvokePattern; var unknown: IInterface; Pattern : IUIAutomationInvokePattern; begin fElement.GetCurrentPattern(UIA_InvokePatternID, unknown); if (unknown <> nil) then begin if unknown.QueryInterface(IUIAutomationInvokePattern, Pattern) <> S_OK then begin raise EDelphiAutomationException.Create('Unable to initialise control pattern'); end; end; result := pattern; end; function TAutomationBase.GetWindowPattern : IUIAutomationWindowPattern; var inter: IInterface; pattern: IUIAutomationWindowPattern; begin self.fElement.GetCurrentPattern(UIA_WindowPatternId, inter); if (inter <> nil) then begin if inter.QueryInterface(IID_IUIAutomationWindowPattern, pattern) <> S_OK then begin raise EDelphiAutomationException.Create('Unable to initialise Window control pattern'); end; end; result := pattern; end; function TAutomationBase.GetTogglePattern: IUIAutomationTogglePattern; var inter: IInterface; pattern : IUIAutomationTogglePattern; begin self.fElement.GetCurrentPattern(UIA_TogglePatternId, inter); if (inter <> nil) then begin if inter.QueryInterface(IID_IUIAutomationTogglePattern, pattern) <> S_OK then begin raise EDelphiAutomationException.Create('Unable to initialise control pattern'); end; end; result := pattern; end; function TAutomationBase.getName: string; var name : widestring; begin FElement.Get_CurrentName(name); result := name; end; function TAutomationBase.GetControlType: Integer; begin Result := 0; FElement.Get_CurrentControlType(Result); end; function TAutomationBase.GetHandle: Pointer; var retVal : Pointer; begin self.FElement.Get_CurrentNativeWindowHandle(retVal); result := retVal; end; end.