[C#]-Detect Event When Lid Open/Close

protected override void WndProc(ref Message m)
        {
            switch (m.Msg)
            {
                case WM_POWERBROADCAST:
                    OnPowerBroadcast(m.WParam, m.LParam);
                  
                    
                    switch (m.WParam.ToInt32())
                    {
                        case PBT_APMPOWERSTATUSCHANGE:
                          // Disconnect Power Cable
                           
                        break;

                        case PBT_APMQUERYSUSPEND:
                        case PBT_APMSUSPEND:
                            IntPtr deny = new IntPtr(BROADCAST_QUERY_DENY);
                            if (deny != null)
                            {
                                m.Result = deny;
                            }
                            break;          
                    }
                    
                    break;
                default:
                    break;
            }
            base.WndProc(ref m);
        }
private void OnPowerBroadcast(IntPtr wParam, IntPtr lParam)
        {
            bool isTurnOnSound = false;

            Debug.WriteLine("OnPowerBroadcast: " + wParam);
            
            if ((int)wParam == PBT_POWERSETTINGCHANGE)              // 32787
            {
                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)
                    {
                        if( !isLidOpen )    // When Lid Close
                        {
                             // TODO
                        }
                    }

                    _previousLidState = isLidOpen;
                }
        
            }
        }

댓글 남기기

이메일은 공개되지 않습니다. 필수 입력창은 * 로 표시되어 있습니다