用输入框提示用户? [C++]
我的目标是简单地使用弹出框来要求用户输入。我已经搜索了相当多的内容,几乎所有结果都表明创建消息框非常简单:
MessageBox (NULL, "Hello World" , "Hello", MB_OKCANCEL);
但是创建一个接受输入的弹出窗口更加复杂,并且没有直接的方法来做到这一点。我在 Google 上找到的所有结果的日期都是从 2001 年到 2005 年的某个时间。我想我来这里是想问近年来是否出现了一些更直接的解决方案。
希望有一些像 Java 一样好的、直接的东西:
int number = JOptionPane.showInputDialog ("Enter an integer");
如果不是这样,我能得到一个关于如何做到这一点的简短解释吗?
编辑:我无法让任何东西发挥作用。 :( 我最终用 Java 编写了完成这项工作的代码,然后编写了一行 C++ 代码来调用 .jar 文件。 :-/ 由于这个问题对时间敏感,所以有总比没有好。
My goal is to simply use a pop-up box to ask the user for an input. I've searched around quite a bit and pretty much all the results say that creating a messageBox is really easy:
MessageBox (NULL, "Hello World" , "Hello", MB_OKCANCEL);
But that creating a pop-up that takes input is more involved and there isn't a straight forward way to do it. All of the results I could find on Google were dated somewhere from 2001 to 2005. I guess I'm here asking if some more straight forward solution has come about in recent years.
Hopefully something nice and straight forward like in Java:
int number = JOptionPane.showInputDialog ("Enter an integer");
If that isn't the case, could I get a brief explanation of how to do it?
Edit: I couldn't get anything to work. :( I ended up writing the code to do the work in Java, and then wrote one line of C++ code to call the .jar file. :-/ Since the issue was time sensitive, it was better than nothing.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
纯 C++ 没有这样的东西。基本上,您想要做的事情只能通过使用对操作系统的 API 调用或使用一些 GUI 库(如 Qt
) 来实现(我推荐它,因为它比调用本机 API 更容易,而且它也是多平台的)可以像在 java 上一样显示输入对话框:
您可以在此处下载 Qt 库: qt.nokia.com/products/developer-tools/
There is nothing like that for pure C++. Basically what you're trying to do can only be achieved by using an API call to the OS or by using some GUI library like Qt (which I recommend cause it's waaaaay easier then calling native APIs and it's also multi-platform)
Using Qt you can show an input dialog pretty much the same way you do it on java:
You can download the Qt library here: qt.nokia.com/products/developer-tools/
如果您使用的是 Visual C++ Express,则有许多免费资源编辑器可用于创建对话框。 ResEdit 是我发现的最好的之一。
您需要在添加到项目中的 .RC 文件中创建对话框资源。
然后,这是一个非常简单的调用 DialogBox - 将从资源文件加载对话框并将其放置在屏幕上。传入的 DialogProc 将被调用并带有许多通知。通常,您希望对所有内容都返回 FALSE,但将 WM_INITDIALOG 处理为用文本初始化编辑控件的位置,并且在单击按钮时将发送 WM_COMMAND。
If you are using Visual C++ Express there are a number of free resource editors that can be used to create dialogs. ResEdit is one of the better ones I've found.
You need to create a dialog resource in a .RC file that you add to your project.
Then, It is a very simple case of calling DialogBox - which will load the dialog box from your resource file and place it on the screen. The passed in DialogProc will be called with a number of notifications. Typically you would want to return FALSE for everything, but handle WM_INITDIALOG as a place to initialize the edit control with text, and WM_COMMAND will be sent when a button is clicked.
Microsoft 认为您的用例不够常见,无法进行优化,例如
MessageBox
。他们希望您布置一个包含许多控件的对话框,也许与控件进行一些复杂的交互,并且只有在对话框完全填满后才做出响应。您所要求的只是其简化版本。资源编辑器是创建对话框的最简单方法,但它不包含在 Visual Studio 的免费 Express 版本中。您将设计一个对话框,其中包含一个用于提示的文本控件和一个供用户填写的编辑控件。您使用
DialogBox
Windows 函数呈现该对话框,当用户单击“确定”按钮时该对话框将返回或对话框一角的 X。 Microsoft 在此处提供了一些相关文档。有一些可用的平台试图使该过程变得更容易,例如 MFC、WTL、Qt 和 wx,但这就是使用纯 Windows API 的方式。
Microsoft doesn't consider your use case to be common enough to optimize for, as with
MessageBox
. They expect you to lay out a dialog with many controls on it, perhaps with some complex interaction with the controls, and only respond once the dialog is fully filled in. What you're asking for is just the simplified version of that.The resource editor is the easiest way to create a dialog, but that's not included in the free Express version of Visual Studio. You would design the dialog with a text control for the prompt and an edit control for the user to fill in. You present the dialog with the
DialogBox
Windows function, and it returns when the user hits the OK button or the X in the corner of the dialog. Microsoft has some documentation for it here.There are a few platforms available that try to make the process easier, such as MFC, WTL, Qt, and wx, but this is how you'd do it with the pure Windows API.
我的答案基于 Stephen Quan 对 How to load 的回答&从 C++ 中调用 VBScript 函数? 添加了完整的 UTF-8 支持,您可以从 CPP 文件中的代码注释中了解到这一点。与使用 Microsoft 脚本控件创建 InputBox 不同,它可以在 x86 和 x64 可执行文件、库和控件中使用。
"inputbox.h":
"inputbox.cpp":
创建上述两个文件,然后将它们添加到您的 Visual Studio 项目中。
在任何您想要输入或密码框功能的文件中(在标题中找到),只需包含标题:
我还摆脱了 VBScript 输入框标题栏中的默认 Windows 应用程序图标,因为很多人我'我见过有人抱怨看到那里有多么丑陋。
如果您有任何疑问,请告诉我。
My answer is based on Stephen Quan's answer to How to load & call a VBScript function from within C++? Added full UTF-8 support, as you can gather from the code comments in the CPP file. Unlike using Microsoft Script Control to create the InputBox, this can be used in x86 and x64 executables, libraries, and controls.
"inputbox.h":
"inputbox.cpp":
Create the two above files and then add them to your Visual Studio project.
In any file you want the input or password box functions, (found in the header), simply include the header:
I also got rid of the default Windows application icon in the title bar of the VBScript InputBox, because a lot of people I've seen complain about how ugly it is to see that there.
Let me know if you have any questions.
我不得不承认,多年来我在输入框方面并没有真正做太多事情,但基本上你必须走出 C++ 才能获得任何类型的图形输入框。出于可移植性的原因,该语言中根本没有内置任何机制来处理此类内容。我不记得它是否也适用于 C++,但 C 甚至不假设你有控制台。不管怎样,你最好的选择是你已经尝试过的东西:Win32 API、Qt 等。但是,如果你可以使用控制台,请随意使用 iostream 库来完成工作。
I have to admit that I haven't really done much in the way of input boxes in ages, but you basically have to go outside C++ in order to get any kind of graphical input box. There's simply no mechanism built into the language for that kind of stuff for portability reasons. I don't remember if it applied to C++ as well, but C doesn't even assume you have a console. Anyway, your best bet would be something along the lines you were already trying: Win32 API, Qt, etc. If you can use the console, however, feel free to just use the iostream library to get the job done.
使用控制台窗口更适合程序提示用户、继续、再次提示用户等等的通信模式。
为此,您可以使用标准库的工具,例如
cin
和cout
。Using a console window is better suited to the mode of communication where a program prompts the user, continues, prompts the user again, and so on.
And for that you can use the standard library's facilities like
cin
andcout
.与 Visual Basic 和其他语言不同,C++ 中没有“内置”输入框之类的命令。与 MessageBox 可以直接调用不同,InputBox() 需要编写。事实上,我已经这样做了。 以下文章介绍了如何将这样的 InputBox 实现为小型静态库的一部分,可以在任何 Win32 c++ 程序中使用该静态库,无需任何资源。 Github 上的源代码。它可以如下使用:
例如:
Unlike Visual Basic and other languages, there is no "built in" Input Box like command in c++. Unlike MessageBox that can be just invoked, InputBox() needs to be written. In fact, I have done so. The following article describes how to implement such InputBox as part of a small Static Library that can be used, with no Resources, from any Win32 c++ program. Source code at Github. It can be used as follow:
For example:
试试这个:
C++ vs2010 中的输入框
try this:
InputBox in c++ vs2010