VB.NET How to get a window handle for Presenter View in powerpoint

DanAtDVA 0 Reputation points
2023-02-22T15:18:34.9333333+00:00

Code: VB.net, Visual Studio 2019, Office 365 windows desktop compatibility only, on windows 10 or better, powerpoint add-in

Use case: need to pop up a stay-on-top (topmost) modeless window when a powerpoint slideshow is started (only when presenter mode is turned on). Need to set the owner of the new window to the "presenter mode" window, so that: it is only top-most when powerpoint slideshow is focused.

What I do have working already: My code already pops a modeless window when a presentation slideshow is started.

My related questions are:

a) when someone presses "ESCAPE" to "end the slideshow", how to I keep the modeless window from "swallowing" the escape key? (which is what it's doing now because it's topmost window). I need to pass the escape key event back to powerpoint slideshow to end the slide show.

b) what is the code to "find" the handle to the Presenter Mode window? Once i find the handle, how to I "assign it" in VB.NET to the Windows Form modeless pop-over as the owner?

Note: I used SpyXX (Spy++) and got this:

STANDARD SHOW DISPLAY WINDOW

PowerPoint Slide Show - cdvp2-test.pptx" screenClass
Children:

  • "DropShadowTop" DropShadow
  • "MsoDockLeft" MsoCommandBarDock
  • "MsoDockRight" MsoCommandBarDock
  • "MsoDockTop" MsoCommandBarDock
  • "MsoDockBottom" MsoCommandBarDock

Work Window: PRESENTER VIEW
"cdvp2-test.pptx - PowerPoint Presenter View" PodiumParent
Children:

  • "cdvp2-test.pptx - PowerPoint Presenter View" screenClass
    --- "" MsoWorkPane
    ----- "" NUIPane
    ------- "" NetUIHWND
Microsoft 365 and Office | PowerPoint | For business | Windows
Developer technologies | .NET | Other
Developer technologies | VB
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Alan Farias 755 Reputation points
    2023-02-24T16:00:50.2066667+00:00

    a) To prevent the modeless window from capturing the Escape key, you can handle the KeyDown event of the window and check if the pressed key is Escape. If it is, you can set the Handled property of the KeyEventArgs object to true, which will prevent the key from being processed by the window. Here's an example:

    Private Sub ModelessForm_KeyDown(sender As Object, e As KeyEventArgs) Handles Me.KeyDown
        If e.KeyCode = Keys.Escape Then
            e.Handled = True
            ' End the slideshow here
        End If
    End Sub
    

    b) To find the handle of the Presenter View window, you can use the FindWindow function from the user32.dll library. The window class name of the Presenter View window is "screenClass", so you can pass that as the lpClassName parameter to the function. Once you have the handle, you can use it as the owner of your modeless window by setting the Owner property of the window to the handle. Here's an example:

    ' Import the FindWindow function from user32.dll
    Private Declare Function FindWindow Lib "user32.dll" Alias "FindWindowA" _
        (ByVal lpClassName As String, ByVal lpWindowName As String) As IntPtr
    
    ' Find the Presenter View window by class name
    Dim presenterViewHandle As IntPtr = FindWindow("screenClass", Nothing)
    
    ' Set the Presenter View window as the owner of the modeless form
    ModelessForm.Owner = presenterViewHandle
    

    Note that you should call this code after the Presenter View window has been created, which may not happen immediately when the slideshow starts. You can use a timer to periodically check if the window has been created and then set the owner when it is available.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.