-
Notifications
You must be signed in to change notification settings - Fork 0
/
Event.cs
122 lines (105 loc) · 3.07 KB
/
Event.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using ESolutions.Win32;
namespace ESolutions.Drawing.Imaging.Twain
{
internal class Event
{
#region InteropStruct
/// <summary>
/// Data structure which is passed to the twain_32.dll
/// </summary>
private EventInterop interopStruct;
#endregion
#region WindowsMessage
/// <summary>
/// The windows message that shall be passed to the twain_32.dll
/// </summary>
private ESolutions.Win32.WindowsMsg windowsMessage;
#endregion
#region DataSourceManager
/// <summary>
/// Datasourcemanager controlling the current twain session
/// </summary>
private DataSourceManager dataSourceManager;
#endregion
#region DataSource
/// <summary>
/// Data source which shall process the window message
/// </summary>
private DataSource dataSource;
#endregion
#region Event
/// <summary>
/// Constructor taking the datasourcemanager and the datasource which shall process a windows message
/// </summary>
/// <param name="dataSourceManager"></param>
/// <param name="dataSource"></param>
internal Event (DataSourceManager dataSourceManager, DataSource dataSource)
{
interopStruct = new EventInterop ();
interopStruct.WindowsMessagePointer = Marshal.AllocHGlobal (Marshal.SizeOf (windowsMessage));
windowsMessage = new WindowsMsg ();
this.dataSourceManager = dataSourceManager;
this.dataSource = dataSource;
}
#endregion
#region ~Event
/// <summary>
/// Destructor
/// </summary>
~Event ()
{
Marshal.FreeHGlobal (interopStruct.WindowsMessagePointer);
}
#endregion
#region ProcessMessage
/// <summary>
/// Processes window message and indicates wether it is a twain event or not
/// </summary>
/// <param name="m">Message send to the application</param>
/// <returns>The translates TwainMessage if suitable</returns>
internal Messages ProcessMessage (ref System.Windows.Forms.Message m)
{
Messages result = Messages.Null;
if (dataSource.IsOpen)
{
int pos = User32.GetMessagePos ();
windowsMessage.hwnd = m.HWnd;
windowsMessage.message = m.Msg;
windowsMessage.wParam = m.WParam;
windowsMessage.lParam = m.LParam;
windowsMessage.time = User32.GetMessageTime ();
windowsMessage.pt_x = (short) pos;
windowsMessage.pt_y = (short) (pos >> 16);
Marshal.StructureToPtr (windowsMessage, interopStruct.WindowsMessagePointer, false);
interopStruct.Message = 0;
ReturnCodes rc = Twain32.ProcessMessage (dataSourceManager.Identity, dataSource.Identity, ref interopStruct);
if (rc == ReturnCodes.DataSourceEvent)
{
result = (Messages) interopStruct.Message;
}
}
return result;
}
#endregion
}
[StructLayout (LayoutKind.Sequential, Pack = 2)]
internal struct EventInterop
{
#region WindowsMessagePointer
/// <summary>
/// Pointer to a MSG Structure
/// </summary>
public IntPtr WindowsMessagePointer;
#endregion
#region Message
/// <summary>
/// Reply of the twain driver
/// </summary>
public short Message;
#endregion
}
}