Skip to content

Commit 5b2ad30

Browse files
committed
Added Preserve Selected Device in RefreshDeviceList
Added Preserve Selected Device in RefreshDeviceList
1 parent ebbdbbc commit 5b2ad30

2 files changed

Lines changed: 38 additions & 31 deletions

File tree

changelog.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
WIAPASCAL CHANGE LOG
22
======================
3+
2025/08/29 Added Preserve Selected Device in RefreshDeviceList
4+
35
2025/06/11 Release 1.0.1;
46
Solved extra File with size=0 in some scanners;
57
Fix Settings Form Ok,Cancel Button Negative Top;

wia.pas

Lines changed: 36 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -542,18 +542,17 @@ TWIAManager = class(TObject)
542542
rOnBeforeDeviceTransfer: TOnDeviceTransfer;
543543

544544
function GetDevMgrIntf: WIA_LH.IWiaDevMgr2;
545-
procedure SetEnumAll(AValue: Boolean);
546545
function GetSelectedDevice: TWIADevice;
547546
procedure SetSelectedDeviceIndex(AValue: Integer);
548547
function GetDevice(Index: Integer): TWIADevice;
549548
function GetDevicesCount: Integer;
550549

551550
function CreateDevManager: IUnknown; virtual;
552551

553-
procedure EmptyDeviceList(setZeroLength:Boolean);
552+
procedure EmptyDeviceList(PreserveSelected: Boolean);
554553

555554
//Enumerate the avaliable devices
556-
function EnumerateDevices: Boolean;
555+
function EnumerateDevices(PreserveSelected: Boolean): Boolean;
557556

558557
public
559558
constructor Create(AEnumAll: Boolean = True);
@@ -563,7 +562,7 @@ TWIAManager = class(TObject)
563562
procedure ClearDeviceList;
564563

565564
//Refresh the list of sources
566-
procedure RefreshDeviceList;
565+
procedure RefreshDeviceList(PreserveSelected: Boolean=True);
567566

568567
//Display a dialog to let the user choose a Device and returns it's index
569568
function SelectDeviceDialog: Integer; virtual;
@@ -579,7 +578,7 @@ TWIAManager = class(TObject)
579578
property DevMgrIntf: WIA_LH.IWiaDevMgr2 read GetDevMgrIntf;
580579

581580
//Kind of Enum, if True Enum even disconnected Devices
582-
property EnumAll: Boolean read rEnumAll write SetEnumAll;
581+
property EnumAll: Boolean read rEnumAll write rEnumAll;
583582

584583
//Returns a Device
585584
property Devices[Index: Integer]: TWIADevice read GetDevice;
@@ -2963,15 +2962,6 @@ function TWIAManager.GetDevMgrIntf: WIA_LH.IWiaDevMgr2;
29632962
Result:=pDevMgr;
29642963
end;
29652964

2966-
procedure TWIAManager.SetEnumAll(AValue: Boolean);
2967-
begin
2968-
if (rEnumAll <> AValue) then
2969-
begin
2970-
rEnumAll:= AValue;
2971-
HasEnumerated:= EnumerateDevices;
2972-
end;
2973-
end;
2974-
29752965
function TWIAManager.GetSelectedDevice: TWIADevice;
29762966
begin
29772967
if (rSelectedDeviceIndex >= 0) and (rSelectedDeviceIndex < Length(rDeviceList))
@@ -2994,7 +2984,7 @@ function TWIAManager.GetDevice(Index: Integer): TWIADevice;
29942984
begin
29952985
//Enumerate devices if needed
29962986
if not(HasEnumerated)
2997-
then HasEnumerated:= EnumerateDevices;
2987+
then HasEnumerated:= EnumerateDevices(False);
29982988

29992989
if (Index >= 0) and (Index < Length(rDeviceList))
30002990
then Result:= rDeviceList[Index]
@@ -3005,7 +2995,7 @@ function TWIAManager.GetDevicesCount: Integer;
30052995
begin
30062996
//Enumerate devices if needed
30072997
if not(HasEnumerated)
3008-
then HasEnumerated:= EnumerateDevices;
2998+
then HasEnumerated:= EnumerateDevices(False);
30092999

30103000
Result:= Length(rDeviceList);
30113001
end;
@@ -3015,17 +3005,22 @@ function TWIAManager.CreateDevManager: IUnknown;
30153005
lres:= CoCreateInstance(CLSID_WiaDevMgr2, nil, CLSCTX_LOCAL_SERVER, IID_IWiaDevMgr2, Result);
30163006
end;
30173007

3018-
procedure TWIAManager.EmptyDeviceList(setZeroLength:Boolean);
3008+
procedure TWIAManager.EmptyDeviceList(PreserveSelected: Boolean);
30193009
var
30203010
i:Integer;
30213011

30223012
begin
30233013
for i:=Low(rDeviceList) to High(rDeviceList) do
3024-
if (rDeviceList[i]<>nil) then FreeAndNil(rDeviceList[i]);
3025-
if setZeroLength then SetLength(rDeviceList, 0);
3014+
if (rDeviceList[i] <> nil) then
3015+
begin
3016+
if not(PreserveSelected and (i = rSelectedDeviceIndex))
3017+
then FreeAndNil(rDeviceList[i]);
3018+
end;
3019+
3020+
rDeviceList:= nil;
30263021
end;
30273022

3028-
function TWIAManager.EnumerateDevices: Boolean;
3023+
function TWIAManager.EnumerateDevices(PreserveSelected: Boolean): Boolean;
30293024
var
30303025
i:integer;
30313026
ppIEnum: IEnumWIA_DEV_INFO;
@@ -3036,11 +3031,16 @@ function TWIAManager.EnumerateDevices: Boolean;
30363031
//pPropNames: array [0..3] of LPOLESTR;
30373032
pPropSpec: array [0..4] of PROPSPEC;
30383033
pPropVar: array [0..4] of PROPVARIANT;
3034+
lastSelected: TWIADevice;
30393035

30403036
begin
30413037
Result :=False;
3042-
//SetLength(rDeviceList, 0);
3043-
EmptyDeviceList(True);
3038+
3039+
if PreserveSelected
3040+
then lastSelected:= GetSelectedDevice
3041+
else lastSelected:= nil;
3042+
3043+
EmptyDeviceList(PreserveSelected);
30443044

30453045
try
30463046
if (pDevMgr = nil)
@@ -3059,12 +3059,9 @@ function TWIAManager.EnumerateDevices: Boolean;
30593059
if (lres<>S_OK)
30603060
then Exception.Create('Number of WIA Devices not available');
30613061

3062-
EmptyDeviceList(True);
3063-
30643062
if (devCount > 0) then
30653063
begin
30663064
SetLength(rDeviceList, devCount);
3067-
//EmptyDeviceList(False);
30683065

30693066
// Define which properties you want to read:
30703067
// Device ID. This is what you would use to create
@@ -3111,7 +3108,15 @@ function TWIAManager.EnumerateDevices: Boolean;
31113108
// lres := pWiaPropertyStorage.ReadPropertyNames(Length(pPropIDS), @pPropIDS, @pPropNames);
31123109

31133110
if (VT_BSTR = pPropVar[0].vt)
3114-
then rDeviceList[i] :=TWIADevice.Create(Self, i, pPropVar[0].bstrVal)
3111+
then begin
3112+
if (lastSelected <> nil) and (lastSelected.ID = pPropVar[0].bstrVal)
3113+
then begin
3114+
rDeviceList[i]:= lastSelected;
3115+
rSelectedDeviceIndex:= i;
3116+
lastSelected.rIndex:= i; //Update Index because can be different (Actually not used)
3117+
end
3118+
else rDeviceList[i] :=TWIADevice.Create(Self, i, pPropVar[0].bstrVal);
3119+
end
31153120
else Exception.Create('ID of Device '+IntToStr(i)+' not String');
31163121

31173122
if (VT_BSTR = pPropVar[1].vt)
@@ -3146,7 +3151,7 @@ function TWIAManager.EnumerateDevices: Boolean;
31463151
end;
31473152

31483153
except
3149-
EmptyDeviceList(True);
3154+
EmptyDeviceList(PreserveSelected);
31503155
Result :=False;
31513156
end;
31523157
end;
@@ -3174,20 +3179,20 @@ constructor TWIAManager.Create(AEnumAll: Boolean);
31743179

31753180
destructor TWIAManager.Destroy;
31763181
begin
3177-
EmptyDeviceList(True);
3182+
EmptyDeviceList(False);
31783183
if (pDevMgr<>nil) then pDevMgr :=nil; //Free the Interface
31793184

31803185
inherited Destroy;
31813186
end;
31823187

31833188
procedure TWIAManager.ClearDeviceList;
31843189
begin
3185-
EmptyDeviceList(True);
3190+
EmptyDeviceList(False);
31863191
end;
31873192

3188-
procedure TWIAManager.RefreshDeviceList;
3193+
procedure TWIAManager.RefreshDeviceList(PreserveSelected: Boolean);
31893194
begin
3190-
HasEnumerated:= EnumerateDevices;
3195+
HasEnumerated:= EnumerateDevices(PreserveSelected);
31913196
end;
31923197

31933198
function TWIAManager.FindDevice(AID: String): Integer;

0 commit comments

Comments
 (0)