简体   繁体   English

尝试从单击按钮的编辑框中捕获文本,然后显示到另一个编辑框

[英]Trying to capture text from an edit box on button click and then display to another edit box

I am very new to Win32 gui coding and have been having quite a lot of difficulty with capturing user inputs. 我对Win32 gui编码非常陌生,并且在捕获用户输入方面遇到了很多困难。

Basically what I am trying to do is have the user enter some text into a textbox (call it textbox A), click a button and then have that text replicated in another textbox (call it textbox B). 基本上,我想做的是让用户在文本框中输入一些文本(称为文本框A),单击按钮,然后将该文本复制到另一个文本框中(称为文本框B)。 I am trying to use the GetWindowsText() function to copy the text in the from textbox A to a buffer and then when the button is clicked send a message to textbox B setting the text equal to the contents of the buffer. 我正在尝试使用GetWindowsText()函数将文本框A中的文本复制到缓冲区,然后单击该按钮时,将消息发送到文本框B,将文本设置为等于缓冲区的内容。 Simple right? 简单吧? ... The code from my callback is: ...来自我的回调的代码是:

    case WM_COMMAND:
    switch(LOWORD(wParam))
    {
        case IDC_MAIN_BUTTON:
        {
            GetWindowText(hwnd_path, buffer, 5);
            SendMessage(hEdit,WM_SETTEXT,NULL,(LPARAM)buffer);
            MessageBox(NULL, buffer, TEXT("Edit Text"), MB_OK);
        }
        break;
    }
    break;

hwnd_path is the handle for textbox A, hEdit is the handle for textbox B and buffer has been declared as: TCHAR buffer[6] - these variables have been declared globally so they should all be in the scope of the callback function. hwnd_path是文本框A的句柄,hEdit是文本框B的句柄,并且缓冲区已声明为: TCHAR buffer[6] -这些变量已全局声明,因此它们都应在回调函数的范围内。 I've added the messagebox just to double check the value of buffer. 我添加了消息框只是为了仔细检查缓冲区的值。

Problem is that nothing appears in either textbox B or the messagebox upon clicking the button - it appears that buffer remains empty after the GetWindowText(hwnd_path, buffer, 5) command. 问题是单击该按钮后,文本框B或消息框中GetWindowText(hwnd_path, buffer, 5)显示任何内容-在执行GetWindowText(hwnd_path, buffer, 5)命令后,缓冲区保持为空。

If anyone could offer any advice I'd be very grateful. 如果有人可以提供任何建议,我将不胜感激。

Jack 插口

Use SetWindowText instead of calling SendMessage. 使用SetWindowText而不是调用SendMessage。

I suspect that your hEdit and/or hwnd_path handles are not what you think they are. 我怀疑您的hEdit和/或hwnd_path句柄不是您认为的那样。 Make sure these HWND variables are actually referencing your edit and text control handles when you created them. 在创建它们时,请确保这些HWND变量实际上引用了您的编辑和文本控件句柄。

Don't expect the dialog to instantly update after you call "MessageBox". 不要期望在调用“ MessageBox”后对话框立即更新。 It may take a few internal message posts to for the SetWindowText API to actually finish. SetWindowText API可能需要一些内部消息才能完成。 MessageBox does pump messages internally, so this is probably ok. MessageBox确实在内部泵送消息,因此这可能没问题。

Here's some sample code that does work. 这是一些有效的示例代码。 It was written within a DialogBox wndproc. 它是在DialogBox wndproc中编写的。 If you are in a dialog box, replace the two calls to GetDlgItem with an assignment of the window handle returned when you created the edit and text field. 如果在对话框中,请将对GetDlgItem的两个调用替换为创建编辑和文本字段时返回的窗口句柄的分配。

    case WM_COMMAND:
    {
        switch LOWORD(wParam)
        {
            case IDC_BUTTON1:
            {
                wchar_t szEditText[300] = {0};

                // If you aren't using DialogBox(), then replace these apis by assigneing hEdit and hText with the HANDLES of your created controls.
                HWND hEdit = GetDlgItem(hwnd, IDC_EDIT1);
                HWND hText = GetDlgItem(hwnd, IDC_TEXT1);

                GetWindowText(hEdit, szEditText, ARRAYSIZE(szEditText));
                SetWindowText(hText, szEditText);

                // If this Window was created with DialogBox, then return TRUE
                // Otherwise, return 0.
                return TRUE;
            }

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM