1.参数的使用(MB_SYSTEMMODAL和MB_SERVICE_NOTIFICATION)
MB_SYSTEMMODAL 所有窗口的Modal模式,含 WS_EX_TOPMOST 风格(该风格可通过MB_TOPMOST单独设定)
MB_SERVICE_NOTIFICATION 显示在当前桌面,即使没有用户登录(一般用于service 服务)
2.MessageBoxIndirect (一般用于自定义图标)
MSGBOXPARAMS msgBox;
msgBox.cbSize = sizeof(MSGBOXPARAMS);
msgBox.dwStyle = MB_USERICON | nButtons;
msgBox.hInstance = AfxGetApp()->m_hInstance;
msgBox.hwndOwner = NULL;
msgBox.lpszCaption = lpszTitle;
msgBox.lpszIcon = MAKEINTRESOURCE(nResourceID);//资源中的ICON
msgBox.lpszText = lpszText;
MessageBoxIndirect(&msgBox);
3.WH_CBT 钩子 (一般用于设定窗口位置和修改按钮文本)
a)设定窗口的位置(http://support.microsoft.com/default.aspx?scid=kb;en-us;180936)
HHOOK hHook;
LRESULT CALLBACK CBTProc(int nCode,WPARAM wParam,LPARAM lParam)
{
if(nCode == HCBT_ACTIVATE)
{
RECT rect;
GetWindowRect(hWnd, &rect);
MoveWindow((HWND)hWnd, 0, 0, rect.right-rect.left, rect.bottom-rect.top, TRUE);
}
return CallNextHookEx(hHook, nCode, wParam, lParam);
}
使用方法
hHook = SetWindowsHookEx(WH_CBT, CBTProc, GetModuleHandle(NULL), 0);
MessageBox(NULL, "hello world!", "info", MB_OK);
b)修改按钮文本(一般用于国际化)
HHOOK hHook;
LRESULT __stdcall CBTHookProc( long nCode,WPARAM wParam,LPARAM lParam)
{
if (nCode==HCBT_ACTIVATE)
{
SetDlgItemText((HWND)wParam,IDYES,"&Yes");
SetDlgItemText((HWND)wParam,IDNO ,"&No");
SetDlgItemText((HWND)wParam,IDOK,"&OK");
SetDlgItemText((HWND)wParam,IDCANCEL,"&Cancel");
UnhookWindowsHookEx(hHook);
}
return 0;
}
使用方法:
hHook=SetWindowsHookEx(WH_CBT,(HOOKPROC)CBTHookProc,AfxGetInstanceHandle(),NULL);
P.S. 过一段时间消失的MessageBox(源自MSDN)
void CALLBACK MessageBoxTimer(HWND hwnd, UINT uiMsg, UINT idEvent, DWORD dwTime)
{
PostQuitMessage(0);
}
UINT TimedMessageBox(HWND hwndParent, LPCTSTR ptszMessage, LPCTSTR ptszTitle, UINT flags, DWORD dwTimeout)
{
MSG msg;
UINT idTimer = SetTimer(NULL, 0, dwTimeout, (TIMERPROC)MessageBoxTimer);
UINT uiResult = MessageBox(hwndParent, ptszMessage, ptszTitle, flags);
KillTimer(NULL, idTimer);
if (PeekMessage(&msg, NULL, WM_QUIT, WM_QUIT, PM_REMOVE)) {
uiResult = 0;//默认值
}
return uiResult;
}
使用方法同MessageBox,只是多了个DWORD参数 dwTimeout