简体   繁体   English

使用DialogBoxIndirect时,如何关闭对话框时用户输入的文本?

[英]When using DialogBoxIndirect, how do I get text the user entered when the dialog closes?

I'm using DialogBoxIndirect() to create a modal dialog in memory. 我正在使用DialogBoxIndirect()在内存中创建模式对话框。 One of the controls that I'm adding to the dialog has the EDIT class, so users can type in information in the dialog. 我要添加到对话框中的控件之一具有EDIT类,因此用户可以在对话框中输入信息。 When the dialog is closed, how do I figure out what the user typed into the EDIT field? 关闭对话框后,如何确定用户在“编辑”字段中键入的内容? I don't have an HWND for the EDIT field or the dialog itself, all I have is the id. 我没有EDIT字段或对话框本身的HWND,我只有ID。 The only way I know of is GetWindowText(), but that requires an HWND. 我知道的唯一方法是GetWindowText(),但这需要HWND。

Code snippet: 程式码片段:

//-----------------------
// Define Edit Input
//-----------------------
lpw = lpwAlign(lpw);    // Align DLGITEMTEMPLATE on DWORD boundary
lpdit = (LPDLGITEMTEMPLATE)lpw;
lpdit->x = 10; lpdit->y = 10;
lpdit->cx = 150; lpdit->cy = 25;
lpdit->id = ID_TEXT2;       // Text input
lpdit->dwExtendedStyle = WS_EX_CLIENTEDGE;
lpdit->style = WS_CHILD | WS_VISIBLE;
lpw = (LPWORD)(lpdit + 1);
*lpw++ = 0xFFFF;
*lpw++ = 0x0081;        // Edit class
lpwsz = (LPWSTR)lpw;
nchar = MultiByteToWideChar(CP_ACP, 0, lpszMessage, -1, lpwsz, 50);
lpw += nchar;
*lpw++ = 0;             // No creation data

//-----------------------
// Define an OK button.
//-----------------------
lpw = lpwAlign(lpw);    // Align DLGITEMTEMPLATE on DWORD boundary
lpdit = (LPDLGITEMTEMPLATE)lpw;
lpdit->x = 10; lpdit->y = 40;
lpdit->cx = 35; lpdit->cy = 13;
lpdit->id = IDOK;       // OK button identifier
lpdit->style = WS_CHILD | WS_VISIBLE | BS_DEFPUSHBUTTON;
lpw = (LPWORD)(lpdit + 1);
*lpw++ = 0xFFFF;
*lpw++ = 0x0080;        // Button class
lpwsz = (LPWSTR)lpw;
nchar = MultiByteToWideChar(CP_ACP, 0, "OK", -1, lpwsz, 50);
lpw += nchar;
*lpw++ = 0;             // No creation data

GlobalUnlock(hgbl);
ret = DialogBoxIndirect(hinst, (LPDLGTEMPLATE)hgbl, GetFocus(), (DLGPROC)GenericDlgProc);
// How do I get the text here, that the user entered into control id ID_TEXT2?

You can use GetDlgItemText() to get text from an edit control using its ID; 您可以使用GetDlgItemText()使用其ID从编辑控件中获取文本。 basically what this does is a GetDlgItem() followed by a GetWindowText() all in one useful function call. 基本上,这是通过一个有用的函数调用来完成一个GetDlgItem()和一个GetWindowText()

However by the time your DialogBoxIndirect() call has returned it's too late to do this - the dialog is gone, and the controls along with it. 但是,当DialogBoxIndirect()调用返回时,已经太晚了-对话框不见了,控件也随之消失了。 You can't read a control's value once it's been destroyed. 控件的值一旦销毁就无法读取。

The usual way to deal with this is to handle WM_DESTROY in your DialogProc , and save the control values there (alternatively, if you have an OK and a Cancel button, you might do this in the WM_COMMAND handler for IDOK instead). 解决此问题的常用方法是在DialogProc处理WM_DESTROY ,并将控制值保存在那里(或者,如果您具有OK和Cancel按钮,则可以在WM_COMMAND处理程序中为IDOK进行此处理)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当用户调整我的对话框大小时,如何强制窗口不在我的对话框中重绘任何内容? - How do I force windows NOT to redraw anything in my dialog when the user is resizing my dialog? 为什么在使用 Linux 的“dialog.h”中的“dialog_checklist”时出现分段错误? - Why do I get a segmentation fault when using "dialog_checklist" from Linux's "dialog.h"? 我应该怎么做才能获得用户输入的最小序列? - What should i do to get the smallest of the sequence entered by the user? 当另一个窗口关闭时,关闭无模式对话框 - Close modeless dialog when another window closes 如何让我的向量在输入后显示 8 个整数? 当我输入 8 个整数时,它只显示零而不是我输入的内容 - How do I get my vector to display 8 integers after entering them? When I do enter 8 integers, it only displays zeros and not what I entered 如何使用 char* 从用户那里获取数据,然后在 C++ 中执行输入数据的深度复制? - How do I take data from user using char* and then performs deep copy of entered data in C++? DialogBoxIndirect创建大于要求的对话框 - DialogBoxIndirect creates dialog bigger than asked 在终端I / O导航时,如何检查用户输入的值是否错误? - How to check whether a user has entered the wrong value when navigating through a Terminal I/O? 当我尝试从文件中读取矩阵时,会出现各种错误,但仅使用输入的数据时工作正常 - When I try to read a matrix from a file, I get all kinds of errors, but works fine when just using data entered in 在MFC中拖动子对话框时如何移动父对话框? - How do i move parent dialog when dragging child dialog, in MFC?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM