UAC的限制引起WM_DROPFILES无法响应的解决办法

Home / C++ 百晓生 2018-7-26 3215

由于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得到函数地址就能使用。

在你的程序中添加以下函数:

#define MSGFLT_ADD 1
#define MSGFLT_REMOVE 2
typedef BOOL(WINAPI *_ChangeWindowMessageFilter)(UINT message, DWORD dwFlag);
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;
}

在OnInitDialog中添加函数调用:

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

最后别忘记

本文链接:https://www.it72.com/12395.htm

推荐阅读
最新回复 (0)
返回