diff --git a/Assets/FolderOpened.png b/Assets/FolderOpened.png
new file mode 100644
index 0000000..2b404dd
Binary files /dev/null and b/Assets/FolderOpened.png differ
diff --git a/Assets/WebBrowser.png b/Assets/WebBrowser.png
new file mode 100644
index 0000000..ebc5665
Binary files /dev/null and b/Assets/WebBrowser.png differ
diff --git a/MainWindow.xaml b/MainWindow.xaml
index b9401dc..55407e0 100644
--- a/MainWindow.xaml
+++ b/MainWindow.xaml
@@ -7,8 +7,8 @@
mc:Ignorable="d"
Title="New Process Monitoring" Height="269" Width="561" Activated="wMain_Activated" Closing="Window_Closing" ResizeMode="CanResizeWithGrip" SourceInitialized="Window_SourceInitialized">
+
-
diff --git a/MainWindow.xaml.cs b/MainWindow.xaml.cs
index 7123c3d..108994e 100644
--- a/MainWindow.xaml.cs
+++ b/MainWindow.xaml.cs
@@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
+using System.IO;
using System.Linq;
using System.Management;
using System.Text;
@@ -31,21 +32,63 @@ public partial class MainWindow : Window
Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "NewPm", "processes.json"));
private SemaphoreSlim _tableItemsRefreshSemaphore = new SemaphoreSlim(1);
+ private MinimizeToTray _minimizeToTray;
ManagementObjectSearcher processSearcher = new ManagementObjectSearcher("SELECT ProcessId, Caption, ExecutablePath, CommandLine FROM Win32_Process");
public MainWindow()
{
InitializeComponent();
+ _minimizeToTray = new MinimizeToTray(this);
- MinimizeToTray.Enable(this);
+ var tbnOpenGoogle = new FrameworkElementFactory(typeof(Image));
+ var tbnOpenFolder = new FrameworkElementFactory(typeof(Image));
+
+ BitmapImage tbnOpenGoogleImage = new BitmapImage(new Uri("pack://application:,,,/Assets/WebBrowser.png"));
+ tbnOpenGoogle.SetValue(Image.SourceProperty, tbnOpenGoogleImage);
+
+ BitmapImage tbnOpenFolderImage = new BitmapImage(new Uri("pack://application:,,,/Assets/FolderOpened.png"));
+ tbnOpenFolder.SetValue(Image.SourceProperty, tbnOpenFolderImage);
+
+ tbnOpenGoogle.AddHandler(Image.MouseUpEvent, new MouseButtonEventHandler((s, e) =>
+ {
+ if (tProcesses.SelectedItem != null)
+ {
+ var path = ((ProcessTableItem)tProcesses.SelectedItem).FullPath;
+ var fileName = System.IO.Path.GetFileName(path);
+ System.Diagnostics.Process.Start("https://www.google.com/search?q=What+is+" + fileName);
+ }
+ }));
+ tbnOpenFolder.AddHandler(Image.MouseUpEvent, new MouseButtonEventHandler((s, e) =>
+ {
+ if (tProcesses.SelectedItem != null)
+ {
+ var filePath = ((ProcessTableItem)tProcesses.SelectedItem).FullPath;
+ Process.Start("explorer.exe", "/select, \"" + filePath + "\"");
+ }
+ }));
+
+ this.tProcesses.Columns.Insert(0, new DataGridTemplateColumn()
+ {
+ Header = "",
+ CellTemplate = new DataTemplate() { VisualTree = tbnOpenGoogle }
+ }
+ );
+ this.tProcesses.Columns.Insert(0, new DataGridTemplateColumn()
+ {
+ Header = "",
+ CellTemplate = new DataTemplate() { VisualTree = tbnOpenFolder }
+ }
+ );
tProcesses.ItemsSource = _tableItems;
+
+
_ = ProcessWatcherLoopAsync();
+ Application.Current.Exit += App_Exit;
}
-
public async Task ProcessWatcherLoopAsync()
{
@@ -68,8 +111,9 @@ private async Task RefreshProcessTableAsync()
using (var results = processSearcher.Get())
{
- foreach (var mo in results.Cast())
+ foreach (var resultItem in results)
{
+ var mo = (ManagementObject)resultItem;
var pTitle = (string)mo["Caption"];
var pPath = (string)mo["ExecutablePath"];
var pCommandLine = (string)mo["CommandLine"];
@@ -131,7 +175,7 @@ private void wMain_Activated(object sender, EventArgs e)
private void tProcesses_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
{
if (e.PropertyType == typeof(System.DateTime))
- (e.Column as DataGridTextColumn).Binding.StringFormat = "dd.MM.yyyy hh.mm";
+ (e.Column as DataGridTextColumn).Binding.StringFormat = "dd.MM.yyyy hh:mm";
}
private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
@@ -145,5 +189,9 @@ private void Window_SourceInitialized(object sender, EventArgs e)
WindowExtensions.HideMinimizeAndMaximizeButtons(this);
this.WindowState = WindowState.Minimized;
}
+ private void App_Exit(object sender, ExitEventArgs e)
+ {
+ _minimizeToTray.Dispose();
+ }
}
}
diff --git a/MinimizeToTray.cs b/MinimizeToTray.cs
index 5499ca9..e079216 100644
--- a/MinimizeToTray.cs
+++ b/MinimizeToTray.cs
@@ -11,89 +11,99 @@
namespace NewProcessMonitoring
{
- internal class MinimizeToTray
+ internal class MinimizeToTray: IDisposable
{
+ private Window _window;
+ private NotifyIcon _notifyIcon;
+ //private bool _balloonShown;
+
///
- /// Enables "minimize to tray" behavior for the specified Window.
+ /// Initializes a new instance of the MinimizeToTray class.
///
- /// Window to enable the behavior for.
- public static void Enable(Window window)
+ /// Window instance to attach to.
+ public MinimizeToTray(Window window)
{
- // No need to track this instance; its event handlers will keep it alive
- new MinimizeToTrayInstance(window);
+ Debug.Assert(window != null, "window parameter is null.");
+ _window = window;
+ _window.StateChanged += HandleStateChanged;
}
///
- /// Class implementing "minimize to tray" functionality for a Window instance.
+ /// Handles the Window's StateChanged event.
///
- private class MinimizeToTrayInstance
+ /// Event source.
+ /// Event arguments.
+ private void HandleStateChanged(object sender, EventArgs e)
{
- private Window _window;
- private NotifyIcon _notifyIcon;
- //private bool _balloonShown;
-
- ///
- /// Initializes a new instance of the MinimizeToTrayInstance class.
- ///
- /// Window instance to attach to.
- public MinimizeToTrayInstance(Window window)
+ if (_notifyIcon == null)
{
- Debug.Assert(window != null, "window parameter is null.");
- _window = window;
- _window.StateChanged += HandleStateChanged;
+ // Initialize NotifyIcon instance "on demand"
+ var contextMenu = new System.Windows.Forms.ContextMenu();
+ var menuItem_Exit = new System.Windows.Forms.MenuItem();
+ contextMenu.MenuItems.AddRange(
+ new System.Windows.Forms.MenuItem[] { menuItem_Exit });
+ menuItem_Exit.Index = 0;
+ menuItem_Exit.Text = "E&xit";
+ menuItem_Exit.Click += (_sender, _e) => System.Windows.Application.Current.Shutdown();
+
+
+ _notifyIcon = new NotifyIcon();
+ _notifyIcon.ContextMenu = contextMenu;
+ _notifyIcon.Icon = Icon.ExtractAssociatedIcon(Assembly.GetEntryAssembly().Location);
+ _notifyIcon.MouseClick += new MouseEventHandler(HandleNotifyIconOrBalloonClicked);
+ _notifyIcon.BalloonTipClicked += new EventHandler(HandleNotifyIconOrBalloonClicked);
}
+ // Update copy of Window Title in case it has changed
+ _notifyIcon.Text = _window.Title;
- ///
- /// Handles the Window's StateChanged event.
- ///
- /// Event source.
- /// Event arguments.
- private void HandleStateChanged(object sender, EventArgs e)
- {
- if (_notifyIcon == null)
- {
- // Initialize NotifyIcon instance "on demand"
- var contextMenu = new System.Windows.Forms.ContextMenu();
- var menuItem_Exit = new System.Windows.Forms.MenuItem();
- contextMenu.MenuItems.AddRange(
- new System.Windows.Forms.MenuItem[] { menuItem_Exit });
- menuItem_Exit.Index = 0;
- menuItem_Exit.Text = "E&xit";
- menuItem_Exit.Click += (_sender, _e) => System.Windows.Application.Current.Shutdown();
+ // Show/hide Window and NotifyIcon
+ var minimized = (_window.WindowState == WindowState.Minimized);
+ _window.ShowInTaskbar = !minimized;
+ _notifyIcon.Visible = minimized;
+ //if (minimized && !_balloonShown)
+ //{
+ // // If this is the first time minimizing to the tray, show the user what happened
+ // _notifyIcon.ShowBalloonTip(1000, null, _window.Title, ToolTipIcon.None);
+ // _balloonShown = true;
+ //}
+ }
+ ///
+ /// Handles a click on the notify icon or its balloon.
+ ///
+ /// Event source.
+ /// Event arguments.
+ private void HandleNotifyIconOrBalloonClicked(object sender, EventArgs e)
+ {
+ // Restore the Window
+ _window.WindowState = WindowState.Normal;
+ _window.Activate();
+ }
- _notifyIcon = new NotifyIcon();
- _notifyIcon.ContextMenu = contextMenu;
- _notifyIcon.Icon = Icon.ExtractAssociatedIcon(Assembly.GetEntryAssembly().Location);
- _notifyIcon.MouseClick += new MouseEventHandler(HandleNotifyIconOrBalloonClicked);
- _notifyIcon.BalloonTipClicked += new EventHandler(HandleNotifyIconOrBalloonClicked);
- }
- // Update copy of Window Title in case it has changed
- _notifyIcon.Text = _window.Title;
- // Show/hide Window and NotifyIcon
- var minimized = (_window.WindowState == WindowState.Minimized);
- _window.ShowInTaskbar = !minimized;
- _notifyIcon.Visible = minimized;
- //if (minimized && !_balloonShown)
- //{
- // // If this is the first time minimizing to the tray, show the user what happened
- // _notifyIcon.ShowBalloonTip(1000, null, _window.Title, ToolTipIcon.None);
- // _balloonShown = true;
- //}
- }
- ///
- /// Handles a click on the notify icon or its balloon.
- ///
- /// Event source.
- /// Event arguments.
- private void HandleNotifyIconOrBalloonClicked(object sender, EventArgs e)
- {
- // Restore the Window
- _window.WindowState = WindowState.Normal;
- _window.Activate();
- }
+ bool disposed = false;
+ ~MinimizeToTray()
+ {
+ Dispose(disposing: false);
+ }
+
+ public void Dispose()
+ {
+ Dispose(disposing: true);
+ GC.SuppressFinalize(this);
+ }
+ protected virtual void Dispose(bool disposing)
+ {
+ if (disposed)
+ return;
+
+ _notifyIcon.Visible = false;
+ _notifyIcon.Icon = null;
+ _notifyIcon.Dispose();
+ _notifyIcon = null;
+
+ disposed = true;
}
}
}
diff --git a/NewProcessMonitoringNetFwk.csproj b/NewProcessMonitoringNetFwk.csproj
index 2de73ea..682d070 100644
--- a/NewProcessMonitoringNetFwk.csproj
+++ b/NewProcessMonitoringNetFwk.csproj
@@ -152,9 +152,9 @@
-
-
-
+
+
+
@@ -168,5 +168,11 @@
false
+
+
+
+
+
+
\ No newline at end of file
diff --git a/Screenshot.png b/Screenshot.png
index 25cb0cd..c742869 100644
Binary files a/Screenshot.png and b/Screenshot.png differ