mirror of
https://github.com/microsoft/PowerToys.git
synced 2026-04-08 20:27:36 +02:00
Adding the ability to have a tray icon
This commit is contained in:
@@ -5,6 +5,7 @@
|
|||||||
using Microsoft.Win32;
|
using Microsoft.Win32;
|
||||||
using NLog;
|
using NLog;
|
||||||
using System;
|
using System;
|
||||||
|
using System.Drawing;
|
||||||
using System.Runtime.InteropServices;
|
using System.Runtime.InteropServices;
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
@@ -33,15 +34,18 @@ namespace Espresso.Shell.Core
|
|||||||
|
|
||||||
private static Logger log;
|
private static Logger log;
|
||||||
|
|
||||||
|
// More details about the API used: https://docs.microsoft.com/windows/win32/api/winbase/nf-winbase-setthreadexecutionstate
|
||||||
|
[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
|
||||||
|
static extern EXECUTION_STATE SetThreadExecutionState(EXECUTION_STATE esFlags);
|
||||||
|
|
||||||
|
[DllImport("Shell32.dll", EntryPoint = "ExtractIconExW", CharSet = CharSet.Unicode, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
|
||||||
|
private static extern int ExtractIconEx(string sFile, int iIndex, out IntPtr piLargeVersion, out IntPtr piSmallVersion, int amountIcons);
|
||||||
|
|
||||||
static APIHelper()
|
static APIHelper()
|
||||||
{
|
{
|
||||||
log = LogManager.GetCurrentClassLogger();
|
log = LogManager.GetCurrentClassLogger();
|
||||||
}
|
}
|
||||||
|
|
||||||
// More details about the API used: https://docs.microsoft.com/windows/win32/api/winbase/nf-winbase-setthreadexecutionstate
|
|
||||||
[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
|
|
||||||
static extern EXECUTION_STATE SetThreadExecutionState(EXECUTION_STATE esFlags);
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Sets the computer awake state using the native Win32 SetThreadExecutionState API. This
|
/// Sets the computer awake state using the native Win32 SetThreadExecutionState API. This
|
||||||
/// function is just a nice-to-have wrapper that helps avoid tracking the success or failure of
|
/// function is just a nice-to-have wrapper that helps avoid tracking the success or failure of
|
||||||
@@ -183,5 +187,23 @@ namespace Espresso.Shell.Core
|
|||||||
return string.Empty;
|
return string.Empty;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public static Icon? Extract(string file, int number, bool largeIcon)
|
||||||
|
{
|
||||||
|
IntPtr large;
|
||||||
|
IntPtr small;
|
||||||
|
ExtractIconEx(file, number, out large, out small, 1);
|
||||||
|
try
|
||||||
|
{
|
||||||
|
return Icon.FromHandle(largeIcon ? large : small);
|
||||||
|
}
|
||||||
|
catch
|
||||||
|
{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,6 +5,7 @@
|
|||||||
using Espresso.Shell.Models;
|
using Espresso.Shell.Models;
|
||||||
using System;
|
using System;
|
||||||
using System.Drawing;
|
using System.Drawing;
|
||||||
|
using System.Threading.Tasks;
|
||||||
using System.Windows.Forms;
|
using System.Windows.Forms;
|
||||||
|
|
||||||
namespace Espresso.Shell.Core
|
namespace Espresso.Shell.Core
|
||||||
@@ -23,14 +24,16 @@ namespace Espresso.Shell.Core
|
|||||||
trayIcon.Icon = icon;
|
trayIcon.Icon = icon;
|
||||||
trayIcon.ContextMenuStrip = contextMenu;
|
trayIcon.ContextMenuStrip = contextMenu;
|
||||||
trayIcon.Visible = true;
|
trayIcon.Visible = true;
|
||||||
|
|
||||||
|
Application.Run();
|
||||||
}
|
}
|
||||||
|
|
||||||
internal static void InitializeEspressoTray(EspressoMode mode, bool keepDisplayOn, Action indefiniteSelectionCallback, Action timedSelectionCallback)
|
internal static void InitializeEspressoTray(string text, EspressoMode mode, bool keepDisplayOn, Action indefiniteSelectionCallback, Action timedSelectionCallback)
|
||||||
{
|
{
|
||||||
var contextMenuStrip = new ContextMenuStrip();
|
var contextMenuStrip = new ContextMenuStrip();
|
||||||
|
|
||||||
// Main toolstrip.
|
// Main toolstrip.
|
||||||
var operationContextMenu = new ToolStrip();
|
var operationContextMenu = new ToolStripMenuItem();
|
||||||
operationContextMenu.Text = "Mode";
|
operationContextMenu.Text = "Mode";
|
||||||
|
|
||||||
// Indefinite keep-awake menu item.
|
// Indefinite keep-awake menu item.
|
||||||
@@ -40,13 +43,32 @@ namespace Espresso.Shell.Core
|
|||||||
{
|
{
|
||||||
indefiniteMenuItem.Checked = true;
|
indefiniteMenuItem.Checked = true;
|
||||||
}
|
}
|
||||||
operationContextMenu.Items.Add(indefiniteMenuItem);
|
|
||||||
|
|
||||||
// Timed keep-awake menu item
|
// Timed keep-awake menu item
|
||||||
|
var timedMenuItem = new ToolStripMenuItem();
|
||||||
|
timedMenuItem.Text = "Timed";
|
||||||
|
|
||||||
// Exit menu item
|
var halfHourMenuItem = new ToolStripMenuItem();
|
||||||
|
halfHourMenuItem.Text = "30 minutes";
|
||||||
|
|
||||||
// About menu item
|
var oneHourMenuItem = new ToolStripMenuItem();
|
||||||
|
halfHourMenuItem.Text = "1 hour";
|
||||||
|
|
||||||
|
var twoHousrMenuItem = new ToolStripMenuItem();
|
||||||
|
halfHourMenuItem.Text = "2 hours";
|
||||||
|
|
||||||
|
timedMenuItem.DropDownItems.Add(halfHourMenuItem);
|
||||||
|
timedMenuItem.DropDownItems.Add(oneHourMenuItem);
|
||||||
|
timedMenuItem.DropDownItems.Add(twoHousrMenuItem);
|
||||||
|
|
||||||
|
operationContextMenu.DropDownItems.Add(indefiniteMenuItem);
|
||||||
|
operationContextMenu.DropDownItems.Add(timedMenuItem);
|
||||||
|
|
||||||
|
contextMenuStrip.Items.Add(operationContextMenu);
|
||||||
|
|
||||||
|
#pragma warning disable CS8604 // Possible null reference argument.
|
||||||
|
Task.Factory.StartNew(() => InitializeTrayIcon(text, APIHelper.Extract("shell32.dll", 42, true), contextMenuStrip));
|
||||||
|
#pragma warning restore CS8604 // Possible null reference argument.
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -199,6 +199,8 @@ namespace Espresso.Shell
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TrayHelper.InitializeEspressoTray("Espresso", EspressoMode.INDEFINITE, true, new Action(()=>Console.WriteLine("test")), new Action(() => Console.WriteLine("test")));
|
||||||
|
|
||||||
new ManualResetEvent(false).WaitOne();
|
new ManualResetEvent(false).WaitOne();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user