-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathShow-Message.ps1
More file actions
65 lines (56 loc) · 1.91 KB
/
Show-Message.ps1
File metadata and controls
65 lines (56 loc) · 1.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
<#
.SYNOPSIS
Shows WM_SETTINGCHANGE broadcast messages
.DESCRIPTION
Used in the development of the VisualEffects module
.NOTES
To stop the script, just close the PowerShell window
#>
$code = @'
using System;
using System.Windows.Forms;
using System.Runtime.InteropServices;
public class SettingsChangedHandlerForm:Form
{
private static int WM_SETTINGCHANGE = 0x1A;
public class SettingChangedArgs : EventArgs
{
public IntPtr wParam {get; set;}
public string msg {get; set;}
}
public event EventHandler<SettingChangedArgs> SettingChanged;
protected override void WndProc(ref System.Windows.Forms.Message msg)
{
if (msg.Msg == WM_SETTINGCHANGE){
var text = Marshal.PtrToStringAuto(msg.LParam);
var args = new SettingChangedArgs {wParam = msg.WParam, msg = text};
SettingChanged.Invoke(this, args);
}
base.WndProc(ref msg);
}
public SettingsChangedHandlerForm(EventHandler<SettingChangedArgs> SettingChanged)
{
this.Visible = false;
this.WindowState = FormWindowState.Minimized;
this.SettingChanged = SettingChanged;
}
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
this.Activate();
}
}
'@
$referencedAssemblies = @('System.Windows.Forms')
if ($PSEdition -eq 'Core') {
$referencedAssemblies += 'System.ComponentModel.Primitives', 'System.Windows.Forms.Primitives'
}
Add-Type -TypeDefinition $code -ReferencedAssemblies $referencedAssemblies
$settingChangedEventHandler = {
param($EventSender, $SettingChanged)
'[{0}] WM_SETTINGCHANGE W:0x{1:x4} L:{2}' -f (Get-Date), $SettingChanged.wParam, $SettingChanged.msg | Out-Host
}
$form = [SettingsChangedHandlerForm]::new($settingChangedEventHandler)
Write-Host -ForegroundColor Green 'Listening for messages...'
# note this will block execution
[System.Windows.Forms.Application]::Run($form)