Skip to content

Commit

Permalink
Dark Mode GUI
Browse files Browse the repository at this point in the history
  • Loading branch information
steve02081504 committed Dec 30, 2023
1 parent 7812d9d commit 35cace3
Show file tree
Hide file tree
Showing 6 changed files with 115 additions and 29 deletions.
15 changes: 11 additions & 4 deletions GUI/GUI.ps1
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
[CmdletBinding()]
param([string]$ConfingFile,[string]$Localize)
param(
[string]$ConfingFile,
[string]$Localize,
[ValidateSet('Light', 'Dark', 'Auto')]
[string]$UIMode = 'Auto'
)

$Prarms = [hashtable]$PSBoundParameters

if (!$Localize) {
# 本机语言
$Localize = (Get-Culture).Name
$Prarms.Localize = (Get-Culture).Name
}

if (($PSVersionTable.PSEdition -eq "Core") -and (Get-Command powershell -ErrorAction Ignore)) {
powershell -NoProfile -ExecutionPolicy Bypass -File $PSScriptRoot\src\Main.ps1 -ConfingFile $ConfingFile -Localize $Localize | Out-Null
powershell -NoProfile -ExecutionPolicy Bypass -File $PSScriptRoot\src\Main.ps1 @Prarms | Out-Null
return
}

.$PSScriptRoot\src\Main.ps1 -ConfingFile $ConfingFile -Localize $Localize | Out-Null
.$PSScriptRoot\src\Main.ps1 @Prarms | Out-Null
35 changes: 35 additions & 0 deletions GUI/src/DarkMode.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
if($UIMode -eq 'Auto') {
$DarkMode = Get-ItemPropertyValue -Path "HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\Themes\Personalize" -Name "AppsUseLightTheme" -ErrorAction SilentlyContinue
$DarkMode = $DarkMode -eq 0
}
else {
$DarkMode = $UIMode -eq 'Dark'
}

if ($DarkMode) {
$Script:refs['MainForm'].BackColor = [System.Drawing.ColorTranslator]::FromHtml('#333333')
$Script:refs['MainForm'].ForeColor = [System.Drawing.ColorTranslator]::FromHtml('#FFFFFF')
$Script:refs.Values | Where-Object { $_.GetType().Name -eq 'TextBox' } | ForEach-Object {
$_.BackColor = 'WindowFrame'
$_.ForeColor = 'Window'
$_.BorderStyle = 'FixedSingle'
}
$Script:refs.Values | Where-Object { $_.GetType().Name -eq 'Button' } | ForEach-Object {
$_.BackColor = 'WindowFrame'
$_.ForeColor = 'Window'
$_.FlatStyle = 'Flat'
}
$Script:refs.Values | Where-Object { $_.GetType().Name -eq 'CheckBox' } | ForEach-Object {
$_.ForeColor = 'Window'
$_.FlatStyle = 'Flat'
}
$Script:refs.Values | Where-Object { $_.GetType().Name -eq 'GroupBox' } | ForEach-Object {
$_.ForeColor = [System.Drawing.ColorTranslator]::FromHtml('#FFFFFF')
}
# DWMWA_USE_IMMERSIVE_DARK_MODE
[Dwm]::SetWindowAttribute($Script:refs['MainForm'].Handle, 20, 1)
# DWMWA_MICA_EFFECT
[Dwm]::SetWindowAttribute($Script:refs['MainForm'].Handle, 1029 , 1)
# DWMWA_BORDER_COLOR
[Dwm]::SetWindowBorderColor($Script:refs['MainForm'].Handle, [System.Drawing.ColorTranslator]::FromHtml('#FFFFFF'))
}
6 changes: 5 additions & 1 deletion GUI/src/Main.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
param (
[string]$ConfingFile,
#本地化信息
[string]$Localize
[string]$Localize,
[ValidateSet('Light', 'Dark', 'Auto')]
[string]$UIMode = 'Auto'
)

# ScriptBlock to Execute in STA Runspace
Expand All @@ -17,6 +19,8 @@ $sbGUI = {

. "$BaseDir\Events.ps1"

. "$BaseDir\DarkMode.ps1"

#region Other Actions Before ShowDialog

try {
Expand Down
13 changes: 13 additions & 0 deletions GUI/src/UItools.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,19 @@ public class psd {
[System.Runtime.InteropServices.DllImport("user32.dll")]
public static extern bool SetProcessDPIAware();
}
using System.Drawing;
public class Dwm {
[DllImport("dwmapi.dll")]
public static extern int DwmSetWindowAttribute(IntPtr hwnd, int attr, int[] attrValue, int attrSize);
public static int SetWindowAttribute(IntPtr hwnd, int attr, int attrValue) {
return DwmSetWindowAttribute(hwnd, attr, new int[] { attrValue }, 4);
}
public static int SetWindowBorderColor(IntPtr hwnd, Color color){
return SetWindowAttribute(hwnd, 34, int.Parse(`$"{color.B:X2}{color.G:X2}{color.R:X2}", System.Globalization.NumberStyles.HexNumber));
}
}
"@ -ReferencedAssemblies System.Windows.Forms, System.Drawing, System.Drawing.Primitives, System.Net.Primitives, System.ComponentModel.Primitives, Microsoft.Win32.Primitives
$script:tscale = 1

Expand Down
38 changes: 26 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,23 +34,37 @@ ps12exe .\source.ps1 .\target.exe

compiles `source.ps1` into the executable target.exe (if `.\target.exe` is omitted, output is written to `.\source.exe`).

## Comparison
## Project Comparison

Compared to [`MScholtes/PS2EXE@678a892`](https://github.com/MScholtes/PS2EXE/tree/678a89270f4ef4b636b69db46b31e1b4e0a9e1c5), this project:
When compared to [`MScholtes/PS2EXE@678a892`](https://github.com/MScholtes/PS2EXE/tree/678a89270f4ef4b636b69db46b31e1b4e0a9e1c5), this project introduces the following enhancements:

- Adds [powerful preprocessing features](#prepossessing) that allow you to preprocess the script before compiling (instead of copy-pasting everything and embedding it into the script)
- Special parameters are no longer enabled by default in the generated files, but can be enabled with the new `-SepcArgsHandling` parameter if needed.
- The `-CompilerOptions` parameter has been added to allow you to further customise the generated executable.
- Added [`-Minifyer` parameter](#minifyer) to allow you to pre-process scripts before compilation to get smaller generated executables
- Support for compiling scripts and including files from urls, support for downloading icons from urls
- Optimised option handling and window title display under the `-noConsole` parameter, you can now customise the title of popup windows by setting `$Host.UI.RawUI.WindowTitle`.
- Removed exe files from code repository
- Better multi-language supportpure script GUI
- Separated the cs file from the ps1 file, easier to read and maintain.
- and more...
- Incorporates [robust preprocessing capabilities](#prepossessing), enabling you to preprocess scripts prior to compilation, eliminating the need to embed everything into the script manually.
- Special parameters are not enabled by default in the generated files anymore, but can be activated with the new `-SepcArgsHandling` parameter if required.
- A new `-CompilerOptions` parameter has been introduced, providing additional customization options for the generated executable.
- The [`-Minifyer` parameter](#minifyer) has been added, allowing for script preprocessing before compilation, resulting in smaller executables.
- Supports compiling scripts and including files from URLs, as well as downloading icons from URLs.
- Under the `-noConsole` parameter, option handling and window title display have been optimized. You can now personalize the title of popup windows by setting `$Host.UI.RawUI.WindowTitle`.
- Executable files have been removed from the code repository.
- Enhanced multi-language support, pure script GUI, and dark mode support.
- The C# file has been separated from the PowerShell file, making it easier to read and maintain.
- And much more...

## Parameter

### GUI parameters

```powershell
ps12exeGUI [[-ConfingFile] '<filename>'] [-Localize '<language>'] [[-UIMode] 'Dark'|'Light'|'Auto']
```

```text
ConfingFile = path to config file (default is none)
Localize = language code (default is current system language, try to load 'en-UK' if no corresponding language file is available, if still not available, iterate through all language files until available)
UIMode = UI mode (default is Auto)
```

### Console parameters

```powershell
[input |] ps12exe [[-inputFile] '<filename|url>' | -Content '<script>'] [-outputFile '<filename>']
[-CompilerOptions '<options>'] [-TempDir '<directory>'] [-minifyer '<scriptblock>'] [-noConsole]
Expand Down
37 changes: 25 additions & 12 deletions README_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,23 +32,36 @@ ps12exe .\source.ps1 .\target.exe

`source.ps1`编译为`target.exe`(如果省略`.\target.exe`,输出将写入`.\source.exe`)。

## 比较
## 优势对比

[`MScholtes/PS2EXE@678a892`](https://github.com/MScholtes/PS2EXE/tree/678a89270f4ef4b636b69db46b31e1b4e0a9e1c5)相比,该项目
相较于[`MScholtes/PS2EXE@678a892`](https://github.com/MScholtes/PS2EXE/tree/678a89270f4ef4b636b69db46b31e1b4e0a9e1c5),本项目带来了以下改进

- 追加了[强大的预处理功能](#预处理)允许你在编译前对脚本进行预处理(而不是将所有内容复制粘贴嵌入到脚本中)
- 生成的文件中特殊参数不再默认启用,如有需要可使用`-SepcArgsHandling`参数启用
- 追加了`-CompilerOptions`参数,允许你进一步定制生成的可执行文件
- 追加了[`-Minifyer`参数](#minifyer)允许你在编译前对脚本进行预处理,以获得更小的生成可执行文件
- 支持自url编译脚本和url include文件、支持自url下载图标
- 优化了`-noConsole`参数下的选项处理和窗口标题显示,你现在可以通过设置`$Host.UI.RawUI.WindowTitle`来自定义弹窗的标题
- 移除了代码仓库中的exe文件
- 更好的多语言纯脚本GUI
- 将cs文件从ps1文件中分离出来,阅读和维护更方便
- 以及更多...
- 引入了[强大的预处理功能](#预处理)让你在编译前就能预处理脚本,无需再复制粘贴所有内容到脚本中。
- 生成的文件中不再默认启用特殊参数,但如果需要,可以通过新的`-SepcArgsHandling`参数启用
- 新增`-CompilerOptions`参数,让你能进一步定制生成的可执行文件。
- 新增[`-Minifyer`参数](#minifyer)让你在编译前预处理脚本,生成更小的可执行文件。
- 支持从URL编译脚本和包含文件,支持从URL下载图标。
- `-noConsole`参数下,优化了选项处理和窗口标题显示,你现在可以通过设置`$Host.UI.RawUI.WindowTitle`自定义弹出窗口的标题。
- 从代码仓库中移除了exe文件。
- 更好的多语言支持、纯脚本GUI,支持深色模式。
- 将cs文件从ps1文件中分离,更易于阅读和维护。
- 还有更多...

## 参数

### GUI参数

```powershell
ps12exeGUI [[-ConfingFile] '<filename>'] [-Localize '<language>'] [[-UIMode] 'Dark'|'Light'|'Auto']
```

```text
ConfingFile = 配置文件的路径(默认为 无)
Localize = 语言代码(默认为当前系统语言,没有对应的语言文件时尝试加载'en-UK',如果仍然没有则遍历所有语言文件直到可用)
UIMode = 界面模式(默认为 Auto)
```

### 控制台参数

```powershell
[input |] ps12exe [[-inputFile] '<filename|url>' | -Content '<script>'] [-outputFile '<filename>']
Expand Down

0 comments on commit 35cace3

Please sign in to comment.