Can I catch the lost focus (deactivation) of a PowerPoint slideshow window due to a hyperlink click?

zhenhao gan 1 Reputation point
2021-01-06T10:00:42.26+00:00

how to catch the lost focus (deactivation) of the current slideshow. In my slideshow a slide has a hyperlink. When the link is clicked, the default browser is opened to the respective page and, of course, my slideshow losses the focus. When the browser is closed, the slideshow automatically receives the focus. Is there any way to catch either the slideshow window deactivate or activate event? I tried any possible events (WindowActivate, WindowDeactivate) without success, as they are referring to the presentation, not to the slideshow. Or maybe any way to detect that a link was clicked?

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,364 questions
PowerPoint Management
PowerPoint Management
PowerPoint: A family of Microsoft presentation graphics products that offer tools for creating presentations and adding graphic effects like multimedia objects and special effects with text.Management: The act or process of organizing, handling, directing or controlling something.
222 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Timon Yang-MSFT 9,576 Reputation points
    2021-01-07T03:03:42.673+00:00

    You can use UI Automation API to do this.
    First add two references: UIAutomationClient and UIAutomationTypes, they are generally located in:
    C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework.NETFramework\v4.8
    The version depends on which version you have installed.
    Then, you can use the following code to detect whether the slide is in focus.

            static void Main(string[] args)  
            {  
                Automation.AddAutomationFocusChangedEventHandler(OnFocusChangedHandler);  
                Console.ReadLine();  
            }  
            private static void OnFocusChangedHandler(object src, AutomationFocusChangedEventArgs args)  
            {  
                AutomationElement element = src as AutomationElement;  
                if (element != null)  
                {  
                    try  
                    {  
                        string name = element.Current.Name;  
                        string id = element.Current.AutomationId;  
      
                        int processId = element.Current.ProcessId;  
                        using (Process process = Process.GetProcessById(processId))  
                        {  
                            if (name.Contains("Slide"))  
                            {  
                                Console.WriteLine(process.MainWindowTitle);  
                                Console.WriteLine("  Name: {0}, Id: {1}, Process: {2}", name, id, process.ProcessName);  
                            }  
                        }  
                    }  
                    catch (ElementNotAvailableException)  
                    {  
                        Console.WriteLine("this app has closed");  
                    }  
                }  
    

    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments