正如我通过搜索发现的,无法创建 MessageBox()带有居中对齐的文本。
那么是否有一些简单的替代方法提供 MessageBox() 功能(包括程序等待关闭/接受框),可以选择居中对齐文本?
感谢您的建议/示例。
PS:在 Windows7+ 上,使用 C++ Windows API(在 MS Visual Studio 2012 中编译)
编辑:
一些有用的提示:
1) Express 版本的 Visual Studio 没有资源编辑器/文件创建选项:
- 您确实有一个比express“更高”的Visual Studio - How to: Add a Resource File
- 你有 just Express 版本:
- How to add a resource editor for Express version
- 自由编辑 ResEdit
- 这MSDN forum page包含一些其他有用的提示,例如:
From the 'solution explorer', right-click the *.rc file and select 'open with..', then select 'source code (text) editor' from the list. You might want to 'set as default' to save you repeating those intitial steps. Once done, you should be able to manually edit the resource script within Express.
- Free XN Resource Editor
2) Visual Studio C++ how to display a dynamic message (i.e., string) in my About box?
最佳答案
As I found out by searching, it is not possible to create a MessageBox() with center-aligned text.
无法创建居中对齐的MessageBox()
对话框,因为API 没有为此提供选项。但是有可能通过一些小技巧来操纵标准MessageBox()`对话框以强制它居中对齐。
使用SetWindowsHookEx()
创建一个 WH_CBT
调用 MessageBox()
的线程的钩子(Hook)(不需要 DLL)。 hook callback允许您发现 MessageBox()
创建的对话框的 HWND。有了它,您可以随心所欲地操纵它。在这种情况下,您可以使用 FindWindowEx()
获取 STATIC
的 HWND控制对话框的文本,然后使用 SetWindowLong()
将 SS_CENTER
样式应用于它.例如:
LRESULT CALLBACK CenterMsgBoxTextProc(int nCode, WPARAM wParam, LPARAM lParam)
{
if (nCode == HCBT_ACTIVATE)
{
HWND hDlg = (HWND) wParam;
HWND hTxt = FindWindowEx(hDlg, NULL, TEXT("STATIC"), NULL);
if (hTxt)
SetWindowLong(hTxt, GWL_STYLE, GetWindowLong(hTxt, GWL_STYLE) | SS_CENTER);
}
return CallNextHookEx(NULL, nCode, wParam, lParam);
}
HHOOK hHook = SetWindowsHookEx(WH_CBT, (HOOKPROC) &CenterMsgBoxTextProc, NULL, GetCurrentThreadId());
MessageBox(...);
if (hHook) UnhookWindowsHookEx(hHook);
或者,您可以使用 SetWinEventHook()
而不是 SetWindowsHookEx()
:
void CALLBACK CenterMsgBoxTextProc(HWINEVENTHOOK hWinEventHook, DWORD event, HWND hwnd, LONG idObject, LONG idChild, DWORD dwEventThread, DWORD dwmsEventTime)
{
HWND hTxt = FindWindowEx(hwnd, NULL, TEXT("STATIC"), NULL);
if (hTxt)
SetWindowLong(hTxt, GWL_STYLE, GetWindowLong(hTxt, GWL_STYLE) | SS_CENTER);
}
HWINEVENTHOOK hHook = SetWinEventHook(EVENT_OBJECT_CREATE, EVENT_OBJECT_CREATE, NULL, &CenterMsgBoxTextProc, GetCurrentProcessId(), GetCurrentThreadId(), 0);
MessageBox(NULL, TEXT("test"), TEXT("test"), MB_OK);
if (hHook) UnhookWinEvent(hHook);
两种情况下的结果如下:
关于c++ - Win api MessageBox 替代方案,它有一组文本对齐 : center,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28807068/