Add new line to textbox (Win32)

I am making a Notepad clone. Right now my text is loading fine, but where they are newlines they do not create newlines in the textbox.

I load it like this:

void LoadText(HWND ctrl,HWND parent)
{

    int leng;
    char buf[330000];

    char FileBuffer[500];
    memset(FileBuffer,0,500);

    FileBuffer[0] = '*';
    FileBuffer[1] = '.';
    FileBuffer[2] = 't';
    FileBuffer[3] = 'x';
    FileBuffer[4] = 't';

    OPENFILENAMEA ofn;
    memset(&ofn, 0, sizeof(OPENFILENAMEA));
    ofn.lStructSize = sizeof(OPENFILENAMEA);
    ofn.hwndOwner = parent;
    ofn.lpstrFile = FileBuffer;
    ofn.nMaxFile = 500;
    ofn.lpstrFilter = "Filetype (*.txt)\0\0";
    ofn.lpstrDefExt = "txt";
    ofn.Flags = OFN_EXPLORER;

    if(!GetOpenFileNameA(&ofn))
    {
        return;
    }
    ifstream *file;
    file = new ifstream(FileBuffer,ios::in);

    int lenn;
    lenn = 0;

    while (!file->eof())
    {

        buf[lenn] = file->get();
        lenn += 1;

    }
    buf[lenn - 1] = 0;

    file->read(buf,lenn);
    SetWindowTextA(ctrl,buf);
    file->close();
}

      

How can I do this for newline characters?

thanks

(Fixed, it turns out the thread didn't give me CRs, so I had to insert them.

+2


a source to share


1 answer


Make sure you have installed ES_MULTILINE|ES_WANTRETURN

.

Multi-line editing tools use "line breaks" to force them to wrap. Use CRCRLF to indicate "soft line break" ( source ). So I think you need to replace the whole CRLF (or whatever EOL character your file uses) with CRCRLF. You are already reading your file at character by character, so you can just insert the extra CR into the buffer.



As a side note, you will eventually want to make the IO file on a separate thread (that is, not on the UI thread) so you don't hang the UI while you read the file. It would be nice if the UI showed some kind of loading animation or progress bar.

+5


a source







All Articles