forked from rubberduck-vba/SlimDucky
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSubclassingWindow.cs
100 lines (86 loc) · 3.73 KB
/
SubclassingWindow.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
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
namespace SlimDucky
{
public abstract class SubclassingWindow : IDisposable
{
private readonly IntPtr _subclassId;
private readonly IntPtr _hwnd;
private readonly SubClassCallback _wndProc;
private bool _listening;
private GCHandle _thisHandle;
private readonly object _subclassLock = new object();
public delegate int SubClassCallback(IntPtr hWnd, IntPtr msg, IntPtr wParam, IntPtr lParam, IntPtr uIdSubclass, IntPtr dwRefData);
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool IsWindow(IntPtr hWnd);
[DllImport("ComCtl32.dll", CharSet = CharSet.Auto)]
private static extern int SetWindowSubclass(IntPtr hWnd, SubClassCallback newProc, IntPtr uIdSubclass, IntPtr dwRefData);
[DllImport("ComCtl32.dll", CharSet = CharSet.Auto)]
private static extern int RemoveWindowSubclass(IntPtr hWnd, SubClassCallback newProc, IntPtr uIdSubclass);
[DllImport("ComCtl32.dll", CharSet = CharSet.Auto)]
private static extern int DefSubclassProc(IntPtr hWnd, IntPtr msg, IntPtr wParam, IntPtr lParam);
public IntPtr Hwnd { get { return _hwnd; } }
protected SubclassingWindow(IntPtr subclassId, IntPtr hWnd)
{
_subclassId = subclassId;
_hwnd = hWnd;
_wndProc = SubClassProc;
AssignHandle();
}
public void Dispose()
{
ReleaseHandle();
_thisHandle.Free();
}
private void AssignHandle()
{
lock(_subclassLock)
{
var result = SetWindowSubclass(_hwnd, _wndProc, _subclassId, IntPtr.Zero);
if(result != 1)
{
throw new Exception("SetWindowSubClass Failed");
}
Debug.WriteLine("SubclassingWindow.AssignHandle called for hWnd " + Hwnd);
//DO NOT REMOVE THIS CALL. Dockable windows are instantiated by the VBE, not directly by RD. On top of that,
//since we have to inherit from UserControl we don't have to keep handling window messages until the VBE gets
//around to destroying the control's host or it results in an access violation when the base class is disposed.
//We need to manually call base.Dispose() ONLY in response to a WM_DESTROY message.
_thisHandle = GCHandle.Alloc(this, GCHandleType.Normal);
_listening = true;
}
}
private void ReleaseHandle()
{
lock(_subclassLock)
{
if(!_listening)
{
return;
}
Debug.WriteLine("SubclassingWindow.ReleaseHandle called for hWnd " + Hwnd);
var result = RemoveWindowSubclass(_hwnd, _wndProc, _subclassId);
if(result != 1)
{
throw new Exception("RemoveWindowSubclass Failed");
}
_listening = false;
}
}
public virtual int SubClassProc(IntPtr hWnd, IntPtr msg, IntPtr wParam, IntPtr lParam, IntPtr uIdSubclass, IntPtr dwRefData)
{
if(!_listening)
{
Debug.WriteLine("State corrupted. Received window message while not listening.");
return DefSubclassProc(hWnd, msg, wParam, lParam);
}
if((uint)msg == (uint)WM.RUBBERDUCK_SINKING || (uint)msg == (uint)WM.DESTROY)
{
ReleaseHandle();
}
return DefSubclassProc(hWnd, msg, wParam, lParam);
}
}
}