c++ - WinAPI 编辑控件不显示换行符

标签 c++ winapi edit

好吧,这只对了一半。换行符在大多数情况下工作正常,但是当我将文件加载到其中时,没有显示任何换行符。复制文本并将其粘贴到打开查看所有字符的 Notepad++ 中,显示回车符和换行符在那里。

我的加载代码:

void open_file(HWND hwnd,const char* fname){
    SendMessage(textbox,WM_SETTEXT,(WPARAM)0,(LPARAM)"");
    FILE* file=fopen(fname,"r");
    fullpath=fname;
    filename=fullpath.substr(fullpath.rfind('\\')+1,fullpath.length());
    int pos;
    while(!feof(file)){
        pos=GetWindowTextLength(textbox);
        SendMessage(textbox,EM_SETSEL,pos,pos);
        fread(buffer,2048,sizeof(char),file);
        SendMessage(textbox,EM_REPLACESEL,false,(LPARAM)buffer);}
    fclose(file);
    SendMessage(hwnd,WM_SETTEXT,0,(LPARAM)filename.c_str());}

最佳答案

由于您是以文本模式打开文件,因此您的文本代表 \n 的换行符。可能文本编辑控件需要 \r\n

一种可能性是这样做(即兴)

std::string line;
std::ifstream file( fname );
while( std::getline( file, line ) )
{
    line += "\r\n";
    // Append  the line to the edit control here (use c_str() ).
}

但更好的是,一次设置所有文本,例如:

std::string line;
std::string text;
std::ifstream file( fname );
while( std::getline( file, line ) )
{
    line += "\r\n";
    text += line;
}
SetWindowText( textbox, text.c_str() ... whatever );  // Not sure of args, check docs.

干杯,

关于c++ - WinAPI 编辑控件不显示换行符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6482981/

相关文章:

delphi - 使用 LOGON32_LOGON_NETWORK 从 Delphi 重复调用 LogonUser 会导致帐户被锁定吗?

php - 外部编辑 JavaScript 变量

module - 如何破解已安装的 perl6 模块源?

c++ - 在 std::string 中存储 unicode UTF-8 字符串

c++ - 使用 WaitForMultipleObjects 时如何获取哪个对象超时?

c++ - Ncurses 窗口不显示任何内容

c - 截屏代码产生黑色位图

editor - sitecore 编辑框架 buttonRoot 未设置为编辑框架

c++ - 使用父类的运算符而不是子类

c++ - 存在 ARM GNU 编译器 -j[jobs] 选项