我正在尝试检测笔记本电脑盖何时打开和关闭,应该非常简单。我似乎可以正确注册该事件,但是当我关闭笔记本电脑窗口时我没有收到通知。
这是DLL导入
(DLL 代码:http ://www.pinvoke.net/default.aspx/user32/registerpowersettingnotification.html)(GUID_LIDCLOSE_ACTION:http ://social.msdn.microsoft.com/Forums/en-US/tabletandtouch/thread/0bbf90be -9322-47fb-bfa4-016b57211b3a )
[DllImport(@"User32", SetLastError = true,
EntryPoint = "RegisterPowerSettingNotification",
CallingConvention = CallingConvention.StdCall)]
private static extern IntPtr RegisterPowerSettingNotification(
IntPtr hRecipient,
ref Guid PowerSettingGuid,
Int32 Flags);
static Guid GUID_LIDCLOSE_ACTION =
new Guid(0xBA3E0F4D, 0xB817, 0x4094, 0xA2, 0xD1,
0xD5, 0x63, 0x79, 0xE6, 0xA0, 0xF3);
private const int WM_POWERBROADCAST = 0x0218;
private const int DEVICE_NOTIFY_WINDOW_HANDLE = 0x00000000;
const int PBT_POWERSETTINGCHANGE = 0x8013; // DPPE
[StructLayout(LayoutKind.Sequential, Pack = 4)]
internal struct POWERBROADCAST_SETTING
{
public Guid PowerSetting;
public uint DataLength;
public byte Data;
}
然后这是我注册 GUID_LIDCLOSE_ACTION 事件的方式:
private void registerLidClosedNotification()
{
IntPtr hWnd = this.Handle;
IntPtr ret = RegisterPowerSettingNotification(hWnd,
ref GUID_LIDCLOSE_ACTION,
DEVICE_NOTIFY_WINDOW_HANDLE);
Debug.WriteLine("Registered: " + ret.ToString());
Debug.WriteLIne("LastError:" + Marshal.GetLastWin32Error().ToString());
}
这是它的输出:
注册号:6867560
上次错误:0
对我来说看上去很好。
然后我应该在哪里收到消息:
private static IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
{
Debug.WriteLine("Entered: WndProc"); // we never make it even this far!
那么,如果它已注册,为什么不进入 WndProc 函数:[