C++ 在windows、nix和macos上弹出错误消息框的最简单方法?
我正在编写一个非常简单的跨平台程序,它使用OpenGL进行显示,使用GLFW进行跨平台窗口创建 在某些情况下,我想在退出时向用户弹出一条有意义的错误消息-一条简单的“错误:无法初始化,因为n”弹出消息,在退出前带有一个“确定”点击框C++ 在windows、nix和macos上弹出错误消息框的最简单方法?,c++,user-interface,popup,cross-platform,C++,User Interface,Popup,Cross Platform,我正在编写一个非常简单的跨平台程序,它使用OpenGL进行显示,使用GLFW进行跨平台窗口创建 在某些情况下,我想在退出时向用户弹出一条有意义的错误消息-一条简单的“错误:无法初始化,因为n”弹出消息,在退出前带有一个“确定”点击框 我真的不希望添加一个功能齐全的gui系统(如wxWidgets)而只是弹出一条错误消息。如果使用本机平台API真的是最简单的方法,我不介意编写三个不同的平台相关子例程,但我想知道是否还没有一个非常轻量级/最小的跨平台库能够为我做到这一点?不依赖外部库或必须维护更多的
我真的不希望添加一个功能齐全的gui系统(如wxWidgets)而只是弹出一条错误消息。如果使用本机平台API真的是最简单的方法,我不介意编写三个不同的平台相关子例程,但我想知道是否还没有一个非常轻量级/最小的跨平台库能够为我做到这一点?不依赖外部库或必须维护更多的“跨平台”代码,您是否考虑过使用
OpenGL
?画一个内部有文本的红色四边形
您很可能在某个时候拥有某种用户界面,因此您可能可以重用该代码
编辑:根据OpenGL准备就绪之前可能出现的错误消息的规格,我必须说,在我看来,唯一的选择是为每个操作系统编写一个最小的包装,以尝试以本机方式显示消息框。可以作为一个有用的参考。因为以前似乎没有简单的跨平台消息框库,所以我决定创建一个 为了让调用者不必担心平台相关的细节,我创建了一个接口,并将其放在头文件中: 拳击手 接下来,我为我想要支持的每个操作系统(Windows、OS X和Linux(使用GTK+)创建了实现文件。构建时,将根据目标操作系统选择一个实现文件 boxer_linux.cpp
#包括
#包括
名称空间盒{
名称空间{
GtkMessageType getMessageType(样式){
开关(样式){
案例样式::信息:
返回GTK_消息_信息;
案例样式::警告:
返回GTK_消息_警告;
案例样式::错误:
返回GTK_消息_错误;
案例风格::问题:
返回GTK_消息_问题;
违约:
返回GTK_消息_信息;
}
}
GtkButtonsType getButtonsType(按钮){
开关(按钮){
案例按钮::确定:
返回GTK_按钮_OK;
案例按钮::确定取消:
返回GTK按钮\u确定\u取消;
案例按钮::是否:
返回GTK按钮是否;
违约:
返回GTK_按钮_OK;
}
}
选择getSelection(gint响应){
开关(响应){
案例GTK_响应_正常:
返回选择::确定;
案例GTK_响应_取消:
返回选择::取消;
案例GTK_响应_是:
返回选择::是;
案例GTK_响应_编号:
返回选择::否;
违约:
返回选择::无;
}
}
}//名称空间
选择显示(常量字符*消息、常量字符*标题、样式、按钮){
如果(!gtk_init_check(0,NULL)){
返回选择::无;
}
GtkWidget*dialog=gtk_消息_对话框_新建(空,
GTK_对话框_模态,
getMessageType(样式),
getButtonsType(按钮),
“%s”,
信息);
gtk_窗口_设置_标题(gtk_窗口(对话框),标题);
选择=获取选择(gtk_对话框运行(gtk_对话框(对话框));
gtk_小部件_销毁(gtk_小部件(对话框));
while(g_main_context_迭代(NULL,false));
返回选择;
}
}//名称空间盒
boxer_osx.mm
#包括
#进口
名称空间盒{
名称空间{
NSString*const OK_STR=@“OK”;
NSString*const CANCEL_STR=@“CANCEL”;
NSString*const YES_STR=@“YES”;
NSString*const NO_STR=@“NO”;
NSAlertStyle getAlertStyle(样式样式){
开关(样式){
案例样式::信息:
返回nsinformationalertstyle;
案例样式::警告:
返回NSWarningAlertStyle;
案例样式::错误:
返回nscriticalertstyle;
案例风格::问题:
返回NSWarningAlertStyle;
违约:
返回nsinformationalertstyle;
}
}
无效设置按钮(NSAlert*警报、按钮){
开关(按钮){
案例按钮::确定:
[警报添加按钮,标题:OK_STR];
打破
案例按钮::确定取消:
[警报添加按钮,标题:OK_STR];
[标题为“取消”的警报添加按钮];
打破
案例按钮::是否:
[alert AddButton,标题:YES_STR];
[警报添加按钮,标题:NO_STR];
打破
违约:
[警报添加按钮,标题:OK_STR];
}
}
选择getSelection(整数索引、按钮){
开关(按钮){
案例按钮::确定:
返回索引==NSAlertFirstButtonReturn?选择::确定:选择::无;
案例按钮::确定取消:
如果(索引==NSAlertFirstButtonReturn){
返回选择::确定;
}else if(索引==NSAlertSecondButtonReturn){
返回选择::取消;
}否则{
返回选择::无;
}
案例按钮::是否:
如果(索引==NSAlertFirstButtonReturn){
返回选择::是;
}else if(索引==NSAlertSecondButtonReturn){
返回选择::否;
}否则{
返回选择::无;
}
违约:
返回选择::无;
}
}
}//名称空间
选择显示(常量字符*消息、常量字符*标题、样式、按钮){
NSAlert*警报=[[NSAlert alloc]init];
[alert setMessageText:[NSString stringWithCString:标题]
编码:[NSString defaultCStringEncoding]];
[alert setInformativeText:[NSString stringWithCString:messa
#ifndef BOXER_H
#define BOXER_H
namespace boxer {
enum class Style {
Info,
Warning,
Error,
Question
};
enum class Buttons {
OK,
OKCancel,
YesNo
};
enum class Selection {
OK,
Cancel,
Yes,
No,
None
};
const Style DEFAULT_STYLE = Style::Info;
const Buttons DEFAULT_BUTTONS = Buttons::OK;
Selection show(const char *message, const char *title, Style style, Buttons buttons);
inline Selection show(const char *message, const char *title, Style style) {
return show(message, title, style, DEFAULT_BUTTONS);
}
inline Selection show(const char *message, const char *title, Buttons buttons) {
return show(message, title, DEFAULT_STYLE, buttons);
}
inline Selection show(const char *message, const char *title) {
return show(message, title, DEFAULT_STYLE, DEFAULT_BUTTONS);
}
} // namespace boxer
#endif
#include <boxer/boxer.h>
#include <gtk/gtk.h>
namespace boxer {
namespace {
GtkMessageType getMessageType(Style style) {
switch (style) {
case Style::Info:
return GTK_MESSAGE_INFO;
case Style::Warning:
return GTK_MESSAGE_WARNING;
case Style::Error:
return GTK_MESSAGE_ERROR;
case Style::Question:
return GTK_MESSAGE_QUESTION;
default:
return GTK_MESSAGE_INFO;
}
}
GtkButtonsType getButtonsType(Buttons buttons) {
switch (buttons) {
case Buttons::OK:
return GTK_BUTTONS_OK;
case Buttons::OKCancel:
return GTK_BUTTONS_OK_CANCEL;
case Buttons::YesNo:
return GTK_BUTTONS_YES_NO;
default:
return GTK_BUTTONS_OK;
}
}
Selection getSelection(gint response) {
switch (response) {
case GTK_RESPONSE_OK:
return Selection::OK;
case GTK_RESPONSE_CANCEL:
return Selection::Cancel;
case GTK_RESPONSE_YES:
return Selection::Yes;
case GTK_RESPONSE_NO:
return Selection::No;
default:
return Selection::None;
}
}
} // namespace
Selection show(const char *message, const char *title, Style style, Buttons buttons) {
if (!gtk_init_check(0, NULL)) {
return Selection::None;
}
GtkWidget *dialog = gtk_message_dialog_new(NULL,
GTK_DIALOG_MODAL,
getMessageType(style),
getButtonsType(buttons),
"%s",
message);
gtk_window_set_title(GTK_WINDOW(dialog), title);
Selection selection = getSelection(gtk_dialog_run(GTK_DIALOG(dialog)));
gtk_widget_destroy(GTK_WIDGET(dialog));
while (g_main_context_iteration(NULL, false));
return selection;
}
} // namespace boxer
#include <boxer/boxer.h>
#import <Cocoa/Cocoa.h>
namespace boxer {
namespace {
NSString* const OK_STR = @"OK";
NSString* const CANCEL_STR = @"Cancel";
NSString* const YES_STR = @"Yes";
NSString* const NO_STR = @"No";
NSAlertStyle getAlertStyle(Style style) {
switch (style) {
case Style::Info:
return NSInformationalAlertStyle;
case Style::Warning:
return NSWarningAlertStyle;
case Style::Error:
return NSCriticalAlertStyle;
case Style::Question:
return NSWarningAlertStyle;
default:
return NSInformationalAlertStyle;
}
}
void setButtons(NSAlert *alert, Buttons buttons) {
switch (buttons) {
case Buttons::OK:
[alert addButtonWithTitle:OK_STR];
break;
case Buttons::OKCancel:
[alert addButtonWithTitle:OK_STR];
[alert addButtonWithTitle:CANCEL_STR];
break;
case Buttons::YesNo:
[alert addButtonWithTitle:YES_STR];
[alert addButtonWithTitle:NO_STR];
break;
default:
[alert addButtonWithTitle:OK_STR];
}
}
Selection getSelection(int index, Buttons buttons) {
switch (buttons) {
case Buttons::OK:
return index == NSAlertFirstButtonReturn ? Selection::OK : Selection::None;
case Buttons::OKCancel:
if (index == NSAlertFirstButtonReturn) {
return Selection::OK;
} else if (index == NSAlertSecondButtonReturn) {
return Selection::Cancel;
} else {
return Selection::None;
}
case Buttons::YesNo:
if (index == NSAlertFirstButtonReturn) {
return Selection::Yes;
} else if (index == NSAlertSecondButtonReturn) {
return Selection::No;
} else {
return Selection::None;
}
default:
return Selection::None;
}
}
} // namespace
Selection show(const char *message, const char *title, Style style, Buttons buttons) {
NSAlert *alert = [[NSAlert alloc] init];
[alert setMessageText:[NSString stringWithCString:title
encoding:[NSString defaultCStringEncoding]]];
[alert setInformativeText:[NSString stringWithCString:message
encoding:[NSString defaultCStringEncoding]]];
[alert setAlertStyle:getAlertStyle(style)];
setButtons(alert, buttons);
Selection selection = getSelection([alert runModal], buttons);
[alert release];
return selection;
}
} // namespace boxer
#include <boxer/boxer.h>
#include <windows.h>
namespace boxer {
namespace {
UINT getIcon(Style style) {
switch (style) {
case Style::Info:
return MB_ICONINFORMATION;
case Style::Warning:
return MB_ICONWARNING;
case Style::Error:
return MB_ICONERROR;
case Style::Question:
return MB_ICONQUESTION;
default:
return MB_ICONINFORMATION;
}
}
UINT getButtons(Buttons buttons) {
switch (buttons) {
case Buttons::OK:
return MB_OK;
case Buttons::OKCancel:
return MB_OKCANCEL;
case Buttons::YesNo:
return MB_YESNO;
default:
return MB_OK;
}
}
Selection getSelection(int response) {
switch (response) {
case IDOK:
return Selection::OK;
case IDCANCEL:
return Selection::Cancel;
case IDYES:
return Selection::Yes;
case IDNO:
return Selection::No;
default:
return Selection::None;
}
}
} // namespace
Selection show(const char *message, const char *title, Style style, Buttons buttons) {
UINT flags = MB_TASKMODAL;
flags |= getIcon(style);
flags |= getButtons(buttons);
return getSelection(MessageBox(NULL, message, title, flags));
}
} // namespace boxer