|
| 1 | +using System; |
| 2 | +using System.ComponentModel; |
| 3 | +using System.Diagnostics; |
| 4 | +using System.Drawing; |
| 5 | +using System.Runtime.InteropServices; |
| 6 | +using System.Windows.Forms; |
| 7 | + |
| 8 | +namespace SlimDucky |
| 9 | +{ |
| 10 | + [ComVisible(true)] |
| 11 | + [Guid(Guid)] |
| 12 | + [ProgId(ProgId)] |
| 13 | + [EditorBrowsable(EditorBrowsableState.Never)] |
| 14 | + //Nothing breaks because we declare a ProgId |
| 15 | + // ReSharper disable once InconsistentNaming |
| 16 | + //Underscores make classes invisible to VB6 object explorer |
| 17 | + public partial class _DockableWindowHost : UserControl |
| 18 | + { |
| 19 | + public const string ProgId = "SlimDucky.DockableWindowHost"; |
| 20 | + public const string Guid = "F6F1B4EC-9A1E-4311-91C2-D79A6A6F047E"; |
| 21 | + |
| 22 | + // ReSharper disable UnusedAutoPropertyAccessor.Local |
| 23 | + [StructLayout(LayoutKind.Sequential)] |
| 24 | + private struct Rect |
| 25 | + { |
| 26 | + public int Left { get; set; } |
| 27 | + public int Top { get; set; } |
| 28 | + public int Right { get; set; } |
| 29 | + public int Bottom { get; set; } |
| 30 | + } |
| 31 | + // ReSharper restore UnusedAutoPropertyAccessor.Local |
| 32 | + |
| 33 | + [StructLayout(LayoutKind.Explicit)] |
| 34 | + private struct LParam |
| 35 | + { |
| 36 | + [FieldOffset(0)] |
| 37 | + public uint Value; |
| 38 | + [FieldOffset(0)] |
| 39 | + public readonly ushort LowWord; |
| 40 | + [FieldOffset(2)] |
| 41 | + public readonly ushort HighWord; |
| 42 | + } |
| 43 | + |
| 44 | + [DllImport("User32.dll")] |
| 45 | + static extern IntPtr GetParent(IntPtr hWnd); |
| 46 | + |
| 47 | + [DllImport("User32.dll", EntryPoint = "GetClientRect")] |
| 48 | + static extern int GetClientRect(IntPtr hWnd, ref Rect lpRect); |
| 49 | + |
| 50 | + private IntPtr _parentHandle; |
| 51 | + private ParentWindow _subClassingWindow; |
| 52 | + private GCHandle _thisHandle; |
| 53 | + |
| 54 | + internal void AddUserControl(UserControl control, IntPtr vbeHwnd) |
| 55 | + { |
| 56 | + _parentHandle = GetParent(Handle); |
| 57 | + _subClassingWindow = new ParentWindow(vbeHwnd, new IntPtr(GetHashCode()), _parentHandle); |
| 58 | + _subClassingWindow.CallBackEvent += OnCallBackEvent; |
| 59 | + |
| 60 | + //DO NOT REMOVE THIS CALL. Dockable windows are instantiated by the VBE, not directly by RD. On top of that, |
| 61 | + //since we have to inherit from UserControl we don't have to keep handling window messages until the VBE gets |
| 62 | + //around to destroying the control's host or it results in an access violation when the base class is disposed. |
| 63 | + //We need to manually call base.Dispose() ONLY in response to a WM_DESTROY message. |
| 64 | + _thisHandle = GCHandle.Alloc(this, GCHandleType.Normal); |
| 65 | + |
| 66 | + if(control != null) |
| 67 | + { |
| 68 | + control.Dock = DockStyle.Fill; |
| 69 | + Controls.Add(control); |
| 70 | + } |
| 71 | + AdjustSize(); |
| 72 | + } |
| 73 | + |
| 74 | + private void OnCallBackEvent(object sender, SubClassingWindowEventArgs e) |
| 75 | + { |
| 76 | + if(!e.Closing) |
| 77 | + { |
| 78 | + var param = new LParam { Value = (uint)e.LParam }; |
| 79 | + Size = new Size(param.LowWord, param.HighWord); |
| 80 | + } |
| 81 | + else |
| 82 | + { |
| 83 | + Debug.WriteLine("DockableWindowHost removed event handler."); |
| 84 | + _subClassingWindow.CallBackEvent -= OnCallBackEvent; |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + private void AdjustSize() |
| 89 | + { |
| 90 | + var rect = new Rect(); |
| 91 | + if(GetClientRect(_parentHandle, ref rect) != 0) |
| 92 | + { |
| 93 | + Size = new Size(rect.Right - rect.Left, rect.Bottom - rect.Top); |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + protected override bool ProcessKeyPreview(ref Message m) |
| 98 | + { |
| 99 | + const int wmKeydown = 0x100; |
| 100 | + var result = false; |
| 101 | + |
| 102 | + var hostedUserControl = (UserControl)Controls[0]; |
| 103 | + |
| 104 | + if(m.Msg == wmKeydown) |
| 105 | + { |
| 106 | + var pressedKey = (Keys)m.WParam; |
| 107 | + switch(pressedKey) |
| 108 | + { |
| 109 | + case Keys.Tab: |
| 110 | + switch(ModifierKeys) |
| 111 | + { |
| 112 | + case Keys.None: |
| 113 | + SelectNextControl(hostedUserControl.ActiveControl, true, true, true, true); |
| 114 | + result = true; |
| 115 | + break; |
| 116 | + case Keys.Shift: |
| 117 | + SelectNextControl(hostedUserControl.ActiveControl, false, true, true, true); |
| 118 | + result = true; |
| 119 | + break; |
| 120 | + } |
| 121 | + break; |
| 122 | + case Keys.Return: |
| 123 | + if(hostedUserControl.ActiveControl.GetType() == typeof(Button)) |
| 124 | + { |
| 125 | + var activeButton = (Button)hostedUserControl.ActiveControl; |
| 126 | + activeButton.PerformClick(); |
| 127 | + } |
| 128 | + break; |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + if(!result) |
| 133 | + { |
| 134 | + result = base.ProcessKeyPreview(ref m); |
| 135 | + } |
| 136 | + return result; |
| 137 | + } |
| 138 | + |
| 139 | + protected override void DefWndProc(ref Message m) |
| 140 | + { |
| 141 | + //See the comment in the ctor for why we have to listen for this. |
| 142 | + if(m.Msg == (int)WM.DESTROY) |
| 143 | + { |
| 144 | + Debug.WriteLine("DockableWindowHost received WM.DESTROY."); |
| 145 | + _thisHandle.Free(); |
| 146 | + } |
| 147 | + base.DefWndProc(ref m); |
| 148 | + } |
| 149 | + |
| 150 | + //override |
| 151 | + |
| 152 | + public void Release() |
| 153 | + { |
| 154 | + Debug.WriteLine("DockableWindowHost release called."); |
| 155 | + _subClassingWindow.Dispose(); |
| 156 | + } |
| 157 | + |
| 158 | + protected override void DestroyHandle() |
| 159 | + { |
| 160 | + Debug.WriteLine("DockableWindowHost DestroyHandle called."); |
| 161 | + base.DestroyHandle(); |
| 162 | + } |
| 163 | + |
| 164 | + [ComVisible(false)] |
| 165 | + public class ParentWindow : SubclassingWindow |
| 166 | + { |
| 167 | + public event SubClassingWindowEventHandler CallBackEvent; |
| 168 | + public delegate void SubClassingWindowEventHandler(object sender, SubClassingWindowEventArgs e); |
| 169 | + |
| 170 | + private readonly IntPtr _vbeHwnd; |
| 171 | + |
| 172 | + private void OnCallBackEvent(SubClassingWindowEventArgs e) |
| 173 | + { |
| 174 | + CallBackEvent?.Invoke(this, e); |
| 175 | + } |
| 176 | + |
| 177 | + public ParentWindow(IntPtr vbeHwnd, IntPtr id, IntPtr handle) : base(id, handle) |
| 178 | + { |
| 179 | + _vbeHwnd = vbeHwnd; |
| 180 | + } |
| 181 | + |
| 182 | + private bool _closing; |
| 183 | + public override int SubClassProc(IntPtr hWnd, IntPtr msg, IntPtr wParam, IntPtr lParam, IntPtr uIdSubclass, IntPtr dwRefData) |
| 184 | + { |
| 185 | + switch((uint)msg) |
| 186 | + { |
| 187 | + case (uint)WM.SIZE: |
| 188 | + var args = new SubClassingWindowEventArgs(lParam); |
| 189 | + if(!_closing) |
| 190 | + OnCallBackEvent(args); |
| 191 | + break; |
| 192 | + case (uint)WM.SETFOCUS: |
| 193 | + if(!_closing) |
| 194 | + User32.SendMessage(_vbeHwnd, WM.RUBBERDUCK_CHILD_FOCUS, Hwnd, Hwnd); |
| 195 | + break; |
| 196 | + case (uint)WM.KILLFOCUS: |
| 197 | + if(!_closing) |
| 198 | + User32.SendMessage(_vbeHwnd, WM.RUBBERDUCK_CHILD_FOCUS, Hwnd, IntPtr.Zero); |
| 199 | + break; |
| 200 | + case (uint)WM.RUBBERDUCK_SINKING: |
| 201 | + OnCallBackEvent(new SubClassingWindowEventArgs(lParam) { Closing = true }); |
| 202 | + _closing = true; |
| 203 | + break; |
| 204 | + } |
| 205 | + return base.SubClassProc(hWnd, msg, wParam, lParam, uIdSubclass, dwRefData); |
| 206 | + } |
| 207 | + } |
| 208 | + } |
| 209 | +} |
0 commit comments