开发者

WinAPI edit control doesn't display newlines

开发者 https://www.devze.com 2023-03-15 05:58 出处:网络
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. Copying the text and pasting it into Notepad++ with view all charact

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. Copying the text and pasting it into Notepad++ with view all characters turned on shows that the carriage return and line feed are there.

My loading code:

void open_file(HWND hwnd,const char* fname){
    SendMessage(textbox,WM_SETTEXT,(WPARAM)0,(LPARAM)"");
    FILE* file=fopen(fname,"r");
  开发者_如何学C  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. Possibly the text edit control requires \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.,

0

精彩评论

暂无评论...
验证码 换一张
取 消