[英]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.