Added ProcessEvent triplet call.

This commit is contained in:
Eugene Wang
2018-11-18 09:58:38 -05:00
parent 0605610665
commit ec153e6f3d
6 changed files with 103 additions and 25 deletions

View File

@@ -190,8 +190,8 @@ namespace NTwain.Data
[StructLayout(LayoutKind.Sequential, Pack = 2)]
partial struct TW_EVENT
{
TW_MEMREF _pEvent;
TW_UINT16 _tWMessage;
public TW_MEMREF pEvent;
public TW_UINT16 TWMessage;
}
[StructLayout(LayoutKind.Sequential, Pack = 2)]

View File

@@ -1179,29 +1179,7 @@ namespace NTwain.Data
// /// </summary>
// internal const int ItemOffset = 14;
// }
/// <summary>
/// Used on Windows and Macintosh pre OS X to pass application events/messages from the
/// application to the Source.
/// </summary>
public partial struct TW_EVENT
{
/// <summary>
/// A pointer to the event/message to be examined by the Source.
/// Under Microsoft Windows, pEvent is a pMSG (pointer to a Microsoft
/// Windows MSG struct). That is, the message the application received from
/// GetMessage(). On the Macintosh, pEvent is a pointer to an EventRecord.
/// </summary>
public IntPtr Event { get { return _pEvent; } set { _pEvent = value; } }
/// <summary>
/// Any message the Source needs to send to the application in
/// response to processing the event/message. The messages currently defined for
/// this purpose are <see cref="Message.Null"/>, <see cref="Message.XferReady"/>
/// and <see cref="Message.CloseDSReq"/>.
/// </summary>
public Message TWMessage { get { return (Message)_tWMessage; } }
}
// /// <summary>
// /// This structure is used to pass specific information between the data source and the application

View File

@@ -0,0 +1,34 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
namespace NTwain.Internals
{
/// <summary>
/// The MSG structure in Windows for TWAIN use.
/// </summary>
[StructLayout(LayoutKind.Sequential)]
struct MSG
{
public MSG(IntPtr hwnd, int message, IntPtr wParam, IntPtr lParam)
{
_hwnd = hwnd;
_message = (uint)message;
_wParam = wParam;
_lParam = lParam;
_time = 0;
_x = 0;
_y = 0;
}
IntPtr _hwnd;
uint _message;
IntPtr _wParam;
IntPtr _lParam;
uint _time;
int _x;
int _y;
}
}

View File

@@ -0,0 +1,22 @@
using NTwain.Data;
using NTwain.Internals;
namespace NTwain.Triplets.Control
{
sealed class Event : BaseTriplet
{
internal Event(TwainSession session) : base(session) { }
public ReturnCode ProcessEvent(ref TW_EVENT evt)
{
if (Is32Bit)
{
return NativeMethods.DsmWin32(Session.Config.App32, Session.CurrentSource.Identity32,
DataGroups.Control, DataArgumentType.Event, Message.ProcessEvent, ref evt);
}
return NativeMethods.DsmWin64(Session.Config.App32, Session.CurrentSource.Identity32,
DataGroups.Control, DataArgumentType.Event, Message.ProcessEvent, ref evt);
}
}
}

View File

@@ -42,6 +42,9 @@ namespace NTwain.Triplets
UserInterface _ui;
internal UserInterface UserInterface => _ui ?? (_ui = new UserInterface(Session));
Event _event;
internal Event Event => _event ?? (_event = new Event(Session));
XferGroup _xferGroup;
/// <summary>
/// Gets the operations defined for DAT_XFERGROUP.

View File

@@ -1,4 +1,5 @@
using NTwain.Data;
using NTwain.Internals;
using NTwain.Triplets;
using System;
using System.Collections.Generic;
@@ -15,6 +16,46 @@ namespace NTwain
{
internal TW_USERINTERFACE _lastEnableUI;
/// <summary>
/// If on Windows pass all messages from WndProc here to handle it
/// for TWAIN-specific things.
/// </summary>
/// <param name="hwnd">The window handle.</param>
/// <param name="msg">The message.</param>
/// <param name="wParam">The w parameter.</param>
/// <param name="lParam">The l parameter.</param>
/// <returns>
/// true if handled by TWAIN.
/// </returns>
public bool HandleWndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam)
{
var handled = false;
if (State > TwainState.S4)
{
var winMsg = new MSG(hwnd, msg, wParam, lParam);
// transform it into a pointer for twain
IntPtr msgPtr = IntPtr.Zero;
try
{
msgPtr = Config.MemoryManager.Allocate((uint)Marshal.SizeOf(winMsg));
IntPtr locked = Config.MemoryManager.Lock(msgPtr);
Marshal.StructureToPtr(winMsg, locked, false);
TW_EVENT evt = new TW_EVENT { pEvent = locked };
if (handled = DGControl.Event.ProcessEvent(ref evt) == ReturnCode.DSEvent)
{
HandleSourceMsg((Message)evt.TWMessage);
}
}
finally
{
if (msgPtr != IntPtr.Zero) { Config.MemoryManager.Free(msgPtr); }
}
}
return handled;
}
private void HandleSourceMsg(Message msg)
{
switch (msg)