Skip to content

Commit bd68f8c

Browse files
committed
Adds VBE.MDIActiveChild method for getting the VBE's active MDI window even if it isn't the active VBE window.
1 parent d098398 commit bd68f8c

File tree

3 files changed

+34
-0
lines changed

3 files changed

+34
-0
lines changed

Rubberduck.VBEEditor/SafeComWrappers/Abstract/IVBE.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ public interface IVBE : ISafeComWrapper, IEquatable<IVBE>
2121
IWindows Windows { get; }
2222

2323
IHostApplication HostApplication();
24+
IWindow ActiveMDIChild();
2425

2526
bool IsInDesignMode { get; }
2627
}

Rubberduck.VBEEditor/SafeComWrappers/VB6/VBE.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,11 @@ public IHostApplication HostApplication()
109109
return null;
110110
}
111111

112+
public IWindow ActiveMDIChild()
113+
{
114+
throw new NotImplementedException();
115+
}
116+
112117
public bool IsInDesignMode
113118
{
114119
get { return VBProjects.All(project => project.Mode == EnvironmentMode.Design); }

Rubberduck.VBEEditor/SafeComWrappers/VBA/VBE.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.IO;
44
using System.Linq;
55
using System.Runtime.InteropServices;
6+
using System.Text;
67
using Rubberduck.VBEditor.Application;
78
using Rubberduck.VBEditor.SafeComWrappers.Abstract;
89
using Rubberduck.VBEditor.SafeComWrappers.MSForms;
@@ -279,6 +280,33 @@ public IHostApplication HostApplication()
279280
return null;
280281
}
281282

283+
/// <summary> Returns the topmost MDI child window. </summary>
284+
public IWindow ActiveMDIChild()
285+
{
286+
const string mdiClientClass = "MDIClient";
287+
const int maxCaptionLength = 512;
288+
289+
IntPtr mainWindow = (IntPtr)MainWindow.HWnd;
290+
291+
IntPtr mdiClient = NativeMethods.FindWindowEx(mainWindow, IntPtr.Zero, mdiClientClass, string.Empty);
292+
293+
IntPtr mdiChild = NativeMethods.GetTopWindow(mdiClient);
294+
StringBuilder mdiChildCaption = new StringBuilder();
295+
int captionLength = NativeMethods.GetWindowText(mdiChild, mdiChildCaption, maxCaptionLength);
296+
297+
if (captionLength > 0)
298+
{
299+
try
300+
{
301+
return Windows[mdiChildCaption];
302+
}
303+
catch
304+
{
305+
}
306+
}
307+
return null;
308+
}
309+
282310
/// <summary> Returns whether the host supports unit tests.</summary>
283311
public bool HostSupportsUnitTests()
284312
{

0 commit comments

Comments
 (0)