{***************************************************************************}
{ }
{ DelphiUIAutomation }
{ }
{ Copyright 2015 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.ComboBox;
interface
uses
generics.collections,
DelphiUIAutomation.Base,
DelphiUIAutomation.ListItem,
UIAutomationClient_TLB;
type
IAutomationComboBox = interface (IAutomationBase)
['{531C8646-A605-4D6D-BEE0-89E1719D6321}']
///
/// Gets the text associated with this combobox
///
function getText: string;
///
/// Sets the text associated with this combobox
///
procedure setText(const Value: string);
///
/// Gets or sets the text associated with this combobox
///
property Text : string read getText write setText;
end;
///
/// Represents a combobox control
///
TAutomationComboBox = class (TAutomationBase, IAutomationComboBox)
strict private
FItems : TObjectList;
FExpandCollapsePattern : IUIAutomationExpandCollapsePattern;
FValuePattern : IUIAutomationValuePattern;
private
///
/// Gets the text associated with this combobox
///
function getText: string;
///
/// Sets the text associated with this combobox
///
procedure setText(const Value: string);
function getItems: TObjectList;
procedure InitialiseList;
public
///
/// Gets or sets the text associated with this combobox
///
property Text : string read getText write setText;
///
/// Gets the list of items associated with this combobox
///
property Items : TObjectList read getItems;
///
/// Call the expand property
///
function Expand : HRESULT;
///
/// Call the collapse property
///
function Collapse : HRESULT;
///
/// Constructor for comboboxes.
///
constructor Create(element : IUIAutomationElement); override;
///
/// Destructor for comboboxes.
///
destructor Destroy; override;
end;
implementation
uses
ActiveX,
DelphiUIAutomation.Exception,
DelphiUIAutomation.Automation,
DelphiUIAutomation.ControlTypeIDs,
DelphiUIAutomation.PropertyIDs,
DelphiUIAutomation.PatternIDs,
sysutils;
{ TAutomationComboBox }
constructor TAutomationComboBox.Create(element: IUIAutomationElement);
begin
inherited Create(element);
FExpandCollapsePattern := GetExpandCollapsePattern;
FValuePattern := GetValuePattern;
self.Expand; // Have to expand for the list to be available
InitialiseList;
end;
destructor TAutomationComboBox.Destroy;
begin
FItems.free;
inherited;
end;
procedure TAutomationComboBox.InitialiseList;
var
collection : IUIAutomationElementArray;
itemElement : IUIAutomationElement;
count : integer;
length : integer;
retVal : integer;
item : TAutomationListItem;
begin
FItems := TObjectList.create;
// Find the elements
collection := self.FindAll(TreeScope_Children);
collection.Get_Length(length);
for count := 0 to length -1 do
begin
collection.GetElement(count, itemElement);
itemElement.Get_CurrentControlType(retVal);
if (retVal = UIA_ListItemControlTypeId) then
begin
item := TAutomationListItem.Create(itemElement);
FItems.Add(item);
end;
end;
end;
function TAutomationComboBox.Expand: HRESULT;
begin
if assigned(FExpandCollapsePattern) then
result := self.FExpandCollapsePattern.Expand
else
result := -1;
end;
function TAutomationComboBox.Collapse: HRESULT;
begin
result := self.FExpandCollapsePattern.Collapse;
end;
function TAutomationComboBox.getItems : TObjectList;
begin
result := self.FItems;
end;
function TAutomationComboBox.getText: string;
var
value : widestring;
begin
FValuePattern.Get_CurrentValue(value);
Result := trim(value);
end;
procedure TAutomationComboBox.setText(const Value: string);
begin
FValuePattern.SetValue(value);
end;
end.