-
Notifications
You must be signed in to change notification settings - Fork 60
Expand file tree
/
Copy pathMessage.cs
More file actions
169 lines (144 loc) · 4.29 KB
/
Copy pathMessage.cs
File metadata and controls
169 lines (144 loc) · 4.29 KB
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
using System;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.InteropServices;
using System.Security;
using System.Security.Permissions;
using System.Text;
using Process.NET.Native.Types;
namespace Process.NET.Windows
{
[SecurityPermission(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.UnmanagedCode)]
[SuppressMessage("Microsoft.Security", "CA2108:ReviewDeclarativeSecurityOnValueTypes")]
[SuppressMessage("ReSharper", "FieldCanBeMadeReadOnly.Local")]
[SuppressMessage("ReSharper", "InconsistentNaming")]
[SuppressMessage("ReSharper", "ArrangeTypeMemberModifiers")]
[SuppressMessage("ReSharper", "ConvertToAutoPropertyWhenPossible")]
public struct Message
{
#if DEBUG
static TraceSwitch AllWinMessages = new TraceSwitch("AllWinMessages", "Output every received message");
#endif
IntPtr hWnd;
int msg;
IntPtr wparam;
IntPtr lparam;
IntPtr result;
public IntPtr HWnd
{
get { return hWnd; }
set { hWnd = value; }
}
public int Msg
{
get { return msg; }
set { msg = value; }
}
public IntPtr WParam
{
get { return wparam; }
set { wparam = value; }
}
public IntPtr LParam
{
get { return lparam; }
set { lparam = value; }
}
public IntPtr Result
{
get { return result; }
set { result = value; }
}
public object GetLParam(Type cls)
{
return Marshal.PtrToStructure(lparam, cls);
}
public static Message Create(IntPtr hWnd, WindowsMessages msg, IntPtr wparam, IntPtr lparam)
{
return Create(hWnd, (int) msg, wparam, lparam);
}
public static Message Create(IntPtr hWnd, int msg, IntPtr wparam, IntPtr lparam)
{
var m = new Message
{
hWnd = hWnd,
msg = msg,
wparam = wparam,
lparam = lparam,
result = IntPtr.Zero
};
#if DEBUG
if (AllWinMessages.TraceVerbose)
{
Debug.WriteLine(m.ToString());
}
#endif
return m;
}
public override bool Equals(object o)
{
if (!(o is Message))
{
return false;
}
var m = (Message) o;
return hWnd == m.hWnd &&
msg == m.msg &&
wparam == m.wparam &&
lparam == m.lparam &&
result == m.result;
}
public static bool operator !=(Message a, Message b)
{
return !a.Equals(b);
}
public static bool operator ==(Message a, Message b)
{
return a.Equals(b);
}
public override int GetHashCode()
{
return (int) hWnd << 4 | msg;
}
private static readonly CodeAccessPermission UnmanagedCode =
new SecurityPermission(SecurityPermissionFlag.UnmanagedCode);
public override string ToString()
{
// ASSURT : 151574. Link Demand on System.Windows.Forms.Message
// fails to protect overriden methods.
bool unrestricted = false;
try
{
UnmanagedCode.Demand();
unrestricted = true;
}
catch (SecurityException)
{
// eat the exception.
}
if (unrestricted)
{
return GetProperName(((WindowsMessages) Msg).ToString());
}
return base.ToString();
}
private static string GetProperName(string name)
{
var sb = new StringBuilder();
for (var i = 0; i < name.Length; i++)
{
var c = name[i];
if (i > 0 && char.IsUpper(c))
{
sb.Append(' ');
sb.Append(char.ToLowerInvariant(c));
}
else
{
sb.Append(c);
}
}
return sb.ToString();
}
}
}