向文本字段添加换行符 (Win32) [英] Add newline to a text field (Win32)

查看:47
本文介绍了向文本字段添加换行符 (Win32)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个记事本克隆.现在我的文本加载正常,但在它们是换行符的地方,它们不会在文本字段中换行.

I'm making a Notepad clone. Right now my text loads fine but where their are newline characters, they do not make newlines in the text field.

我是这样加载的:

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 make it do the new line characters?

谢谢

(修复了它,结果流没有给我 CR,所以我不得不插入它们.

(Fixed it, turns out the stream was not giving me CR's so I had to insert them.

推荐答案

确保您设置了 ES_MULTILINE|ES_WANTRETURN.

多行编辑控件使用软换行符"来强制换行.要指示软换行符",请使用 CRCRLF (来源).所以我想你需要用 CRCRLF 替换你所有的 CRLF(或你的文件使用的任何 eol 字符).您已经在逐个字符地读取文件,因此您只需在缓冲区中插入一个额外的 CR.

Multiline edit controls use "soft line break characters" to force it to wrap. To indicate a "soft line break", use CRCRLF (source). So I guess you need to replace all your CRLF's (or whatever eol character your file uses) with CRCRLF. You're already reading your file in character by character, so you can just insert an extra CR into the buffer.

作为旁注,您最终会希望在单独的线程(即不是 UI 线程)上执行文件 IO,以便在读取文件时不会挂起 UI.如果 UI 显示某种加载动画或进度条,那就太好了.

As a side note, you'll eventually want to do the file IO on a separate thread (i.e. not the UI thread) so that you don't hang the UI while you're reading the file. It'd be nice if the UI showed some sort of loading animation, or progress bar.

这篇关于向文本字段添加换行符 (Win32)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆