Processing Ajax...

Title
Close Dialog

Message

Confirm
Close Dialog

Confirm
Close Dialog

Find modal window and focus it

Description
This script will loop through all open windows, and if it finds a modal window, it will focus it. Note that if there's more than one modal window open, it will just focus the last one it finds.
Language
C#.net
Minimum Version
Created By
Thomas Malloch (BFS)
Contributors
Keith Lammers (BFS)
Date Created
Apr 3, 2018
Date Last Modified
Apr 3, 2018

Scripted Function (Macro) Code

using System;
using System.Drawing;
using System.Runtime.InteropServices;

// The 'windowHandle' parameter will contain the window handle for the:
//   - Active window when run by hotkey
//   - Trigger target when run by a Trigger rule
//   - TitleBar Button owner when run by a TitleBar Button
//   - Jump List owner when run from a Taskbar Jump List
//   - Currently focused window if none of these match
public static class DisplayFusionFunction
{
    [DllImport("User32.dll")]
    [return: MarshalAs(UnmanagedType.Bool)]
    private static extern bool IsWindowEnabled(IntPtr hWnd);

	public static void Run(IntPtr windowHandle)
	{
		foreach(IntPtr window in BFS.Window.GetAllWindowHandles())
		{
            if(!IsPopupWindow(window))
                continue;
                
            //BFS.Dialog.ShowMessageInfo(BFS.Window.GetText(window));    
            BFS.Window.Focus(window);
		}
	}
	
	private static bool IsPopupWindow(IntPtr handle)
	{
        // child windows cannot have owners
        if((BFS.Window.GetWindowStyle(handle) & BFS.WindowEnum.WindowStyle.WS_CHILD__CHILDWINDOW) == BFS.WindowEnum.WindowStyle.WS_CHILD__CHILDWINDOW)
            return false;
            
        IntPtr owner = BFS.Window.GetOwner(handle);
        
        // not an owned window
        if(owner == IntPtr.Zero)
            return false;
            
        // window is enabled
        if(!IsWindowEnabled(handle))
            return false;
        
        // window has text
        if(String.IsNullOrEmpty(BFS.Window.GetText(handle)))
            return false;
            
        // owner is disabled
        if(IsWindowEnabled(owner))
            return false;
            
        return true;
	}
}