【问题标题】:WinAPI edit control doesn't display newlinesWinAPI 编辑控件不显示换行符
【发布时间】:2011-06-26 17:12:38
【问题描述】:

嗯,这只是对了一半。换行符在大多数情况下都可以正常工作,但是当我将文件加载到其中时,不会显示任何换行符。将文本复制并粘贴到 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());}

【问题讨论】:

  • 编辑控件使用内存中的\r\n。检查文件是否仅使用 \r。验证您可以手动向编辑控件添加新行。您使用什么样式来创建控件?
  • 是什么让您认为来自 fread 的缓冲区包含以零结尾的字符串?此外,不要分段更新。只需调用 SetWindowText。
  • 代码错误。但是,如果您根本看不到换行符,那么您忘记为编辑框打开 ES_MULTILINE 样式。

标签: c++ winapi edit


【解决方案1】:

由于您以文本模式打开文件,因此您的文本以\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.

干杯,

【讨论】:

    猜你喜欢
    • 2020-12-14
    • 1970-01-01
    • 2014-01-29
    • 1970-01-01
    • 1970-01-01
    • 2011-05-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多