Added cap query support example.

This commit is contained in:
soukoku
2014-04-20 22:24:05 -04:00
parent a766370930
commit 31f77edb40
11 changed files with 76 additions and 44 deletions

View File

@@ -85,15 +85,15 @@
<Compile Include="..\NTwain\Internals\MessageLoop.cs"> <Compile Include="..\NTwain\Internals\MessageLoop.cs">
<Link>Internals\MessageLoop.cs</Link> <Link>Internals\MessageLoop.cs</Link>
</Compile> </Compile>
<Compile Include="..\NTwain\Internals\NativeMethods.cs">
<Link>Internals\NativeMethods.cs</Link>
</Compile>
<Compile Include="..\NTwain\Internals\TentativeStateCommitable.cs"> <Compile Include="..\NTwain\Internals\TentativeStateCommitable.cs">
<Link>Internals\TentativeStateCommitable.cs</Link> <Link>Internals\TentativeStateCommitable.cs</Link>
</Compile> </Compile>
<Compile Include="..\NTwain\Internals\TransferLogic.cs"> <Compile Include="..\NTwain\Internals\TransferLogic.cs">
<Link>Internals\TransferLogic.cs</Link> <Link>Internals\TransferLogic.cs</Link>
</Compile> </Compile>
<Compile Include="..\NTwain\Internals\UnsafeNativeMethods.cs">
<Link>Internals\UnsafeNativeMethods.cs</Link>
</Compile>
<Compile Include="..\NTwain\Internals\WindowsHook.cs"> <Compile Include="..\NTwain\Internals\WindowsHook.cs">
<Link>Internals\WindowsHook.cs</Link> <Link>Internals\WindowsHook.cs</Link>
</Compile> </Compile>

View File

@@ -2037,18 +2037,20 @@ namespace NTwain.Data
/// Bit mask for querying the operation that are supported by the data source on a capability. /// Bit mask for querying the operation that are supported by the data source on a capability.
/// Corresponds to TWQC_*. /// Corresponds to TWQC_*.
/// </summary> /// </summary>
public static class QuerySupportMask [Flags]
public enum QuerySupport
{ {
public const int Get = 0x1; None = 0,
public const int Set = 0x2; Get = 0x1,
public const int GetDefault = 0x4; Set = 0x2,
public const int GetCurrent = 0x8; GetDefault = 0x4,
public const int Reset = 0x10; GetCurrent = 0x8,
public const int SetConstraint = 0x20; Reset = 0x10,
public const int Constrainable = 0x40; SetConstraint = 0x20,
public const int GetHelp = 0x100; Constrainable = 0x40,
public const int GetLabel = 0x200; GetHelp = 0x100,
public const int GetLabelEnum = 0x400; GetLabel = 0x200,
GetLabelEnum = 0x400
} }
/// <summary> /// <summary>

View File

@@ -3,9 +3,6 @@ using System.Collections.Generic;
namespace NTwain.Internals namespace NTwain.Internals
{ {
/// <summary>
/// Internal interface for state management.
/// </summary>
interface ITwainSessionInternal : ITwainSession interface ITwainSessionInternal : ITwainSession
{ {
/// <summary> /// <summary>

View File

@@ -1,26 +1,26 @@
using System; using System;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
using System.Security;
namespace NTwain.Internals namespace NTwain.Internals
{ {
static class NativeMethods [SuppressUnmanagedCodeSecurity]
static class UnsafeNativeMethods
{ {
// should be unsafe native methods?
#region mem stuff for twain 1.x #region mem stuff for twain 1.x
[DllImport("kernel32", SetLastError = true, EntryPoint = "GlobalAlloc")] [DllImport("kernel32", SetLastError = true, EntryPoint = "GlobalAlloc")]
public static extern IntPtr WinGlobalAlloc(uint uFlags, UIntPtr dwBytes); internal static extern IntPtr WinGlobalAlloc(uint uFlags, UIntPtr dwBytes);
[DllImport("kernel32", SetLastError = true, EntryPoint = "GlobalFree")] [DllImport("kernel32", SetLastError = true, EntryPoint = "GlobalFree")]
public static extern IntPtr WinGlobalFree(IntPtr hMem); internal static extern IntPtr WinGlobalFree(IntPtr hMem);
[DllImport("kernel32", SetLastError = true, EntryPoint = "GlobalLock")] [DllImport("kernel32", SetLastError = true, EntryPoint = "GlobalLock")]
public static extern IntPtr WinGlobalLock(IntPtr handle); internal static extern IntPtr WinGlobalLock(IntPtr handle);
[DllImport("kernel32", SetLastError = true, EntryPoint = "GlobalUnlock")] [DllImport("kernel32", SetLastError = true, EntryPoint = "GlobalUnlock")]
[return: MarshalAs(UnmanagedType.Bool)] [return: MarshalAs(UnmanagedType.Bool)]
public static extern bool WinGlobalUnlock(IntPtr handle); internal static extern bool WinGlobalUnlock(IntPtr handle);
#endregion #endregion
} }

View File

@@ -13,7 +13,7 @@ namespace NTwain
{ {
public IntPtr Allocate(uint size) public IntPtr Allocate(uint size)
{ {
IntPtr retVal = NativeMethods.WinGlobalAlloc(0x0040, new UIntPtr(size)); IntPtr retVal = UnsafeNativeMethods.WinGlobalAlloc(0x0040, new UIntPtr(size));
if (retVal == IntPtr.Zero) if (retVal == IntPtr.Zero)
{ {
@@ -24,17 +24,17 @@ namespace NTwain
public void Free(IntPtr handle) public void Free(IntPtr handle)
{ {
NativeMethods.WinGlobalFree(handle); UnsafeNativeMethods.WinGlobalFree(handle);
} }
public IntPtr Lock(IntPtr handle) public IntPtr Lock(IntPtr handle)
{ {
return NativeMethods.WinGlobalLock(handle); return UnsafeNativeMethods.WinGlobalLock(handle);
} }
public void Unlock(IntPtr handle) public void Unlock(IntPtr handle)
{ {
NativeMethods.WinGlobalUnlock(handle); UnsafeNativeMethods.WinGlobalUnlock(handle);
} }
} }
} }

View File

@@ -69,7 +69,7 @@
<Compile Include="ITwainState.cs" /> <Compile Include="ITwainState.cs" />
<Compile Include="Internals\WinMemoryManager.cs" /> <Compile Include="Internals\WinMemoryManager.cs" />
<Compile Include="Internals\MessageLoop.cs" /> <Compile Include="Internals\MessageLoop.cs" />
<Compile Include="Internals\NativeMethods.cs" /> <Compile Include="Internals\UnsafeNativeMethods.cs" />
<Compile Include="Platform.cs" /> <Compile Include="Platform.cs" />
<Compile Include="Properties\Resources.Designer.cs"> <Compile Include="Properties\Resources.Designer.cs">
<AutoGen>True</AutoGen> <AutoGen>True</AutoGen>

View File

@@ -64,7 +64,7 @@ namespace NTwain
} }
static readonly WinMemoryManager _defaultMemManager; static readonly IMemoryManager _defaultMemManager;
static IMemoryManager _specifiedMemManager; static IMemoryManager _specifiedMemManager;
/// <summary> /// <summary>

View File

@@ -6,8 +6,8 @@ using System.Linq;
namespace NTwain namespace NTwain
{ {
/// <summary> /// <summary>
/// Defines common methods on <see cref="TwainSession"/> using the raw /// Defines useful methods on <see cref="TwainSession"/> without having to dive into
/// TWAIN triplet api. /// the raw TWAIN triplet API.
/// </summary> /// </summary>
public static class TwainSessionExtensions public static class TwainSessionExtensions
{ {
@@ -68,6 +68,33 @@ namespace NTwain
#region caps routines #region caps routines
/// <summary>
/// Gets the actual supported operations for a capability.
/// </summary>
/// <param name="session">The session.</param>
/// <param name="capId">The cap identifier.</param>
/// <returns></returns>
public static QuerySupport GetCapSupport(this ITwainOperation session, CapabilityId capId)
{
if (session == null) { throw new ArgumentNullException("session"); }
QuerySupport retVal = QuerySupport.None;
using (TWCapability cap = new TWCapability(capId))
{
var rc = session.DGControl.Capability.QuerySupport(cap);
if (rc == ReturnCode.Success)
{
var read = CapReadOut.ReadValue(cap);
if (read.ContainerType == ContainerType.OneValue)
{
retVal = read.OneValue.ConvertToEnum<QuerySupport>();
}
}
}
return retVal;
}
/// <summary> /// <summary>
/// Gets the current value for a capability. /// Gets the current value for a capability.
/// </summary> /// </summary>

View File

@@ -25,17 +25,28 @@
<DataTemplate> <DataTemplate>
<StackPanel> <StackPanel>
<TextBlock Text="{Binding Name}" TextWrapping="Wrap"></TextBlock> <TextBlock Text="{Binding Name}" TextWrapping="Wrap"></TextBlock>
<TextBlock Text="{Binding Version, StringFormat='Version {0}'}" TextWrapping="Wrap" Foreground="{DynamicResource ModernForeground2}"/> <TextBlock Text="{Binding Version, StringFormat='Version {0}'}" TextWrapping="Wrap" Foreground="{DynamicResource ModernForeground2}"
<TextBlock Text="{Binding Protocol, StringFormat='TWAIN {0}'}" Foreground="{DynamicResource ModernForeground2}"/> Margin="8 0 0 0"/>
<TextBlock Text="{Binding Protocol, StringFormat='TWAIN {0}'}" Foreground="{DynamicResource ModernForeground2}"
Margin="8 0 0 0"/>
</StackPanel> </StackPanel>
</DataTemplate> </DataTemplate>
</ListBox.ItemTemplate> </ListBox.ItemTemplate>
</ListBox> </ListBox>
<Label Content="Caps" Grid.Column="1"></Label> <Label Content="Caps" Grid.Column="1"></Label>
<ListBox x:Name="CapList" Grid.Row="1" Grid.Column="1" MinWidth="100" <ListBox x:Name="CapList" Grid.Row="1" Grid.Column="1" Width="150"
SelectionChanged="CapList_SelectionChanged" SelectionChanged="CapList_SelectionChanged"
Style="{StaticResource AppListBox}"></ListBox> Style="{StaticResource AppListBox}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Text="{Binding Name}" ></TextBlock>
<TextBlock Text="{Binding Supports}" TextWrapping="Wrap" Foreground="{DynamicResource ModernForeground2}"
FontStyle="Italic"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate></ListBox>
<Label Content="Cap values" Grid.Column="2"></Label> <Label Content="Cap values" Grid.Column="2"></Label>
<ListBox x:Name="CapDetailList" Grid.Row="1" Grid.Column="2" MinWidth="100" <ListBox x:Name="CapDetailList" Grid.Row="1" Grid.Column="2" MinWidth="100"

View File

@@ -92,7 +92,8 @@ namespace Tester.WPF
{ {
var caps = _twainVM.SupportedCaps.Select(o => new CapVM var caps = _twainVM.SupportedCaps.Select(o => new CapVM
{ {
Cap = o Cap = o,
Supports = _twainVM.GetCapSupport(o)
}).OrderBy(o => o.Name).ToList(); }).OrderBy(o => o.Name).ToList();
CapList.ItemsSource = caps; CapList.ItemsSource = caps;
} }

View File

@@ -21,13 +21,7 @@ namespace Tester.WPF
} }
} }
//public void SupportedOperations public QuerySupport Supports { get; set; }
//{
// get
// {
// }
//}
public override string ToString() public override string ToString()
{ {