Created
October 24, 2017 18:10
-
-
Save ysc3839/137390345c27411d03e3616d8e7bc564 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[void][System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") | |
Add-Type -TypeDefinition @" | |
using System; | |
using System.Drawing; | |
using System.Runtime.InteropServices; | |
namespace System { | |
public class IconExtractor { | |
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; } | |
} | |
[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); | |
} | |
} | |
"@ -ReferencedAssemblies System.Drawing | |
$ES_AWAYMODE_REQUIRED = [uint32]"0x00000040" | |
$ES_CONTINUOUS = [uint32]"0x80000000" | |
$ES_DISPLAY_REQUIRED = [uint32]"0x00000002" | |
$ES_SYSTEM_REQUIRED = [uint32]"0x00000001" | |
$ste = Add-Type -memberDefinition @' | |
[DllImport("kernel32.dll", CharSet = CharSet.Auto,SetLastError = true)] | |
public static extern void SetThreadExecutionState(uint esFlags); | |
'@ -name System -namespace Win32 -passThru | |
$contextMenu = New-Object System.Windows.Forms.ContextMenu | |
$menuItemEnable = New-Object System.Windows.Forms.MenuItem -ArgumentList "Enable" | |
$menuItemExit = New-Object System.Windows.Forms.MenuItem -ArgumentList "Exit" | |
$menuItemEnable.Checked = $True | |
$menuItemEnable.add_Click({ | |
$menuItemEnable.Checked = !$menuItemEnable.Checked | |
If ($menuItemEnable.Checked) { | |
$ste::SetThreadExecutionState($ES_CONTINUOUS -bor $ES_DISPLAY_REQUIRED) | |
} Else { | |
$ste::SetThreadExecutionState($ES_CONTINUOUS) | |
} | |
}) | |
$menuItemExit.add_Click({ | |
$notifyIcon.Dispose() | |
[void][System.Windows.Forms.Application]::Exit() | |
}) | |
[void]$contextMenu.MenuItems.Add($menuItemEnable) | |
[void]$contextMenu.MenuItems.Add($menuItemExit) | |
$notifyIcon = New-Object System.Windows.Forms.NotifyIcon | |
$notifyIcon.Icon = [System.IconExtractor]::Extract("imageres.dll", 96, $False) | |
$notifyIcon.Text = "Prevent Sleep" | |
$notifyIcon.ContextMenu = $contextMenu | |
$notifyIcon.Visible = $True | |
$ste::SetThreadExecutionState($ES_CONTINUOUS -bor $ES_DISPLAY_REQUIRED) | |
[void][System.Windows.Forms.Application]::Run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment