Vista/Win7上WM_DROPFILES无法响应的解决办法

来源:互联网 发布:万方数据库是免费的吗 编辑:程序博客网 时间:2024/04/30 23:57

在Vista和Win7上,由于UAC的限制,WM_DROPFILES只能由权限较低的APP拖拽到权限较高的APP,反之如果从权限较高的APP拖拽到低权限的APP上,WM_DROPFILES不会被发送到低权限的APP消息队列。

所以,WM_DROPFILES会有时候变得不能响应。

解决的办法,使用ChangeWindowMessageFilter注册WM_DROPFILES这个MEESSAGE。

 

ChangeWindowMessageFilter是Vista以上的一个API,WinXP下并没有。

这个API在User32.dll中,使用时LoadLibrary,GetProcAddress得到函数地址就能使用。

 

//register global messages for vista win7.
BOOL AllowMeesageForVistaAbove(UINT uMessageID, BOOL bAllow)
{
    BOOL bResult = FALSE;
    HMODULE hUserMod = NULL;
    //vista and later
    hUserMod = LoadLibrary(_T("user32.dll"));
    if( NULL == hUserMod )
    {
        return FALSE;
    }
   
    _ChangeWindowMessageFilter pChangeWindowMessageFilter =
        (_ChangeWindowMessageFilter)GetProcAddress( hUserMod, "ChangeWindowMessageFilter");
    if( NULL == pChangeWindowMessageFilter )
    {
        return FALSE;
    }
    bResult = pChangeWindowMessageFilter( uMessageID, bAllow ? 1 : 2 );//MSGFLT_ADD: 1, MSGFLT_REMOVE: 2
   
    if( NULL != hUserMod )
    {
        FreeLibrary( hUserMod );
    }

    return bResult;
}

 

然后再合适的地方,调用这个函数。

 

#define MSGFLT_ADD 1

#define MSGFLT_REMOVE 2

 

AllowMeesageForVistaAbove(SPI_SETANIMATION, MSGFLT_ADD);

 

//allow drop files
AllowMeesageForVistaAbove(WM_DROPFILES, MSGFLT_ADD);

 

这样就能在Vista于Win7上来使用这两个消息了。

原创粉丝点击