Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/TestStack.White/Application.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using TestStack.White.Sessions;
using TestStack.White.UIItems.Finders;
using TestStack.White.UIItems.WindowItems;
using TestStack.White.WindowsAPI;

namespace TestStack.White
{
Expand Down Expand Up @@ -215,7 +216,10 @@ public virtual void Close()
Process.Dispose();
return;
}
Process.CloseMainWindow();
foreach (var window in NativeWindow.GetProcessWindows(Process.Id))
{
window.PostCloseMessage();
}
Process.WaitForExit(5000);
if (!Process.HasExited)
{
Expand Down
45 changes: 44 additions & 1 deletion src/TestStack.White/WindowsAPI/NativeWindow.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Windows;

Expand All @@ -20,6 +21,43 @@ public class NativeWindow
[DllImport("gdi32.dll")]
private static extern COLORREF GetTextColor(IntPtr hdc);

[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool EnumWindows(EnumWindowsProc lpEnumFunc, IntPtr lParam);

private delegate bool EnumWindowsProc(IntPtr hWnd, IntPtr lParam);

[DllImport("user32.dll", SetLastError = true)]
private static extern uint GetWindowThreadProcessId(IntPtr hWnd, out int lpdwProcessId);

[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool IsWindowEnabled(IntPtr hWnd);

[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("user32.dll", SetLastError = true)]
private static extern bool PostMessage(IntPtr hWnd, uint msg, IntPtr wParam, IntPtr lParam);

private const uint WM_CLOSE = 0x0010;

public static IEnumerable<NativeWindow> GetProcessWindows(int processId)
{
var result = new List<NativeWindow>();
Func<IntPtr, bool> processWindow = hwnd =>
{
if (IsWindowEnabled(hwnd))
{
int pid;
GetWindowThreadProcessId(hwnd, out pid);
if (pid == processId)
result.Add(new NativeWindow(hwnd));
}
return true;
};
EnumWindows((wnd, param) => processWindow(wnd), IntPtr.Zero);
return result;
}

public NativeWindow(Point point)
{
handle = WindowFromPoint(new POINT((int) point.X, (int) point.Y));
Expand All @@ -45,5 +83,10 @@ public virtual COLORREF TextColor
return GetTextColor(GetDC(handle));
}
}

public virtual void PostCloseMessage()
{
PostMessage(handle, WM_CLOSE, IntPtr.Zero, IntPtr.Zero);
}
}
}
}