您的位置:首页 > 其它

未公开函数MessageBoxTimeOut 实现定时消息(ZT) MFC实现MessageBox自动消失

2014-04-16 10:45 483 查看
http://www.blogjava.net/baicker/archive/2007/07/13/130072.html


#include<windows.h>
#include<tchar.h>

//Functions&otherdefinitionsrequired-->
typedefint(__stdcall*MSGBOXAAPI)(INHWNDhWnd,
INLPCSTRlpText,INLPCSTRlpCaption,
INUINTuType,INWORDwLanguageId,INDWORDdwMilliseconds);
typedefint(__stdcall*MSGBOXWAPI)(INHWNDhWnd,
INLPCWSTRlpText,INLPCWSTRlpCaption,
INUINTuType,INWORDwLanguageId,INDWORDdwMilliseconds);

intMessageBoxTimeoutA(INHWNDhWnd,INLPCSTRlpText,
INLPCSTRlpCaption,INUINTuType,
INWORDwLanguageId,INDWORDdwMilliseconds);
intMessageBoxTimeoutW(INHWNDhWnd,INLPCWSTRlpText,
INLPCWSTRlpCaption,INUINTuType,
INWORDwLanguageId,INDWORDdwMilliseconds);

#ifdefUNICODE
#defineMessageBoxTimeoutMessageBoxTimeoutW
#else
#defineMessageBoxTimeoutMessageBoxTimeoutA
#endif

#defineMB_TIMEDOUT32000

intMessageBoxTimeoutA(HWNDhWnd,LPCSTRlpText,
LPCSTRlpCaption,UINTuType,WORDwLanguageId,
DWORDdwMilliseconds)
{
staticMSGBOXAAPIMsgBoxTOA=NULL;

if(!MsgBoxTOA)
{
HMODULEhUser32=GetModuleHandle(_T("user32.dll"));
if(hUser32)
{
MsgBoxTOA=(MSGBOXAAPI)GetProcAddress(hUser32,
"MessageBoxTimeoutA");
//fallthroughto'if(MsgBoxTOA)...'
}
else
{
//stuffhappened,addcodetohandleithere
//(possiblyjustcallMessageBox())
return0;
}
}

if(MsgBoxTOA)
{
returnMsgBoxTOA(hWnd,lpText,lpCaption,
uType,wLanguageId,dwMilliseconds);
}

return0;
}

intMessageBoxTimeoutW(HWNDhWnd,LPCWSTRlpText,
LPCWSTRlpCaption,UINTuType,WORDwLanguageId,DWORDdwMilliseconds)
{
staticMSGBOXWAPIMsgBoxTOW=NULL;

if(!MsgBoxTOW)
{
HMODULEhUser32=GetModuleHandle(_T("user32.dll"));
if(hUser32)
{
MsgBoxTOW=(MSGBOXWAPI)GetProcAddress(hUser32,
"MessageBoxTimeoutW");
//fallthroughto'if(MsgBoxTOW)...'
}
else
{
//stuffhappened,addcodetohandleithere
//(possiblyjustcallMessageBox())
return0;
}
}

if(MsgBoxTOW)
{
returnMsgBoxTOW(hWnd,lpText,lpCaption,
uType,wLanguageId,dwMilliseconds);
}

return0;
}
//Endrequireddefinitions<--
//Callthefunctionasfollows:

//Collapse
//youmustloaduser32.dllbeforecallingthefunction
main()
{
HMODULEhUser32=LoadLibrary(_T("user32.dll"));

if(hUser32)
{
intiRet=0;
UINTuiFlags=MB_OK|MB_SETFOREGROUND|MB_SYSTEMMODAL|MB_ICONINFORMATION;

iRet=MessageBoxTimeout(NULL,_T("Testatimeoutof2seconds."),
_T("MessageBoxTimeoutTest"),uiFlags,0,2000);
//iRetwill=1

uiFlags=MB_YESNO|MB_SETFOREGROUND|MB_SYSTEMMODAL|MB_ICONINFORMATION;

iRet=MessageBoxTimeout(NULL,_T("Testatimeoutof5seconds."),
_T("MessageBoxTimeoutTest"),uiFlags,0,5000);
//iRetwill=MB_TIMEDOUTifnobuttonspressed,buttonvaluesotherwise

//onlyunloaduser32.dllwhenyouhavenofurtherneed
//fortheMessageBoxTimeoutfunction
FreeLibrary(hUser32);
}
}



                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: