适用于 Linux 的类似 Windows 的消息框 - 这个 C++/Motif 实现可以工作吗?

发布于 2024-08-03 22:56:51 字数 1492 浏览 9 评论 0原文

我想要一个类似于 Windows 的 Linux 消息框:它应该弹出,显示一些文本,当用户单击“确定”按钮时,它应该消失并将控制权返回给调用函数。

即使根本没有应用程序窗口,消息框也应该可以工作。因此,它创建一个应用程序上下文,通过 XmCreate*Dialog 将一个对话框绑定到它,当用户单击“对话框确定”按钮时,告诉应用程序上下文主循环退出。

这会像预期的那样工作吗? 这是否会自动销毁在此过程中创建的所有小部件和应用程序上下文(如果不是,那必须如何完成?)?

这是代码:

static XtAppContext appContext; static Widget topWidget; void XmCloseMsgBox (Widget w, XtPointer clientData, XmPushButtonCallbackStruct *cbs) { appContext.exit_flag = 1; } void XmMessageBox (const char* pszMsg, bool bError) { Widget msgBox; XmString xmString = XmStringCreateLocalized (const_cast<char*>(pszMsg)); Arg args [1]; topWidget = XtVaAppInitialize (&appContext, "Application Name", NULL, 0, &gameData.app.argC, gameData.app.argV, NULL, NULL); // setup message box text XtSetArg (args [0], XmNmessageString, xmString); // create and label message box xMsgBox = bError ? XmCreateErrorDialog (topWidget, "Error", args, 1) : XmCreateWarningDialog (topWidget, "Warning", args, 1); // remove text resource XmStringFree (xmString); // remove help and cancel buttons XtUnmanageChild (XmMessageBoxGetChild (xMsgBox, XmDIALOG_CANCEL_BUTTON)); XtUnmanageChild (XmMessageBoxGetChild (xMsgBox, XmDIALOG_HELP_BUTTON)); XtAddCallback (xMsgBox, XmNokCallback, XmCloseMsgBox, NULL); XtRealizeWidget (topWidget); // display message box //XtManageChild (xMsgBox); XtAppMainLoop (app); }

I want a message box for Linux similar to the Windows one: It should pop up, display some text, and when the user clicks the Ok button, it should disappear and return control to the calling function.

The message box should work even if there is no application window yet at all. So it creates an application context, ties a dialog via XmCreate*Dialog to it, and when the user clicks the dialogs Ok button, tells the app contexts main loop to exit.

Would this work like intended?
Would this automatically destroy all widgets and the app context that got created in the process (if no, how would that have to be done?)?

Here's the code:

static XtAppContext appContext; static Widget topWidget; void XmCloseMsgBox (Widget w, XtPointer clientData, XmPushButtonCallbackStruct *cbs) { appContext.exit_flag = 1; } void XmMessageBox (const char* pszMsg, bool bError) { Widget msgBox; XmString xmString = XmStringCreateLocalized (const_cast<char*>(pszMsg)); Arg args [1]; topWidget = XtVaAppInitialize (&appContext, "Application Name", NULL, 0, &gameData.app.argC, gameData.app.argV, NULL, NULL); // setup message box text XtSetArg (args [0], XmNmessageString, xmString); // create and label message box xMsgBox = bError ? XmCreateErrorDialog (topWidget, "Error", args, 1) : XmCreateWarningDialog (topWidget, "Warning", args, 1); // remove text resource XmStringFree (xmString); // remove help and cancel buttons XtUnmanageChild (XmMessageBoxGetChild (xMsgBox, XmDIALOG_CANCEL_BUTTON)); XtUnmanageChild (XmMessageBoxGetChild (xMsgBox, XmDIALOG_HELP_BUTTON)); XtAddCallback (xMsgBox, XmNokCallback, XmCloseMsgBox, NULL); XtRealizeWidget (topWidget); // display message box //XtManageChild (xMsgBox); XtAppMainLoop (app); }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

黑白记忆 2024-08-10 22:56:51

我认为以下代码可能就是我所要求的:

XtRealizeWidget (topWid);
// display message box
appContext.exit_flag = 0;
XtAppMainLoop (app); 
while (!appContext.exit_flag) // wait in case XtAppMainLoop has its own thread
    ;
XtUnrealizeWidget (topWid); // destroy topWid and all its children

I think the following code could be what I asked for:

XtRealizeWidget (topWid);
// display message box
appContext.exit_flag = 0;
XtAppMainLoop (app); 
while (!appContext.exit_flag) // wait in case XtAppMainLoop has its own thread
    ;
XtUnrealizeWidget (topWid); // destroy topWid and all its children
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文