c++ - 适用于 Linux 的类似 Windows 的消息框 - 这个 C++/Motif 实现是否可行?

标签 c++ linux messagebox motif

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

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

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

代码如下:

<p></p>

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); 
}

最佳答案

我认为下面的代码可能是我要求的:

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

关于c++ - 适用于 Linux 的类似 Windows 的消息框 - 这个 C++/Motif 实现是否可行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1386770/

相关文章:

c++ - 如何在不读/写 key 文件的情况下使用 crypto++?

c++ - 尝试获取具有特定模板类参数的 std::reference_wrapper 时出现问题

html - 如何下载 url 的 html 中的链接?

linux - 如何在没有系统调用的情况下在堆上分配内存?

c# - System.Windows.MessageBox 与 System.Windows.Forms.MessageBox

c# - 在 C# 中显示一个类的消息框?

c++ - 程序似乎跳过了 'if' 语句

C++ HTTP GET 使用套接字

c++ - MessageBox 和 ShellExecute 成功,但如果在应用程序退出之前立即调用则不会显示任何内容

c++ - 杂志订阅程序的用户定义函数