【问题标题】:Windows-like message box for Linux - would this C++/Motif implementation work?适用于 Linux 的类似 Windows 的消息框 - 这个 C++/Motif 实现会起作用吗?
【发布时间】:2009-09-06 21:28:33
【问题描述】:

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

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

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

代码如下:

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

【问题讨论】:

  • 碰巧的是,您的最后一个问题stackoverflow.com/questions/1386319/… 得到了相当快的回答,但您还没有投票、接受或发表评论请求更多帮助。事实上,你似乎从未接受过任何问题的答案。你觉得你没有得到有用的答案吗?
  • 实际上,上面代码的主要问题是当我设置 appContext.exit_flag = 1 时,topWidget 和 appContext 会发生什么。我是否必须以某种方式(如何)将它们返回给 Motif,或者它们会会自动释放吗?这很难尝试 - 不像测试上面的代码是否显示消息框
  • 我认为我无法以
  • 回答你的问题:到目前为止,唯一有用的答案(所以我希望)是你的。我正在寻找一个轻量级的消息框,并且希望能够避免将六个或更多额外的库链接到我的应用程序,只是为了像消息框这样看似简单的东西。在 Linux 上以可接受的开销实现类似的东西是一件令人头疼的事情。我希望使用 Motif 并不意味着我必须通过额外的库...
  • 我同意,使用更大的 GUI 库来解决一个简单的问题也会困扰我。我相信 Motif 会完成这项工作,在当时它是一流的。并感谢您的反馈!我会尽我所能给你一些更好的答案。

标签: c++ linux messagebox motif


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-06-23
    • 2023-03-30
    • 1970-01-01
    • 1970-01-01
    • 2012-04-06
    • 1970-01-01
    • 2014-09-18
    相关资源
    最近更新 更多