Added winform/wpf reg methods

This commit is contained in:
Eugene Wang
2023-04-02 21:45:48 -04:00
parent 16aaf25f10
commit 84251a6a5e
3 changed files with 49 additions and 9 deletions

View File

@@ -16,7 +16,7 @@ namespace WinForm32
{ {
InitializeComponent(); InitializeComponent();
Text += TwainPlatform.Is32bit ? " 32bit" : " 64bit"; Text += TwainPlatform.Is32bit ? " 32bit" : " 64bit";
TwainPlatform.PreferLegacyDSM = true; TwainPlatform.PreferLegacyDSM = false;
twain = new TwainSession(Assembly.GetExecutingAssembly().Location); twain = new TwainSession(Assembly.GetExecutingAssembly().Location);
twain.StateChanged += Twain_StateChanged; twain.StateChanged += Twain_StateChanged;
@@ -46,7 +46,7 @@ namespace WinForm32
var hwnd = this.Handle; var hwnd = this.Handle;
var rc = twain.OpenDSM(hwnd); var rc = twain.OpenDSM(hwnd);
Application.AddMessageFilter(twain); twain.AddWinformFilter();
Debug.WriteLine($"OpenDSM={rc}"); Debug.WriteLine($"OpenDSM={rc}");
} }
@@ -54,8 +54,7 @@ namespace WinForm32
{ {
var finalState = twain.TryStepdown(STATE.S2); var finalState = twain.TryStepdown(STATE.S2);
Debug.WriteLine($"Stepdown result state={finalState}"); Debug.WriteLine($"Stepdown result state={finalState}");
twain.RemoveWinformFilter();
Application.RemoveMessageFilter(twain);
base.OnClosing(e); base.OnClosing(e);
} }

View File

@@ -67,7 +67,7 @@ namespace NTwain
/// <returns></returns> /// <returns></returns>
public STS SetDefaultSource(TW_IDENTITY_LEGACY source) public STS SetDefaultSource(TW_IDENTITY_LEGACY source)
{ {
// TODO: this doesn't work??? // this doesn't work on windows legacy twain_32.dll
var rc = DGControl.Identity.Set(ref _appIdentity, ref source); var rc = DGControl.Identity.Set(ref _appIdentity, ref source);
if (rc == STS.SUCCESS) if (rc == STS.SUCCESS)

View File

@@ -7,6 +7,8 @@ using System.Diagnostics;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
using System.Threading; using System.Threading;
using System.Windows.Forms; using System.Windows.Forms;
using System.Windows.Interop;
using MSG = NTwain.Data.MSG;
namespace NTwain namespace NTwain
{ {
@@ -15,14 +17,53 @@ namespace NTwain
partial class TwainSession : IMessageFilter partial class TwainSession : IMessageFilter
{ {
// winform use with Application.AddMessageFilter(<TwainSession instance>) private HwndSource? _wpfhook;
/// <summary>
/// Registers this session for use in a Winform UI thread.
/// </summary>
public void AddWinformFilter()
{
Application.AddMessageFilter(this);
}
/// <summary>
/// Unregisters this session if previously registered with <see cref="AddWinformFilter"/>.
/// </summary>
public void RemoveWinformFilter()
{
Application.RemoveMessageFilter(this);
}
/// <summary>
/// Registers this session for use in a WPF UI thread.
/// </summary>
public void AddWpfHook()
{
if (_wpfhook == null)
{
_wpfhook = HwndSource.FromHwnd(_hwnd);
_wpfhook.AddHook(WpfHook);
}
}
/// <summary>
/// Unregisters this session if previously registered with <see cref="AddWpfHook"/>.
/// </summary>
public void RemoveWpfHook()
{
if (_wpfhook != null)
{
_wpfhook.RemoveHook(WpfHook);
_wpfhook = null;
}
}
bool System.Windows.Forms.IMessageFilter.PreFilterMessage(ref System.Windows.Forms.Message m) bool System.Windows.Forms.IMessageFilter.PreFilterMessage(ref System.Windows.Forms.Message m)
{ {
return CheckIfTwainMessage(m.HWnd, m.Msg, m.WParam, m.LParam); return CheckIfTwainMessage(m.HWnd, m.Msg, m.WParam, m.LParam);
} }
// wpf use // wpf use with HwndSource.FromHwnd(Handle).AddHook(
IntPtr HwndSourceHook(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled) IntPtr WpfHook(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
{ {
handled = CheckIfTwainMessage(hwnd, msg, wParam, lParam); handled = CheckIfTwainMessage(hwnd, msg, wParam, lParam);
return IntPtr.Zero; return IntPtr.Zero;
@@ -32,7 +73,7 @@ namespace NTwain
{ {
// this handles the message from a typical WndProc message loop and check if it's from the TWAIN source. // this handles the message from a typical WndProc message loop and check if it's from the TWAIN source.
bool handled = false; bool handled = false;
if (_state >= Data.STATE.S5) if (_state >= STATE.S5)
{ {
TW_EVENT evt = default; TW_EVENT evt = default;
try try