-
Notifications
You must be signed in to change notification settings - Fork 1
/
EHRText.cs
233 lines (191 loc) · 7.84 KB
/
EHRText.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
// Default C# Class Imports
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
// Additional Imports used by the EHR Monitor Test
using System.Runtime.InteropServices;
using System.Threading.Tasks;
using System.IO;
namespace EHR_Monitor_Test
{
public class EHRText
{
private const uint WM_SETTEXT = 0x000C;
private const uint WM_GETTEXT = 0x000D;
private const uint WM_GETTEXTLENGTH = 0x000E;
private const uint WM_USER = 0x0400;
private const uint WM_PASTE = 0x0302;
private const uint WM_COPY = 0x0301;
private const uint EM_SETSEL = 0x00B1;
private const uint EM_STREAMOUT = WM_USER + 74;
private const uint SF_RTF = 2;
[DllImport("user32.dll", EntryPoint="GetForegroundWindow")]
private static extern IntPtr GetForegroundWindow();
[DllImport("user32.dll", EntryPoint="GetWindowThreadProcessId", SetLastError=true)]
private static extern IntPtr GetWindowThreadProcessId(IntPtr hWnd, out IntPtr lpdwProcessId);
[DllImport("user32.dll", EntryPoint="GetGUIThreadInfo", SetLastError=true)]
private static extern bool GetGUIThreadInfo(IntPtr hThreadID, ref GUITHREADINFO lpgui);
[DllImport("user32.dll", EntryPoint="SendMessage", CharSet=CharSet.Auto)]
private static extern IntPtr SendMessage(IntPtr hWnd, uint msg, IntPtr wParam, [Out] StringBuilder lParam);
[DllImport("user32.dll", EntryPoint="SendMessage", CharSet=CharSet.Auto)]
private static extern IntPtr SendMessage(IntPtr hWnd, uint msg, uint wParam, [Out] string lParam);
[DllImport("user32.dll", EntryPoint = "SendMessage", CharSet = CharSet.Auto)]
private static extern IntPtr SendMessage(IntPtr hWnd, uint msg, uint wParam, ref EDITSTREAM lParam);
[DllImport("user32.dll", EntryPoint = "SendMessage", CharSet = CharSet.Auto)]
private static extern IntPtr SendMessage(IntPtr hWnd, uint msg, uint wParam, int lParam);
private delegate int EditStreamCallback(MemoryStream dwCookie, IntPtr pbBuff, int cb, out int pcb);
private static int EditStreamProc(MemoryStream dwCookie, IntPtr pbBuff, int cb, out int pcb)
{
pcb = cb;
byte[] buffer = new byte[cb];
Marshal.Copy(pbBuff, buffer, 0, cb);
dwCookie.Write(buffer, 0, cb);
return 0;
}
[StructLayout(LayoutKind.Sequential)]
private struct EDITSTREAM
{
public MemoryStream dwCookie;
public int dwError;
public EditStreamCallback pfnCallback;
}
private struct RECT
{
public int iLeft;
public int iTop;
public int iRight;
public int iBottom;
}
private struct GUITHREADINFO
{
public int cbSize;
public int flags;
public IntPtr hwndActive;
public IntPtr hwndFocus;
public IntPtr hwndCapture;
public IntPtr hwndMenuOwner;
public IntPtr hwndMoveSize;
public IntPtr hwndCaret;
public RECT rectCaret;
}
private static void SetText(IntPtr handle, string text)
{
SendMessage(handle, WM_SETTEXT, 0, text);
}
// Gets text text from a control by it's handle.
private static string GetText(IntPtr handle)
{
// Gets the text length.
var length = (int)SendMessage(handle, WM_GETTEXTLENGTH, IntPtr.Zero, null);
// Init the string builder to hold the text.
var sb = new StringBuilder(length + 1);
// Writes the text from the handle into the StringBuilder
SendMessage(handle, WM_GETTEXT, (IntPtr)sb.Capacity, sb);
// Return the text as a string.
return sb.ToString();
}
public static string GetTextFromActiveWindowElement()
{
try
{
IntPtr windowHWnd = GetForegroundWindow();
IntPtr lpdwProcessId;
IntPtr threadId = GetWindowThreadProcessId(windowHWnd, out lpdwProcessId);
GUITHREADINFO lpgui = new GUITHREADINFO();
lpgui.cbSize = Marshal.SizeOf(lpgui);
GetGUIThreadInfo(threadId, ref lpgui);
return GetText(lpgui.hwndFocus);
}
catch (Exception e)
{
Console.Write(e.Message);
}
return string.Empty;
}
public static void SetTextOfActiveWindowElement(string text)
{
try
{
IntPtr windowHWnd = GetForegroundWindow();
IntPtr lpdwProcessId;
IntPtr threadId = GetWindowThreadProcessId(windowHWnd, out lpdwProcessId);
GUITHREADINFO lpgui = new GUITHREADINFO();
lpgui.cbSize = Marshal.SizeOf(lpgui);
GetGUIThreadInfo(threadId, ref lpgui);
SetText(lpgui.hwndFocus, text);
}
catch (Exception e)
{
Console.Write(e.Message);
}
}
public static void SetActiveWindowElementToClipboardContents()
{
try
{
IntPtr windowHWnd = GetForegroundWindow();
IntPtr lpdwProcessId;
IntPtr threadId = GetWindowThreadProcessId(windowHWnd, out lpdwProcessId);
GUITHREADINFO lpgui = new GUITHREADINFO();
lpgui.cbSize = Marshal.SizeOf(lpgui);
GetGUIThreadInfo(threadId, ref lpgui);
SetText(lpgui.hwndFocus, "\0"); // empty the target before pasting
SendMessage(lpgui.hwndFocus, WM_PASTE, 0, 0);
}
catch (Exception e)
{
Console.Write(e.Message);
}
}
public static void SetClipboardContentsToActiveWindowElement()
{
try
{
IntPtr windowHWnd = GetForegroundWindow();
IntPtr lpdwProcessId;
IntPtr threadId = GetWindowThreadProcessId(windowHWnd, out lpdwProcessId);
GUITHREADINFO lpgui = new GUITHREADINFO();
lpgui.cbSize = Marshal.SizeOf(lpgui);
GetGUIThreadInfo(threadId, ref lpgui);
SendMessage(lpgui.hwndFocus, EM_SETSEL, 0, -1);
SendMessage(lpgui.hwndFocus, WM_COPY, 0, 0);
}
catch (Exception e)
{
Console.Write(e.Message);
}
}
public static string GetRTFFromActiveWindowElement()
{
try
{
IntPtr windowHWnd = GetForegroundWindow();
IntPtr lpdwProcessId;
IntPtr threadId = GetWindowThreadProcessId(windowHWnd, out lpdwProcessId);
GUITHREADINFO lpgui = new GUITHREADINFO();
lpgui.cbSize = Marshal.SizeOf(lpgui);
GetGUIThreadInfo(threadId, ref lpgui);
string result = String.Empty;
using (MemoryStream stream = new MemoryStream())
{
EDITSTREAM editStream = new EDITSTREAM();
editStream.pfnCallback = new EditStreamCallback(EditStreamProc);
editStream.dwCookie = stream;
SendMessage(lpgui.hwndFocus, EM_STREAMOUT, SF_RTF, ref editStream);
stream.Seek(0, SeekOrigin.Begin);
using (StreamReader reader = new StreamReader(stream))
{
result = reader.ReadToEnd();
}
}
return result;
}
catch (Exception e)
{
Console.Write(e.Message);
return null;
}
}
}
}