-
Notifications
You must be signed in to change notification settings - Fork 0
/
CheckForLaptopCloseOpenLid.cs
230 lines (196 loc) · 8.98 KB
/
CheckForLaptopCloseOpenLid.cs
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing;
using System.Windows.Forms;
using System.Threading;
using System.DirectoryServices.AccountManagement;
using System.ComponentModel;
using Microsoft.Win32;
//namespace TestWindowsDevTracker
//{
// public partial class CheckForLaptopCloseOpenLid
// {
// [DllImport(@"User32", SetLastError = true, EntryPoint = "RegisterPowerSettingNotification",
// CallingConvention = CallingConvention.StdCall)]
// private static extern IntPtr RegisterPowerSettingNotification(IntPtr hRecipient, ref Guid PowerSettingGuid, Int32 Flags);
// static Guid GUID_LIDSWITCH_STATE_CHANGE = new Guid(0xBA3E0F4D, 0xB817, 0x4094, 0xA2, 0xD1, 0xD5, 0x63, 0x79, 0xE6, 0xA0, 0xF3);
// private const int DEVICE_NOTIFY_WINDOW_HANDLE = 0x00000000;
// private const int WM_POWERBROADCAST = 0x0218;
// const int PBT_POWERSETTINGCHANGE = 0x8013;
// [StructLayout(LayoutKind.Sequential, Pack = 4)]
// internal struct POWERBROADCAST_SETTING
// {
// public Guid PowerSetting;
// public uint DataLength;
// public byte Data;
// }
// private bool? _previousLidState = null;
// public CheckForLaptopCloseOpenLid(IntPtr handle)
// {
// RegisterForPowerNotifications();
// }
// [SecurityPermissionAttribute(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.UnmanagedCode)]
// protected override void WndProc(ref Message m)
// {
// switch (m.Msg)
// {
// case WM_POWERBROADCAST:
// OnPowerBroadcast(m.WParam, m.LParam);
// break;
// default:
// break;
// }
// base.WndProc(ref m);
// }
// private void RegisterForPowerNotifications()
// {
// IntPtr handle = this.Handle;
// Debug.WriteLine("Handle: " + handle.ToString()); //If this line is omitted, then lastError = 1008 which is ERROR_NO_TOKEN, otherwise, lastError = 0
// IntPtr hLIDSWITCHSTATECHANGE = RegisterPowerSettingNotification(handle,
// ref GUID_LIDSWITCH_STATE_CHANGE,
// DEVICE_NOTIFY_WINDOW_HANDLE);
// Debug.WriteLine("Registered: " + hLIDSWITCHSTATECHANGE.ToString());
// Debug.WriteLine("LastError:" + Marshal.GetLastWin32Error().ToString());
// }
// private void OnPowerBroadcast(IntPtr wParam, IntPtr lParam)
// {
// if ((int)wParam == PBT_POWERSETTINGCHANGE)
// {
// POWERBROADCAST_SETTING ps = (POWERBROADCAST_SETTING)Marshal.PtrToStructure(lParam, typeof(POWERBROADCAST_SETTING));
// IntPtr pData = (IntPtr)((int)lParam + Marshal.SizeOf(ps));
// Int32 iData = (Int32)Marshal.PtrToStructure(pData, typeof(Int32));
// if (ps.PowerSetting == GUID_LIDSWITCH_STATE_CHANGE)
// {
// bool isLidOpen = ps.Data != 0;
// if (!isLidOpen == _previousLidState)
// {
// LidStatusChanged(isLidOpen);
// }
// _previousLidState = isLidOpen;
// }
// }
// }
// private void LidStatusChanged(bool isLidOpen)
// {
// if (isLidOpen)
// {
// //Do some action on lid open event
// MessageBox.Show("Lid is now open");
// }
// else
// {
// //Do some action on lid close event
// MessageBox.Show("Lid is now closed");
// }
// }
// }
//}
//public partial class CheckForLaptopCloseOpenLid //: Window
//{
// [DllImport("user32.dll")]
// static extern IntPtr SetWinEventHook(uint eventMin, uint eventMax, IntPtr hmodWinEventProc, WinEventDelegate lpfnWinEventProc, uint idProcess, uint idThread, uint dwFlags);
// [DllImport(@"User32", SetLastError = true, EntryPoint = "RegisterPowerSettingNotification",
// CallingConvention = CallingConvention.StdCall)]
// private static extern IntPtr RegisterPowerSettingNotification(IntPtr hRecipient, ref Guid PowerSettingGuid,
// Int32 Flags);
// delegate void WinEventDelegate(IntPtr hWinEventHook, uint eventType, IntPtr hwnd, int idObject, int idChild, uint dwEventThread, uint dwmsEventTime);
// IntPtr m_hhook = IntPtr.Zero;
// private const uint WINEVENT_OUTOFCONTEXT = 0;
// WinEventDelegate dele = null;
// internal struct POWERBROADCAST_SETTING
// {
// public Guid PowerSetting;
// public uint DataLength;
// public byte Data;
// }
// Guid GUID_LIDSWITCH_STATE_CHANGE = new Guid(0xBA3E0F4D, 0xB817, 0x4094, 0xA2, 0xD1, 0xD5, 0x63, 0x79, 0xE6, 0xA0, 0xF3);
// const int DEVICE_NOTIFY_WINDOW_HANDLE = 0x00000000;
// const int WM_POWERBROADCAST = 0x0218;
// const int PBT_POWERSETTINGCHANGE = 0x8013;
// private bool? _previousLidState = null;
// public CheckForLaptopCloseOpenLid(IntPtr hwnd)
// {
// RegisterForPowerNotifications(hwnd);
// //IntPtr hwnd = new WindowInteropHelper(this).Handle;
// //HwndSource.FromHwnd(hwnd).AddHook(new HwndSourceHook(WndProc));
// m_hhook = SetWinEventHook(WM_POWERBROADCAST, WM_POWERBROADCAST, IntPtr.Zero, dele, 0, 0, WINEVENT_OUTOFCONTEXT);
// }
// //IntPtr handle = new WindowInteropHelper(Application.Current.Windows[0]).Handle; to this: IntPtr handle = new WindowInteropHelper(this).Handle;
// private void RegisterForPowerNotifications(IntPtr handle)
// {
// Console.WriteLine($"RegisterForPowerNotifications {handle}");
// //IntPtr handle = new WindowInteropHelper(Application.Current.Windows[0]).Handle;
// //IntPtr handle = this.//new WindowInteropHelper(this).Handle;
// IntPtr hLIDSWITCHSTATECHANGE = RegisterPowerSettingNotification(handle,
// ref GUID_LIDSWITCH_STATE_CHANGE,
// DEVICE_NOTIFY_WINDOW_HANDLE);
// }
// IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
// {
// Console.WriteLine($"WParam {wParam}, LParam {lParam}, msg {msg}");
// switch (msg)
// {
// case WM_POWERBROADCAST:
// OnPowerBroadcast(wParam, lParam);
// break;
// default:
// break;
// }
// return IntPtr.Zero;
// }
// //[System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand, Name = "FullTrust")]
// //protected override IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled/*ref Message m*/)
// //{
// // // Listen for operating system messages.
// // switch (m.Msg)
// // {
// // // The WM_ACTIVATEAPP message occurs when the application
// // // becomes the active application or becomes inactive.
// // case WM_ACTIVATEAPP:
// // // The WParam value identifies what is occurring.
// // appActive = (((int)m.WParam != 0));
// // // Invalidate to get new text painted.
// // this.Invalidate();
// // break;
// // }
// // base.WndProc(ref m);
// // return (IntPtr)0;
// //}
// private void OnPowerBroadcast(IntPtr wParam, IntPtr lParam)
// {
// if ((int)wParam == PBT_POWERSETTINGCHANGE)
// {
// POWERBROADCAST_SETTING ps = (POWERBROADCAST_SETTING)Marshal.PtrToStructure(lParam, typeof(POWERBROADCAST_SETTING));
// IntPtr pData = (IntPtr)((int)lParam + Marshal.SizeOf(ps));
// Int32 iData = (Int32)Marshal.PtrToStructure(pData, typeof(Int32));
// if (ps.PowerSetting == GUID_LIDSWITCH_STATE_CHANGE)
// {
// bool isLidOpen = ps.Data != 0;
// if (!isLidOpen == _previousLidState)
// {
// LidStatusChanged(isLidOpen);
// }
// _previousLidState = isLidOpen;
// }
// }
// }
// private void LidStatusChanged(bool isLidOpen)
// {
// if (isLidOpen)
// {
// //Do some action on lid open event
// Debug.WriteLine("{0}: Lid opened!", DateTime.Now);
// }
// else
// {
// //Do some action on lid close event
// Debug.WriteLine("{0}: Lid closed!", DateTime.Now);
// }
// }
//}
//}