Skip to content

Commit

Permalink
Allow only one instance
Browse files Browse the repository at this point in the history
Set max and min window dimensions
  • Loading branch information
Begus001 committed May 2, 2022
1 parent c900226 commit 851ae9f
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 10 deletions.
2 changes: 1 addition & 1 deletion Topmost/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:Topmost"
mc:Ignorable="d"
Title="Topmost" Height="80" Width="400" ResizeMode="NoResize" WindowStartupLocation="CenterScreen" Closing="Window_Closing" KeyDown="Window_KeyDown">
Title="Topmost" Width="400" Height="80" MaxWidth="400" MaxHeight="80" MinWidth="400" MinHeight="80" ResizeMode="NoResize" WindowStartupLocation="CenterScreen" Closing="Window_Closing" KeyDown="Window_KeyDown">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="1*"/>
Expand Down
36 changes: 27 additions & 9 deletions Topmost/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ namespace Topmost
{
public partial class MainWindow : Window
{
public Version version = new Version("1.0");
public Version version = new Version("1.1");

private const int WM_HOTKEY = 0x0312;
private const long WS_EX_TOPMOST = 0x00000008L;
Expand Down Expand Up @@ -70,6 +70,12 @@ public MainWindow()
{
InitializeComponent();

if (checkRunning())
{
MessageBox.Show("Another instance is already running!", "Instance running", MessageBoxButton.OK, MessageBoxImage.Error);
Environment.Exit(0);
}

saveFile = Path.Combine(saveDir, "key.txt");

if (!Directory.Exists(saveDir))
Expand Down Expand Up @@ -98,6 +104,18 @@ public MainWindow()
RegisterHotKey(ownHWnd, hotkeyId, mod, key);
}

private bool checkRunning()
{
Process[] procs = Process.GetProcessesByName(Assembly.GetExecutingAssembly().GetName().Name);
for (int i = 0; i < procs.Length; i++)
{
if (procs[i].Id == Process.GetCurrentProcess().Id)
continue;
return true;
}
return false;
}

private IntPtr HwndHook(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
{
if (msg == WM_HOTKEY && (int)wParam == hotkeyId)
Expand Down Expand Up @@ -373,6 +391,14 @@ private void btCheck_Click(object sender, RoutedEventArgs e)
checkStateThread.Start();
}

private void Window_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Escape)
checkState = false;

updateUI();
}

public enum ModifierKey : uint
{
ALT = 0x0001,
Expand Down Expand Up @@ -580,13 +606,5 @@ public enum VirtualKey : uint
PA1 = 0xFD,
OEMClear = 0xFE
}

private void Window_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Escape)
checkState = false;

updateUI();
}
}
}

0 comments on commit 851ae9f

Please sign in to comment.