简体   繁体   English

将文本字段中的值存储在.txt中

[英]Store a value from text field in a .txt

My problem is very simple (at least i think it is). 我的问题很简单(至少我认为是)。 I'd like to capture a value from a text input and store it inside a txt file. 我想从文本输入中捕获一个值并将其存储在txt文件中。 Right now i'm using this code, and the action button is intended to write the content in file when clicked. 现在我正在使用此代码,并且单击操作按钮旨在将内容写入文件中。 But i'm getting the value of button registered in the file instead. 但是我得到的是在文件中注册的按钮的值。

    case WM_CREATE:{
        CreateWindow(
            TEXT("EDIT"), TEXT("value"), 
            WS_VISIBLE | WS_CHILD | WS_BORDER, 
            190, 50, 50, 20,
            hwnd, (HMENU) NULL, NULL, NULL 
        );

        CreateWindow(
            TEXT("BUTTON"), TEXT("Ok"), 
            WS_VISIBLE | WS_CHILD, 
            250, 10, 30, 20,
            hwnd, (HMENU) ID_BTN, NULL, NULL 
        );

        break;
    }

    case WM_COMMAND: {
        if (LOWORD(wParam) == ID_BTN) {
            std::ofstream outfile;
            outfile.open("C:\\file.txt", std::ios_base::app);
            outfile << ID_BTN;
            outfile.close();
            MessageBox(hwnd, "Done!", "Title", MB_ICONINFORMATION);
              return 0;
         }
         break;
    }

Thanks. 谢谢。


EDIT : @ZanLynx, I tried to do what you have said, but compiler keep saying hwndText wasn't declared, when it was. 编辑 :@ZanLynx,我试图做您所说的事情,但是编译器一直在说hwndText未声明。

107 `hwndText' undeclared (first use this function)

Here's the code 这是代码

#define ID_BTN 1
#define ID_TXT 2

LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch (message)
    {
        case WM_CREATE:
        {

            HWND hwndText = CreateWindow(
                TEXT("Edit"), TEXT("Write here"), 
                WS_VISIBLE | WS_CHILD | WS_BORDER | ES_AUTOHSCROLL | ES_AUTOVSCROLL,  
                190, 10, 100, 20, 
                hwnd, (HMENU) ID_TXT, NULL, NULL
            );

            CreateWindow(
                TEXT("BUTTON"), TEXT("OK"), 
                WS_VISIBLE | WS_CHILD, 
                250, 10, 30, 20,
                hwnd, (HMENU) ID_BTN, NULL, NULL
            );

            break;
        }

        case WM_COMMAND: 
        {

            if (LOWORD(wParam) == ID_BTN) 
            {
                LRESULT iTextSize = SendMessage(hwndText, EM_GETLIMITTEXT, 0, 0);
                char *szText = new char[iTextSize];
                SendMessage(hwndText, WM_GETTEXT, iTextSize, (LPARAM)szText);

                std::ofstream outfile;
                outfile.open("C:\\f.txt", std::ios_base::app);
                outfile << szText;
                outfile.close();

                MessageBox(hwnd, "Done!", "Title", MB_ICONINFORMATION);
                return 0;
            };  

            break;
        }

        case WM_DESTROY:
            PostQuitMessage (0);
            break;

        default:
            return DefWindowProc (hwnd, message, wParam, lParam);
    }

    return 0;
}

Store the handle to your EDIT window. 将句柄存储到“编辑”窗口中。

Then you can use the Edit Control functions to get the text so you can write it into a file. 然后,您可以使用“ 编辑控件”功能来获取文本,以便可以将其写入文件。

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

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