Skip to content

Instantly share code, notes, and snippets.

@fantasticswallow
Last active August 29, 2015 14:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fantasticswallow/1a9b757d7b9c04699adc to your computer and use it in GitHub Desktop.
Save fantasticswallow/1a9b757d7b9c04699adc to your computer and use it in GitHub Desktop.
PresentationWindowSwitcher.ThisAddin.vb
Imports System.Diagnostics
Imports System.Runtime.InteropServices
Public Class ThisAddIn
Private Sub ThisAddIn_Startup() Handles Me.Startup
If System.Windows.Forms.Screen.AllScreens.Length > 1 Then
If System.Windows.Forms.Screen.AllScreens.Any(Function(x) x.Bounds.X > 0 OrElse x.Bounds.Y > 0) Then
isExtendedBefore = True
End If
End If
timer.Interval = TimeSpan.FromMilliseconds(300)
AddHandler Application.SlideShowBegin, Sub(Wn As PowerPoint.SlideShowWindow)
If Wn.Presentation.SlideShowSettings.ShowPresenterView = Microsoft.Office.Core.MsoTriState.msoTrue Then
Dim pid = Process.GetCurrentProcess.MainWindowHandle
hwndList.Clear()
EnumChildWindows(pid, Function(hw, lp)
hwndList.Add(hw)
Return True
End Function, New IntPtr(0))
If Not hwndList.Contains(pid) Then
hwndList.Add(pid)
End If
isExtendCanceling = True
timer.Start()
End If
End Sub
AddHandler Application.SlideShowEnd, Sub(Pr As PowerPoint.Presentation)
If isExtendCanceling Then
timer.Stop()
isExtendCanceling = False
'RemoveHandler Application.WindowDeactivate, AddressOf Application_WindowDeactive
End If
End Sub
ps.CreateNoWindow = True
ps.WindowStyle = ProcessWindowStyle.Hidden
ps.FileName = "DisplaySwitch.exe"
ps.Arguments = "/clone"
p.StartInfo = ps
ps2.CreateNoWindow = True
ps2.WindowStyle = ProcessWindowStyle.Hidden
ps2.FileName = "DisplaySwitch.exe"
ps2.Arguments = "/extend"
p2.StartInfo = ps2
End Sub
Private Sub Application_WindowDeactive(Pres As PowerPoint.Presentation, Wn As PowerPoint.DocumentWindow)
End Sub
Private p = New Process
Private ps = New ProcessStartInfo
Private p2 = New Process
Private ps2 = New ProcessStartInfo
Private WithEvents timer As New System.Windows.Threading.DispatcherTimer
'Private Property activePtr As IntPtr = Nothing
Private hwndList As New List(Of IntPtr)
Private Property isExtendedBefore As Boolean = False
Private Property isExtendCanceling As Boolean = False
Private Sub ThisAddIn_Shutdown() Handles Me.Shutdown
If timer.IsEnabled Then
timer.Stop()
End If
End Sub
<DllImport("user32.dll")> _
Private Shared Function GetForegroundWindow() As IntPtr
End Function
<DllImport("user32.dll")>
Public Shared Function EnumChildWindows(hWndParent As IntPtr, lpEnumFunc As EnumChildDelegate, lParam As IntPtr) As Boolean
End Function
Public Delegate Function EnumChildDelegate(hwnd As IntPtr, lParam As IntPtr) As Boolean
Private isChanged As Boolean = False
Private Sub timer_Tick(sender As Object, e As EventArgs) Handles timer.Tick
Dim ip = GetForegroundWindow()
If Not hwndList.Contains(ip) Then
If Not isChanged Then
isChanged = True
If Not isExtendedBefore Then
p.Start()
End If
End If
Else
If isChanged Then
isChanged = False
If Not isExtendedBefore Then
p2.Start()
End If
End If
End If
End Sub
End Class
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment