mirror of
https://github.com/soukoku/ntwain.git
synced 2026-01-09 11:21:06 +08:00
Added old console sample to test diy message pump.
This commit is contained in:
15
NTwain.sln
15
NTwain.sln
@@ -28,6 +28,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "WinForm64", "samples\WinFor
|
||||
EndProject
|
||||
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "csizes", "csizes\csizes.vcxproj", "{1AABD2DC-3F81-4301-938B-3EC2EDEF38D4}"
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WinConsole32", "samples\WinConsole32\WinConsole32.csproj", "{4E2417E7-FDC3-46D7-B976-84A97B500B74}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
Debug|Any CPU = Debug|Any CPU
|
||||
@@ -86,6 +88,18 @@ Global
|
||||
{1AABD2DC-3F81-4301-938B-3EC2EDEF38D4}.Release|x64.Build.0 = Release|x64
|
||||
{1AABD2DC-3F81-4301-938B-3EC2EDEF38D4}.Release|x86.ActiveCfg = Release|Win32
|
||||
{1AABD2DC-3F81-4301-938B-3EC2EDEF38D4}.Release|x86.Build.0 = Release|Win32
|
||||
{4E2417E7-FDC3-46D7-B976-84A97B500B74}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{4E2417E7-FDC3-46D7-B976-84A97B500B74}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{4E2417E7-FDC3-46D7-B976-84A97B500B74}.Debug|x64.ActiveCfg = Debug|Any CPU
|
||||
{4E2417E7-FDC3-46D7-B976-84A97B500B74}.Debug|x64.Build.0 = Debug|Any CPU
|
||||
{4E2417E7-FDC3-46D7-B976-84A97B500B74}.Debug|x86.ActiveCfg = Debug|Any CPU
|
||||
{4E2417E7-FDC3-46D7-B976-84A97B500B74}.Debug|x86.Build.0 = Debug|Any CPU
|
||||
{4E2417E7-FDC3-46D7-B976-84A97B500B74}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{4E2417E7-FDC3-46D7-B976-84A97B500B74}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{4E2417E7-FDC3-46D7-B976-84A97B500B74}.Release|x64.ActiveCfg = Release|Any CPU
|
||||
{4E2417E7-FDC3-46D7-B976-84A97B500B74}.Release|x64.Build.0 = Release|Any CPU
|
||||
{4E2417E7-FDC3-46D7-B976-84A97B500B74}.Release|x86.ActiveCfg = Release|Any CPU
|
||||
{4E2417E7-FDC3-46D7-B976-84A97B500B74}.Release|x86.Build.0 = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
@@ -93,6 +107,7 @@ Global
|
||||
GlobalSection(NestedProjects) = preSolution
|
||||
{7792A94E-D0B4-440D-8BD5-CA1CA548782C} = {707B4313-8EF8-4D0F-A95E-590783422187}
|
||||
{C9666CB2-C9A6-48C8-AB51-D616A48058A7} = {707B4313-8EF8-4D0F-A95E-590783422187}
|
||||
{4E2417E7-FDC3-46D7-B976-84A97B500B74} = {707B4313-8EF8-4D0F-A95E-590783422187}
|
||||
EndGlobalSection
|
||||
GlobalSection(ExtensibilityGlobals) = postSolution
|
||||
SolutionGuid = {7459323B-44F6-4E07-8574-E1B4B525086B}
|
||||
|
||||
@@ -37,10 +37,6 @@ names and the spec pdf.
|
||||
|
||||
* All lower-level TWAIN APIs are public instead of hidden away.
|
||||
|
||||
* `TwainAppSession` longer hosts its own internal message pump. Apps will need to hook
|
||||
into their UI message loop on Windows (supports Winform and WPF at the moment).
|
||||
|
||||
|
||||
## Using the lib
|
||||
|
||||
Before using this lib, you are required to be reasonably
|
||||
|
||||
106
samples/WinConsole32/Program.cs
Normal file
106
samples/WinConsole32/Program.cs
Normal file
@@ -0,0 +1,106 @@
|
||||
using NTwain;
|
||||
using NTwain.Data;
|
||||
using System.Diagnostics;
|
||||
|
||||
namespace WinConsole32
|
||||
{
|
||||
internal class Program
|
||||
{
|
||||
static async Task Main(string[] args)
|
||||
{
|
||||
var libVer = FileVersionInfo.GetVersionInfo(typeof(TwainAppSession).Assembly.Location).ProductVersion;
|
||||
Console.WriteLine($"Console sample {(TWPlatform.Is32bit ? " 32bit" : " 64bit")} on NTwain {libVer}");
|
||||
|
||||
MessagePumpThread pump = new MessagePumpThread();
|
||||
TwainAppSession session = new TwainAppSession(Environment.ProcessPath!);
|
||||
|
||||
session.StateChanged += Session_StateChanged;
|
||||
session.SourceDisabled += Session_SourceDisabled1;
|
||||
session.Transferred += Session_Transferred;
|
||||
|
||||
var sts = await pump.AttachAsync(session);
|
||||
|
||||
if (sts.IsSuccess)
|
||||
{
|
||||
Console.WriteLine("Available data sources:");
|
||||
|
||||
TW_IDENTITY_LEGACY firstSrc = default;
|
||||
foreach (var src in session.GetSources())
|
||||
{
|
||||
if (!firstSrc.HasValue) firstSrc = src;
|
||||
Console.WriteLine($"\t{src}");
|
||||
}
|
||||
Console.WriteLine();
|
||||
|
||||
var defaultSrc = session.DefaultSource;
|
||||
Console.WriteLine($"Default data source = {defaultSrc}");
|
||||
Console.WriteLine();
|
||||
|
||||
session.ShowUserSelect();
|
||||
Console.WriteLine($"Selected data source = {session.DefaultSource}");
|
||||
Console.WriteLine();
|
||||
|
||||
var targetSrc = defaultSrc.HasValue ? defaultSrc : firstSrc;
|
||||
|
||||
if (targetSrc.HasValue)
|
||||
{
|
||||
TestThisSource(session, targetSrc);
|
||||
}
|
||||
else
|
||||
{
|
||||
Console.WriteLine("No data source to test.");
|
||||
Console.WriteLine();
|
||||
}
|
||||
|
||||
Console.WriteLine("---------------------------------------------");
|
||||
Console.WriteLine("Test in progress, press Enter to stop testing");
|
||||
Console.WriteLine("---------------------------------------------");
|
||||
Console.ReadLine();
|
||||
session.TryStepdown(STATE.S1);
|
||||
}
|
||||
else
|
||||
{
|
||||
Console.WriteLine("Failed to attach: " + sts);
|
||||
}
|
||||
|
||||
Console.WriteLine("-------------------");
|
||||
Console.WriteLine("Press Enter to exit");
|
||||
Console.WriteLine("-------------------");
|
||||
Console.ReadLine();
|
||||
}
|
||||
|
||||
private static void Session_Transferred(TwainAppSession sender, TransferredEventArgs e)
|
||||
{
|
||||
if (e.Data != null)
|
||||
{
|
||||
Console.WriteLine("SUCCESS! Got twain data on thread {0}.", Environment.CurrentManagedThreadId);
|
||||
}
|
||||
else
|
||||
{
|
||||
Console.WriteLine("BUMMER! No twain data on thread {0}.", Environment.CurrentManagedThreadId);
|
||||
}
|
||||
e.Dispose();
|
||||
}
|
||||
|
||||
private static void Session_StateChanged(TwainAppSession sender, STATE e)
|
||||
{
|
||||
Console.WriteLine($"Session state changed to {sender.State}");
|
||||
}
|
||||
|
||||
private static void Session_SourceDisabled1(TwainAppSession sender, TW_IDENTITY_LEGACY e)
|
||||
{
|
||||
Console.WriteLine($"Session source disabled.");
|
||||
sender.CloseSource();
|
||||
}
|
||||
|
||||
private static void TestThisSource(TwainAppSession session, TW_IDENTITY_LEGACY source)
|
||||
{
|
||||
Console.WriteLine($"Testing data source {source}");
|
||||
Console.WriteLine();
|
||||
|
||||
session.OpenSource(source);
|
||||
|
||||
var rc = session.EnableSource(true, false);
|
||||
}
|
||||
}
|
||||
}
|
||||
15
samples/WinConsole32/WinConsole32.csproj
Normal file
15
samples/WinConsole32/WinConsole32.csproj
Normal file
@@ -0,0 +1,15 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net7.0-windows</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<PlatformTarget>x86</PlatformTarget>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\src\NTwain\NTwain.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
Reference in New Issue
Block a user