简体   繁体   English

WinAPI编辑控件不显示换行符

[英]WinAPI edit control doesn't display newlines

Well, that's only half true. 嗯,这只是半真的。 Newlines work fine for the most part, but when I load a file into it, none of the newlines are shown. Newlines在大多数情况下运行良好,但是当我将文件加载到其中时,没有显示任何换行符。 Copying the text and pasting it into Notepad++ with view all characters turned on shows that the carriage return and line feed are there. 复制文本并将其粘贴到Notepad ++中并查看所有打开的字符,表明回车符和换行符都在那里。

My loading code: 我的加载代码:

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());}

Since you're opening the file in text mode your text represents newline by \\n . 由于您是在文本模式下打开文件,因此文本代表换行符\\n Possibly the text edit control requires \\r\\n . 可能文本编辑控件需要\\r\\n

One possibility is to do like this (off the cuff) 一种可能性就是这样做(脱下袖口)

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() ).
}

But better, set the text all at once, like: 但更好的是,一次设置文本,如:

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.

Cheers & hth., 干杯&hth。,

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

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