Windows登录、锁屏的判定以及消息捕捉

http://blog.csdn.net/windless0530/article/details/5496469

如果窗口想获取相关消息,需要注册Session的Notification,需要Wtsapi32.dll中的两个函数:WTSRegisterSessionNotification和WTSUnRegisterSessionNotification,分别在窗口创建和销毁的时候调用。

typedef BOOL (WINAPI *WTSRegisterSessionNotification)(HWND, DWORD);
typedef BOOL (WINAPI *WTSUnRegisterSessionNotification)(HWND);

#define WTSAPI_DLL      _T("Wtsapi32.dll")

#define MY_WTS_SESSION_FUNCTION(fnType, fnName, arg)    /
    BOOL bRet = FALSE;  /
    HMODULE hLibrary = ::LoadLibrary(WTSAPI_DLL);   /
    if (hLibrary)   /
    {   /
        fnType fn   /
            = (fnType)::GetProcAddress(hLibrary, fnName);   /
        if (fn) /
        {   /
            bRet = fn##arg; /
        }   /
        ::FreeLibrary(hLibrary);    /
    }   /
    return bRet;


BOOL MyWTSRegisterSessionNotification(HWND hWnd, DWORD dwFlag)
{
    MY_WTS_SESSION_FUNCTION(WTSRegisterSessionNotification,
        "WTSRegisterSessionNotification", (hWnd, dwFlag));
}

BOOL MyWTSUnRegisterSessionNotification(HWND hWnd)
{
    MY_WTS_SESSION_FUNCTION(WTSUnRegisterSessionNotification,
        "WTSUnRegisterSessionNotification", (hWnd));
}

这样,在SESSION切换的时候,窗口就可以收到一个WM_WTSSESSION_CHANGE消息。

如果不依赖窗口,需要判断当前系统是否处于锁屏状态,有一段比较tricky的代码……就是试图SwitchDesktop,如果失败就认为在锁屏。

这种方法有另一个优点:可以判断ctrl+alt+delete后系统暂时“锁定”以及“解锁”的状态,而这种状态改变,窗口是没法收到WM_WTSESSIONCHANGE的。

typedef HDESK (WINAPI *PFNOPENDESKTOP)(LPSTR, DWORD, BOOL, ACCESS_MASK); 
typedef BOOL (WINAPI *PFNCLOSEDESKTOP)(HDESK); 
typedef BOOL (WINAPI *PFNSWITCHDESKTOP)(HDESK); 

BOOL PP_IsWorkStationLocked() 
{ 
    // note: we can't call OpenInputDesktop directly because it's not 
    // available on win 9x 
    BOOL bLocked = FALSE;

    // load user32.dll once only 
    static HMODULE hUser32 = LoadLibrary(_T("user32.dll"));

    if (hUser32) 
    { 
        static PFNOPENDESKTOP fnOpenDesktop = (PFNOPENDESKTOP)GetProcAddress(hUser32, "OpenDesktopA"); 
        static PFNCLOSEDESKTOP fnCloseDesktop = (PFNCLOSEDESKTOP)GetProcAddress(hUser32, "CloseDesktop"); 
        static PFNSWITCHDESKTOP fnSwitchDesktop = (PFNSWITCHDESKTOP)GetProcAddress(hUser32, "SwitchDesktop"); 

        if (fnOpenDesktop && fnCloseDesktop && fnSwitchDesktop) 
        { 
            HDESK hDesk = fnOpenDesktop("Default", 0, FALSE, DESKTOP_SWITCHDESKTOP); 

            if (hDesk) 
            { 
                bLocked = !fnSwitchDesktop(hDesk); 
                // cleanup 
                fnCloseDesktop(hDesk); 
            } 
        } 
    } 

    return bLocked;
} 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值