Skip to content

Commit

Permalink
Support teams preview
Browse files Browse the repository at this point in the history
  • Loading branch information
quanljh committed Oct 11, 2023
1 parent f5b328b commit 29caf2e
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 12 deletions.
20 changes: 14 additions & 6 deletions App.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
using System.Diagnostics;
using System.Reflection;
using System.Windows;

namespace MessAround
Expand All @@ -13,5 +9,17 @@ namespace MessAround
/// </summary>
public partial class App : Application
{
public const int EXIT_CODE_ALREADY_RUNNING = -9;

protected override void OnStartup(StartupEventArgs e)
{
if (Process.GetProcessesByName(Assembly.GetExecutingAssembly().GetName().Name).Length > 1)
{
MessageBox.Show("Cannot run this application multiple times", "Error");
Current.Shutdown(EXIT_CODE_ALREADY_RUNNING);
return;
}
base.OnStartup(e);
}
}
}
20 changes: 20 additions & 0 deletions Helpers/NativeMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ public static class NativeMethods
[DllImport("user32.dll", SetLastError = true)]
internal static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);

[DllImport("user32.dll", SetLastError = true)]
internal static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

[DllImport("user32.dll")]
internal static extern bool EnumChildWindows(IntPtr hwndParent, EnumWindowsProc enumProc, IntPtr lParam);

[DllImport("user32.dll")]
internal static extern bool SetForegroundWindow(IntPtr hWnd);

Expand Down Expand Up @@ -157,4 +163,18 @@ public static bool RestoreWindow(IntPtr hWnd)
wpl.showCmd = ShowWindowCommands.Restore;
return SetWindowPlacement(hWnd, ref wpl);
}

// Delegate to be used with EnumWindows
public delegate bool EnumWindowsProc(IntPtr hWnd, IntPtr lParam);

public static bool MyEnumWindowsProc(IntPtr hWnd, IntPtr lParam)
{
StringBuilder title = new StringBuilder(256);
GetWindowText(hWnd, title, 256);

Console.WriteLine($"Window handle: {hWnd}, Title: {title}");

return true; // Continue enumeration
}

}
4 changes: 2 additions & 2 deletions MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
mc:Ignorable="d"
Title="KeepTeamsAlive" Height="300" Width="300">
<StackPanel VerticalAlignment="Center">
<TextBlock HorizontalAlignment="Center" VerticalAlignment="Center" Text="Ready to loaf on the job"></TextBlock>
<TextBlock Margin="0,5" HorizontalAlignment="Center" VerticalAlignment="Center" Text="Close this application if you want to back to work."></TextBlock>
<TextBlock HorizontalAlignment="Center" VerticalAlignment="Center" Text="Please go to have a coffee."></TextBlock>
<TextBlock x:Name="Message" Margin="0,5" HorizontalAlignment="Center" VerticalAlignment="Center" Text="Close this application if you want to back to work."></TextBlock>
</StackPanel>
</Window>
24 changes: 20 additions & 4 deletions MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,15 @@ public MainWindow()

if (processes.Length == 0)
{
_isTeamsRunning = false;
MessageBox.Show("Teams is not running!", "Warning");
Close();
return;
processes = Process.GetProcessesByName("ms-teams");

if (processes.Length == 0)
{
_isTeamsRunning = false;
MessageBox.Show("Teams is not running!", "Warning");
Close();
return;
}
}

_isTeamsRunning = true;
Expand Down Expand Up @@ -72,6 +77,17 @@ private void MainWindow_OnLoaded(object sender, RoutedEventArgs e)
return;
}


if (_teamsHWND == IntPtr.Zero)
{
Message.Text = "Starting teams...";
Process process = new Process();
process.StartInfo.FileName = "ms-teams.exe";
process.StartInfo.WindowStyle = ProcessWindowStyle.Normal;
process.Start();
Message.Text = "Close this application if you want to back to work.";
}

NativeMethods.SetForegroundWindow(_teamsHWND);

_timer = new DispatcherTimer
Expand Down

0 comments on commit 29caf2e

Please sign in to comment.