(C++) 适用于 Linux 的 MessageBox 就像在 MS Windows 中一样

发布于 2024-08-04 03:54:28 字数 638 浏览 1 评论 0原文

我需要为 Linux (SDL) 应用程序实现一个简单的图形消息框,类似于 C++ (gcc/g++ 4.4.0) 中的 Windows MessageBox。它所需要做的就是显示标题、消息和确定或关闭按钮,并在单击该按钮时返回到调用函数。

SDL 只是使用 X(11) 打开一个窗口进行 (OpenGL) 渲染。

我查看了有关 GTK 实现的类似线程,但该实现似乎无法正常工作。

我也尝试过 wxWidgets 的 wxMessageBox 函数,但是编译头文件会使编译器抛出有关 include/c++/4.4.0/bits/stl_algobase.h 中语法错误的错误消息(openSuSE 11.1 32 位上的 gcc 4.4.0 32 位)。使用 wxWidgets 还意味着必须链接大量的库,将 STL 添加到我的应用程序中(否则不需要),谁知道还有什么,所以我不想使用 wxWidgets。

X11/motif (openmotif) 有我需要的东西 (XmCreate{Error|Warning|InfoDialog),但是这些需要一个父窗口小部件(例如顶级窗口),而我没有,并且不接受它们的 NULL 参数。

所以我现在很困惑。有没有一种简单的方法可以做我想做的事?或者至少是一半简单/容易/直接的?如果是,是哪一个(提供尽可能多的详细信息将不胜感激)。

I need to implement a simple graphical message box for a Linux (SDL) application similar to the Windows MessageBox in C++ (gcc/g++ 4.4.0). All it needs to do is to display a caption, a message and an ok or close button and to return to the calling function when that button is clicked.

SDL just uses X(11) to open a window for (OpenGL) rendering.

I have looked through a similar thread regarding a GTK implementation, but that implementation doesn't seem to work properly.

I have also tried wxWidgets' wxMessageBox function, but compiling the headers makes the compiler throw error messages about syntax errors in include/c++/4.4.0/bits/stl_algobase.h (gcc 4.4.0 32 bits on openSuSE 11.1 32 bits). Using wxWidgets also means having to link a ton of libraries, adding STL to my app (Which it doesn't need otherwise) and who knows what else, so I do not want to use wxWidgets.

X11/motif (openmotif) has what I need (XmCreate{Error|Warning|InfoDialog), but these need a parent widget (e.g. top level window) which I don't have and do not accept a NULL parameter for these.

So I am stumped right now. Is there a simple way to do what I want? Or at least a halfway simple/easy/straightforward one? If yes, which one (giving as many details as possible would be highly appreciated).

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

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

发布评论

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

评论(5

許願樹丅啲祈禱 2024-08-11 03:54:28

在 SDL2 中,您现在可以显示消息框:

http://wiki.libsdl.org/SDL_ShowSimpleMessageBox

int SDL_ShowSimpleMessageBox(Uint32      flags,
                             const char* title,
                             const char* message,
                             SDL_Window* window)

http://wiki.libsdl.org/SDL_ShowMessageBox

int SDL_ShowMessageBox(const SDL_MessageBoxData* messageboxdata,
                       int*                      buttonid)

In SDL2, you can now show message boxes:

http://wiki.libsdl.org/SDL_ShowSimpleMessageBox

int SDL_ShowSimpleMessageBox(Uint32      flags,
                             const char* title,
                             const char* message,
                             SDL_Window* window)

http://wiki.libsdl.org/SDL_ShowMessageBox

int SDL_ShowMessageBox(const SDL_MessageBoxData* messageboxdata,
                       int*                      buttonid)
梦晓ヶ微光ヅ倾城 2024-08-11 03:54:28

我个人使用Qt4的QMessageBox

例子:

QMessageBox mb(QMessageBox::Question, "Title", "Message",  QMessageBox::Ok | QMessageBox::Cancel);
if(mb.exec() == QMessageBox::Ok) { do_stuff(); }

I personally use Qt4's QMessageBox.

example:

QMessageBox mb(QMessageBox::Question, "Title", "Message",  QMessageBox::Ok | QMessageBox::Cancel);
if(mb.exec() == QMessageBox::Ok) { do_stuff(); }
北斗星光 2024-08-11 03:54:28

看起来您必须创建一个顶级 X11/Motif 窗口。这里有一些代码可以帮助您入门:

#include <Xm/Xm.h> 
#include <Xm/PushB.h>

/* Prototype Callback function */

void pushed_fn(Widget , XtPointer , 
               XmPushButtonCallbackStruct *);


main(int argc, char **argv) 

{   Widget top_wid, button;
    XtAppContext  app;

    top_wid = XtVaAppInitialize(&app, "Push", NULL, 0,
        &argc, argv, NULL, NULL);

    button = XmCreatePushButton(top_wid, "Push_me", NULL, 0);

    /* tell Xt to manage button */
                XtManageChild(button);

                /* attach fn to widget */
    XtAddCallback(button, XmNactivateCallback, pushed_fn, NULL);

    XtRealizeWidget(top_wid); /* display widget hierarchy */
    XtAppMainLoop(app); /* enter processing loop */ 

}

void pushed_fn(Widget w, XtPointer client_data, 
               XmPushButtonCallbackStruct *cbs) 
  {   
     printf("Don't Push Me!!\n");
  }

这是从此处复制的< /a> 这可能会给你更多关于这方面的指示。

Looks like you will have to create a top-level X11/Motif window. Here's some code to get you started:

#include <Xm/Xm.h> 
#include <Xm/PushB.h>

/* Prototype Callback function */

void pushed_fn(Widget , XtPointer , 
               XmPushButtonCallbackStruct *);


main(int argc, char **argv) 

{   Widget top_wid, button;
    XtAppContext  app;

    top_wid = XtVaAppInitialize(&app, "Push", NULL, 0,
        &argc, argv, NULL, NULL);

    button = XmCreatePushButton(top_wid, "Push_me", NULL, 0);

    /* tell Xt to manage button */
                XtManageChild(button);

                /* attach fn to widget */
    XtAddCallback(button, XmNactivateCallback, pushed_fn, NULL);

    XtRealizeWidget(top_wid); /* display widget hierarchy */
    XtAppMainLoop(app); /* enter processing loop */ 

}

void pushed_fn(Widget w, XtPointer client_data, 
               XmPushButtonCallbackStruct *cbs) 
  {   
     printf("Don't Push Me!!\n");
  }

This was copied from here which might give you some more pointers on this.

悟红尘 2024-08-11 03:54:28

这是我的解决方案。我选择使用 Motif (OpenMotif),因为它需要相对较少的额外库(Xm、Xt、X11)。根据消息大小,我的实现会打开一个简单的消息框或更复杂的对话框,其中包含不可编辑的可滚动文本(后者取自 Motif 程序员手册并适合我的目的)。

包括文件和全局数据:

#include <Xm/Xm.h>
#include <Xm/MwmUtil.h>
#include <Xm/MainW.h>
#include <Xm/CascadeB.h>
#include <Xm/MessageB.h>
#include <Xm/RowColumn.h>
#include <Xm/Form.h>
#include <Xm/PushBG.h>
#include <Xm/LabelG.h>
#include <Xm/PanedW.h>
#include <Xm/Text.h>
#include <Xm/DialogS.h>
#include <Xm/Command.h>

static XtAppContext appShell;

用于确定行数和最大值的辅助函数。文本消息的列数:

static int MsgSize (char* pszMsg, int& nCols)
{
if (!(pszMsg && *pszMsg))
   return 0;
int nRows = 1;
nCols = 0;
for (char* p = pszMsg; *p && (pszMsg = strchr (p, '\n')); nRows++, p = ++pszMsg) {
   if (nCols < pszMsg - p)
      nCols = pszMsg - p;
   }
return nRows;
}

消息对话框的关闭按钮的回调函数:

void DestroyShell (Widget widget, XtPointer clientData, XtPointer callData)
{
Widget shell = (Widget) clientData;
XtDestroyWidget (shell);
// tell the application event loop to terminate w/o terminating the application
XtAppSetExitFlag (appShell);
}

构建一个包含可滚动、不可编辑的文本小部件和关闭按钮的对话框。取自 Motif 程序员手册并稍作修改(无图标,单个按钮),最小化窗口装饰)。

void XmMessageDialog (const char* pszMsg, int nRows, int nCols, bool bError)
{
    Widget       msgBox, pane, msgText, form, widget;
    void         DestroyShell(Widget, XtPointer, XtPointer);
    Arg          args [10];
    int          n = 0;
    int          i;
    Dimension    h;

// Set up a DialogShell as a popup window. Set the delete window protocol response to XmDESTROY to make sure that
// the window goes away appropriately. Otherwise, it's XmUNMAP which means it'd be lost forever, since we're not storing
// the widget globally or statically to this function.
Widget topWid = XtVaAppInitialize (&appShell, "D2X-XL", NULL, 0, &argc, argv, NULL, NULL);
XtSetArg (args [0], XmNdeleteResponse, XmDESTROY);
msgBox = XmCreateDialogShell (topWid, bError ? const_cast<char*>("Error") : const_cast<char*>("Warning"), args, 1);
XtVaGetValues (msgBox, XmNmwmDecorations, &i, NULL);
i &= ~(MWM_DECOR_ALL | MWM_DECOR_MINIMIZE | MWM_DECOR_MAXIMIZE | MWM_DECOR_MENU);
XtVaSetValues (msgBox, XmNmwmDecorations, i, NULL);
XtVaGetValues (msgBox, XmNmwmFunctions, &i, NULL);
i &= ~(MWM_FUNC_ALL | MWM_FUNC_MINIMIZE | MWM_FUNC_MAXIMIZE | MWM_FUNC_CLOSE);
XtVaSetValues (msgBox, XmNmwmFunctions, i, NULL);
// Create a PanedWindow to manage the stuff in this dialog. PanedWindow won't let us set these to 0!
XtSetArg (args [0], XmNsashWidth, 1);
// Make small so user doesn't try to resize
XtSetArg (args [1], XmNsashHeight, 1);
pane = XmCreatePanedWindow (msgBox, const_cast<char*>("pane"), args, 2);
// Create a RowColumn in the form for Label and Text widgets. This is the control area.
form = XmCreateForm (pane, const_cast<char*>("form1"), NULL, 0);
// prepare the text for display in the ScrolledText object we are about to create.
n = 0;
XtSetArg (args [n], XmNscrollVertical, True); n++;
XtSetArg (args [n], XmNscrollHorizontal, False); n++;
XtSetArg (args [n], XmNeditMode, XmMULTI_LINE_EDIT); n++;
XtSetArg (args [n], XmNeditable, False); n++;
XtSetArg (args [n], XmNcursorPositionVisible, False); n++;
XtSetArg (args [n], XmNwordWrap, True); n++;
XtSetArg (args [n], XmNvalue, pszMsg); n++;
XtSetArg (args [n], XmNrows, min (nRows, 30)); n++;
XtSetArg (args [n], XmNcolumns, min (nCols, 120)); n++;
msgText = XmCreateScrolledText (form, const_cast<char*>("help_text"), args, n);
// Attachment values must be set on the Text widget's PARENT, the ScrolledWindow. This  is the object that is positioned.
XtVaSetValues (XtParent (msgText),
               XmNleftAttachment, XmATTACH_FORM,
               XmNtopAttachment, XmATTACH_FORM,
               XmNrightAttachment, XmATTACH_FORM,
               XmNbottomAttachment, XmATTACH_FORM,
               NULL);
XtManageChild (msgText);
XtManageChild (form);
// Create another form to act as the action area for the dialog
XtSetArg (args [0], XmNfractionBase, 5);
form = XmCreateForm (pane, const_cast<char*>("form2"), args, 1);
// The OK button is under the pane's separator and is attached to the left edge of the form. It spreads from
// position 0 to 1 along the bottom (the form is split into 5 separate grids via XmNfractionBase upon creation).
widget = XmCreatePushButtonGadget (form, const_cast<char*>("Close"), NULL, 0);
XtVaSetValues (widget,
               XmNtopAttachment, XmATTACH_FORM,
               XmNbottomAttachment, XmATTACH_FORM,
               XmNleftAttachment, XmATTACH_POSITION,
               XmNleftPosition, 2,
               XmNrightAttachment, XmATTACH_POSITION,
               XmNrightPosition, 3,
               XmNshowAsDefault, True,
               XmNdefaultButtonShadowThickness, 1,
               NULL);
XtManageChild (widget);
XtAddCallback (widget, XmNactivateCallback, DestroyShell, (XtPointer) msgBox);
// Fix the action area pane to its current height -- never let it resize
XtManageChild (form);
XtVaGetValues (widget, XmNheight, &h, NULL);
XtVaSetValues (form, XmNpaneMaximum, h, XmNpaneMinimum, h, NULL);
// This also pops up the dialog, as it is the child of a DialogShell
XtManageChild (pane);
}

消息框“确定”按钮的回调函数

void XmCloseMsgBox (Widget w, XtPointer clientData, XtPointer callData)
{
XtAppSetExitFlag (appShell);
}

决定是使用简单消息框还是高级消息框,显示其中一个,并在用户单击关闭/确定按钮时删除它们。

void XmMessageBox (const char* pszMsg, bool bError)
{
   Widget   topWid;
   int      nRows, nCols;

nRows = MsgSize (const_cast<char*>(pszMsg), nCols);
if ((nRows > 3) || (nCols > 360))
   XmMessageDialog (pszMsg, nRows, nCols, bError);
else { // use the built-in message box
   topWid = XtVaAppInitialize (&appShell, "D2X-XL", NULL, 0, &argC, argv, NULL, NULL);
   // setup message box text
   Arg args [1];
   XmString xmString = XmStringCreateLocalized (const_cast<char*>(pszMsg));
   XtSetArg (args [0], XmNmessageString, xmString);
   // create and label message box
   Widget xMsgBox = bError
                    ? XmCreateErrorDialog (topWid, const_cast<char*>("Error"), args, 1)
                    : XmCreateWarningDialog (topWid, const_cast<char*>("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));
   // add callback to the "close" button that signals closing of the message box
   XtAddCallback (xMsgBox, XmNokCallback, XmCloseMsgBox, NULL);
   XtManageChild (xMsgBox);
   XtRealizeWidget (topWid);
   }
XtAppMainLoop (appShell);
XtUnrealizeWidget (topWid);
XtDestroyApplicationContext (appShell);
}

Here is my solution. I chose to use Motif (OpenMotif) as it requires comparably few extra libraries (Xm, Xt, X11). Depending on the message size, my implementation opens a simple message box or a more sophisticated dialog with a non editable, scrollable text (the latter taken from the Motif programmer's manual and adapted for my purposes).

Include files and global data:

#include <Xm/Xm.h>
#include <Xm/MwmUtil.h>
#include <Xm/MainW.h>
#include <Xm/CascadeB.h>
#include <Xm/MessageB.h>
#include <Xm/RowColumn.h>
#include <Xm/Form.h>
#include <Xm/PushBG.h>
#include <Xm/LabelG.h>
#include <Xm/PanedW.h>
#include <Xm/Text.h>
#include <Xm/DialogS.h>
#include <Xm/Command.h>

static XtAppContext appShell;

Helper function to determine rows and max. cols of a text message:

static int MsgSize (char* pszMsg, int& nCols)
{
if (!(pszMsg && *pszMsg))
   return 0;
int nRows = 1;
nCols = 0;
for (char* p = pszMsg; *p && (pszMsg = strchr (p, '\n')); nRows++, p = ++pszMsg) {
   if (nCols < pszMsg - p)
      nCols = pszMsg - p;
   }
return nRows;
}

Callback function for the message dialog's close button:

void DestroyShell (Widget widget, XtPointer clientData, XtPointer callData)
{
Widget shell = (Widget) clientData;
XtDestroyWidget (shell);
// tell the application event loop to terminate w/o terminating the application
XtAppSetExitFlag (appShell);
}

Build a dialog containing a scrollable, non editable text widget and a close button. Taken from the Motif programmer's manual and slightly adapted (no icon, single button), minimal window decoration).

void XmMessageDialog (const char* pszMsg, int nRows, int nCols, bool bError)
{
    Widget       msgBox, pane, msgText, form, widget;
    void         DestroyShell(Widget, XtPointer, XtPointer);
    Arg          args [10];
    int          n = 0;
    int          i;
    Dimension    h;

// Set up a DialogShell as a popup window. Set the delete window protocol response to XmDESTROY to make sure that
// the window goes away appropriately. Otherwise, it's XmUNMAP which means it'd be lost forever, since we're not storing
// the widget globally or statically to this function.
Widget topWid = XtVaAppInitialize (&appShell, "D2X-XL", NULL, 0, &argc, argv, NULL, NULL);
XtSetArg (args [0], XmNdeleteResponse, XmDESTROY);
msgBox = XmCreateDialogShell (topWid, bError ? const_cast<char*>("Error") : const_cast<char*>("Warning"), args, 1);
XtVaGetValues (msgBox, XmNmwmDecorations, &i, NULL);
i &= ~(MWM_DECOR_ALL | MWM_DECOR_MINIMIZE | MWM_DECOR_MAXIMIZE | MWM_DECOR_MENU);
XtVaSetValues (msgBox, XmNmwmDecorations, i, NULL);
XtVaGetValues (msgBox, XmNmwmFunctions, &i, NULL);
i &= ~(MWM_FUNC_ALL | MWM_FUNC_MINIMIZE | MWM_FUNC_MAXIMIZE | MWM_FUNC_CLOSE);
XtVaSetValues (msgBox, XmNmwmFunctions, i, NULL);
// Create a PanedWindow to manage the stuff in this dialog. PanedWindow won't let us set these to 0!
XtSetArg (args [0], XmNsashWidth, 1);
// Make small so user doesn't try to resize
XtSetArg (args [1], XmNsashHeight, 1);
pane = XmCreatePanedWindow (msgBox, const_cast<char*>("pane"), args, 2);
// Create a RowColumn in the form for Label and Text widgets. This is the control area.
form = XmCreateForm (pane, const_cast<char*>("form1"), NULL, 0);
// prepare the text for display in the ScrolledText object we are about to create.
n = 0;
XtSetArg (args [n], XmNscrollVertical, True); n++;
XtSetArg (args [n], XmNscrollHorizontal, False); n++;
XtSetArg (args [n], XmNeditMode, XmMULTI_LINE_EDIT); n++;
XtSetArg (args [n], XmNeditable, False); n++;
XtSetArg (args [n], XmNcursorPositionVisible, False); n++;
XtSetArg (args [n], XmNwordWrap, True); n++;
XtSetArg (args [n], XmNvalue, pszMsg); n++;
XtSetArg (args [n], XmNrows, min (nRows, 30)); n++;
XtSetArg (args [n], XmNcolumns, min (nCols, 120)); n++;
msgText = XmCreateScrolledText (form, const_cast<char*>("help_text"), args, n);
// Attachment values must be set on the Text widget's PARENT, the ScrolledWindow. This  is the object that is positioned.
XtVaSetValues (XtParent (msgText),
               XmNleftAttachment, XmATTACH_FORM,
               XmNtopAttachment, XmATTACH_FORM,
               XmNrightAttachment, XmATTACH_FORM,
               XmNbottomAttachment, XmATTACH_FORM,
               NULL);
XtManageChild (msgText);
XtManageChild (form);
// Create another form to act as the action area for the dialog
XtSetArg (args [0], XmNfractionBase, 5);
form = XmCreateForm (pane, const_cast<char*>("form2"), args, 1);
// The OK button is under the pane's separator and is attached to the left edge of the form. It spreads from
// position 0 to 1 along the bottom (the form is split into 5 separate grids via XmNfractionBase upon creation).
widget = XmCreatePushButtonGadget (form, const_cast<char*>("Close"), NULL, 0);
XtVaSetValues (widget,
               XmNtopAttachment, XmATTACH_FORM,
               XmNbottomAttachment, XmATTACH_FORM,
               XmNleftAttachment, XmATTACH_POSITION,
               XmNleftPosition, 2,
               XmNrightAttachment, XmATTACH_POSITION,
               XmNrightPosition, 3,
               XmNshowAsDefault, True,
               XmNdefaultButtonShadowThickness, 1,
               NULL);
XtManageChild (widget);
XtAddCallback (widget, XmNactivateCallback, DestroyShell, (XtPointer) msgBox);
// Fix the action area pane to its current height -- never let it resize
XtManageChild (form);
XtVaGetValues (widget, XmNheight, &h, NULL);
XtVaSetValues (form, XmNpaneMaximum, h, XmNpaneMinimum, h, NULL);
// This also pops up the dialog, as it is the child of a DialogShell
XtManageChild (pane);
}

Callback function for the message box' Ok button

void XmCloseMsgBox (Widget w, XtPointer clientData, XtPointer callData)
{
XtAppSetExitFlag (appShell);
}

Decide whether to use the simple or advanced message box, display either of them, and remove them when the user clicks their close/ok button.

void XmMessageBox (const char* pszMsg, bool bError)
{
   Widget   topWid;
   int      nRows, nCols;

nRows = MsgSize (const_cast<char*>(pszMsg), nCols);
if ((nRows > 3) || (nCols > 360))
   XmMessageDialog (pszMsg, nRows, nCols, bError);
else { // use the built-in message box
   topWid = XtVaAppInitialize (&appShell, "D2X-XL", NULL, 0, &argC, argv, NULL, NULL);
   // setup message box text
   Arg args [1];
   XmString xmString = XmStringCreateLocalized (const_cast<char*>(pszMsg));
   XtSetArg (args [0], XmNmessageString, xmString);
   // create and label message box
   Widget xMsgBox = bError
                    ? XmCreateErrorDialog (topWid, const_cast<char*>("Error"), args, 1)
                    : XmCreateWarningDialog (topWid, const_cast<char*>("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));
   // add callback to the "close" button that signals closing of the message box
   XtAddCallback (xMsgBox, XmNokCallback, XmCloseMsgBox, NULL);
   XtManageChild (xMsgBox);
   XtRealizeWidget (topWid);
   }
XtAppMainLoop (appShell);
XtUnrealizeWidget (topWid);
XtDestroyApplicationContext (appShell);
}
甜`诱少女 2024-08-11 03:54:28

我建议您研究一下支持 SDL 作为后端的 GUI 库之一。 GG 就是这样一个库,它具有 ThreeButtonDlg 类。当它的 Run() 返回时,您可以查看它的 Result()。请参阅 minimal 示例中的初始方法。

I would suggest that you look into one of the GUI libraries that support SDL as a backend. One such library would be GG, which has the class ThreeButtonDlg. When its Run() returns, you can look at its Result(). See the Initial method in their minimal example.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文