diff --git a/NTwain/TwainSession.cs b/NTwain/TwainSession.cs index 21419d0..b308b85 100644 --- a/NTwain/TwainSession.cs +++ b/NTwain/TwainSession.cs @@ -153,6 +153,7 @@ namespace NTwain /// /// Closes the TWAIN data source manager. + /// This is called when is invoked. /// public void Close() { @@ -284,6 +285,31 @@ namespace NTwain } } + /// + /// Attempts to show the current device's settings dialog if supported. + /// + /// + public STS ShowSettings() + { + TW_USERINTERFACE ui = default; + ui.hParent = _hWnd; + ui.ShowUI = 1; + return _twain.DatUserinterface(DG.CONTROL, MSG.ENABLEDSUIONLY, ref ui); + } + + /// + /// Begins the capture process on the current device. + /// + /// Whether to display settings UI. Not all devices support this. + /// + public STS StartCapture(bool showUI) + { + TW_USERINTERFACE ui = default; + ui.hParent = _hWnd; + ui.ShowUI = (ushort)(showUI ? 1 : 0); + return _twain.DatUserinterface(DG.CONTROL, MSG.ENABLEDS, ref ui); + } + #endregion } } diff --git a/samples/Net5Console/Net5Console.csproj b/samples/Net5Console/Net5Console.csproj index 0cfac99..cff0fa3 100644 --- a/samples/Net5Console/Net5Console.csproj +++ b/samples/Net5Console/Net5Console.csproj @@ -1,8 +1,9 @@ - + Exe net5.0-windows + diff --git a/samples/Net5Console/Program.cs b/samples/Net5Console/Program.cs index 020f415..98f8443 100644 --- a/samples/Net5Console/Program.cs +++ b/samples/Net5Console/Program.cs @@ -1,6 +1,8 @@ using NTwain; using System; using System.Reflection; +using System.Threading; +using TWAINWorkingGroup; namespace Net5Console { @@ -8,23 +10,71 @@ namespace Net5Console { static void Main(string[] args) { + Console.WriteLine("Starting twain test in console..."); + using (var twain = new TwainSession(Assembly.GetExecutingAssembly(), null, IntPtr.Zero)) + using (var hold = new ManualResetEventSlim()) { + twain.DeviceEvent += (sender, e) => + { + Console.WriteLine($"Got device event " + (TWDE)e.Event); + }; + twain.ScanEvent += (sender, closing) => + { + Console.WriteLine($"Got scan event " + closing); + + TW_USERINTERFACE twuserinterface = default; + if (twain.TWAIN.DatUserinterface(DG.CONTROL, MSG.DISABLEDS, ref twuserinterface) == STS.SUCCESS) + { + Console.WriteLine("Disabled device."); + } + else + { + Console.Error.WriteLine("Failed to disabled device."); + } + hold.Set(); + }; + if (twain.Open() == TWAINWorkingGroup.STS.SUCCESS) { Console.WriteLine("Opened DSM"); + Console.WriteLine("Default device:"); Console.WriteLine($"\t{twain.DefaultDevice}"); + Console.WriteLine("All devices:"); + TW_IDENTITY dsToUse = default; foreach (var dev in twain.GetDevices()) { Console.WriteLine($"\t{dev}"); + if (dev.ProductName == "TWAIN2 FreeImage Software Scanner") + { + dsToUse = dev; + } + } + + twain.CurrentDevice = dsToUse; + if (twain.CurrentDevice.HasValue) + { + Console.WriteLine("Current device after opening attempt:"); + Console.WriteLine($"\t{twain.CurrentDevice}"); + + var sts = twain.StartCapture(false); + if (sts == STS.SUCCESS) + { + Console.Error.WriteLine("Waiting for capture to complete."); + + while (!hold.IsSet) Thread.Sleep(100); + } + else + { + Console.Error.WriteLine("Failed to start capture: " + sts); + } + } + else + { + Console.WriteLine("No devices opened."); } - Console.WriteLine("Current device:"); - Console.WriteLine($"\t{twain.CurrentDevice}"); - twain.CurrentDevice = twain.DefaultDevice; - Console.WriteLine("Current device after setting:"); - Console.WriteLine($"\t{twain.CurrentDevice}"); } else { @@ -32,7 +82,7 @@ namespace Net5Console } Console.WriteLine("Test Ended"); } - + } } }