Skip to content

Commit cb3d223

Browse files
committed
Only subclass designer windows or code panes. Closes #2786
1 parent 1cec793 commit cb3d223

File tree

3 files changed

+16
-7
lines changed

3 files changed

+16
-7
lines changed

RetailCoder.VBE/Common/WinAPI/User32.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -160,9 +160,6 @@ public static class User32
160160
[DllImport("user32.dll", CharSet = CharSet.Auto)]
161161
internal static extern IntPtr SendMessage(IntPtr hWnd, WM msg, IntPtr wParam, IntPtr lParam);
162162

163-
[DllImport("user32.dll", CharSet = CharSet.Unicode)]
164-
public static extern IntPtr FindWindowEx(IntPtr parentHandle, IntPtr childAfter, string lclassName, string windowTitle);
165-
166163
public delegate int WindowEnumProc(IntPtr hwnd, IntPtr lparam);
167164
[DllImport("user32.dll")]
168165
public static extern bool EnumChildWindows(IntPtr hwnd, WindowEnumProc func, IntPtr lParam);

Rubberduck.VBEEditor/Events/VBENativeServices.cs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,15 +70,15 @@ public static void VbeEventCallback(IntPtr hWinEventHook, uint eventType, IntPtr
7070
{
7171
if (hwnd != IntPtr.Zero &&
7272
idObject == (int)ObjId.Caret &&
73-
(eventType == (uint)WinEvent.ObjectLocationChange || eventType == (uint)WinEvent.ObjectCreate) &&
74-
hwnd.ToWindowType() == WindowType.VbaWindow)
73+
(eventType == (uint)WinEvent.ObjectLocationChange || eventType == (uint)WinEvent.ObjectCreate) &&
74+
hwnd.ToWindowType() == WindowType.CodePane)
7575
{
7676
OnSelectionChanged(hwnd);
7777
}
7878
else if (idObject == (int)ObjId.Window && (eventType == (uint)WinEvent.ObjectCreate || eventType == (uint)WinEvent.ObjectDestroy))
7979
{
8080
var type = hwnd.ToWindowType();
81-
if (type != WindowType.DesignerWindow && type != WindowType.VbaWindow)
81+
if (type != WindowType.DesignerWindow && type != WindowType.CodePane)
8282
{
8383
return;
8484
}
@@ -195,14 +195,23 @@ public enum WindowType
195195
{
196196
Indeterminate,
197197
VbaWindow,
198+
CodePane,
198199
DesignerWindow,
199200
Project
200201
}
201202

202203
public static WindowType ToWindowType(this IntPtr hwnd)
203204
{
204205
WindowType id;
205-
return Enum.TryParse(hwnd.ToClassName(), true, out id) ? id : WindowType.Indeterminate;
206+
var type = Enum.TryParse(hwnd.ToClassName(), true, out id) ? id : WindowType.Indeterminate;
207+
if (type != WindowType.VbaWindow)
208+
{
209+
return type;
210+
}
211+
//A this point we only care about code panes - none of the other 4 types of VbaWindows (Immediate, Object Browser, Locals,
212+
//and Watches) contain a tool bar at the top, so just see if the window has one as a child.
213+
var toolbar = User32.FindWindowEx(hwnd, IntPtr.Zero, "ObtbarWndClass", string.Empty);
214+
return toolbar == IntPtr.Zero ? WindowType.VbaWindow : WindowType.CodePane;
206215
}
207216

208217
public static string ToClassName(this IntPtr hwnd)

Rubberduck.VBEEditor/WindowsApi/User32.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,5 +79,8 @@ public static class User32
7979
/// <returns> The parent window IntPtr handle. </returns>
8080
[DllImport("User32.dll")]
8181
internal static extern IntPtr GetParent(IntPtr hWnd);
82+
83+
[DllImport("user32.dll", CharSet = CharSet.Unicode)]
84+
internal static extern IntPtr FindWindowEx(IntPtr parentHandle, IntPtr childAfter, string lclassName, string windowTitle);
8285
}
8386
}

0 commit comments

Comments
 (0)