我可以从对话框的 DoModal 函数返回自定义值吗?
我想要做的是,在使用 DoModal() 创建对话框并按框中的“确定”退出后,返回自定义值。例如,用户将在对话框中输入几个字符串。
What I wish to do is, after creating a dialog box with DoModal()
and pressing OK in the box to exit it, to have a custom value returned. For example, a couple of strings the user would input in the dialog.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您无法更改
DoModal()
函数的返回值,即使可以,我也不会推荐它。这不是惯用的做法,如果您将其返回值更改为字符串类型,您将无法查看用户何时取消对话框(在这种情况下,返回的字符串值应完全忽略)。相反,请向对话框类添加另一个(或多个)函数,例如
GetUserName()
和GetUserPassword
,然后在DoModal 之后查询这些函数的值
返回IDOK
。例如,显示对话框并处理用户输入的函数可能如下所示:
You can't change the return value of the
DoModal()
function, and even if you could, I wouldn't recommend it. That's not the idiomatic way of doing this, and if you changed its return value to a string type, you would lose the ability to see when the user canceled the dialog (in which case, the string value returned should be ignored altogether).Instead, add another function (or multiple) to your dialog box class, something like
GetUserName()
andGetUserPassword
, and then query the values of those functions afterDoModal
returnsIDOK
.For example, the function that shows the dialog and processes user input might look like this:
我正在寻找答案,并同意在大多数情况下您不会更改对话框的标准行为。但在某些情况下,您可能希望选择用户实际响应的内容,例如,如果您有多个按钮,并且特别希望他们选择顶部的“确定”而不是底部的“确定”。你知道指标。
或者说,如果对话框在运行您的函数时导致错误,您是否希望得到稍微不同的结果。最好返回一个不是 IDOK 而是其他值的值。
我在这里找到了
Dialog::EndDialog()
,其中包含详细信息和使用示例:MSDN: Dialog::EndDialogI was looking for an answer and agree that in most cases that you would not change the standard behavior of a dialog. But there might be a case where you would like to pick what the user is actually responding say if you had several buttons and want specifically that they picked the OK at the top versus the OK at the bottom. You know for metrics.
Or say if you wanted to have slightly different results if the dialog caused an error when running on of your functions. It would be nice to return a value that is not IDOK but maybe some other value.
I found
Dialog::EndDialog()
with details and an example of usage here: MSDN: Dialog::EndDialog我认为这是不可能的(或合理的)。 DoModal 返回一个 INT_PTR,通常用于了解用户退出对话框时执行的操作(按“确定”、“取消”、出现错误...)。做到这一点的方法是拥有公共成员或函数,对话框集和对话框的调用者可以访问这些成员或函数来了解值。
就像这样:
例如,这就是您使用 CFileDialog 的方式。
I don't think it is possible (or reasonable). DoModal returns an INT_PTR, which is usually used to know what the user did to exit the dialog (press OK, Cancel, there was an error...). The way to do it is have public members or functions which the dialog set and the caller of the dialog can access to know the values.
Like so:
It's the way you would use CFileDialog, for example.