代码之家  ›  专栏  ›  技术社区  ›  Sydius

如何在Win32 API中使用RichEdit控件(如控制台)?

  •  2
  • Sydius  · 技术社区  · 16 年前

    我有一个 RichEdit x . 我还想它自动滚动到底部显示最新的行时,添加。

    我一直在用 SetWindowText 然而,随着一些成功,我突然想到,可能有一种更有效的方法可以将文本追加到末尾,并从开头删除文本,而不必每次都替换所有文本。这是真的吗?如果是,我该怎么办?

    另外,当添加新文本时,如何使其自动滚动到窗口底部?

    文本编辑校件 (只需在XP和Vista上使用普通的Win32 API)。

    2 回复  |  直到 16 年前
        1
  •  3
  •   Jerry Coffin    16 年前

    要添加文本,请将所选内容设置为文本的结尾(EM_setel),然后用新文本(EM_REPLACESEL)替换所选内容。

        2
  •  1
  •   bluish dmajkic    13 年前

    我寄给你一些示例类的方法 cConsole :

    class cConsole {
    private:
        //-------------------
        int lines;
        int max_lines;             // Init it with your choise ( 300 )
        //-------------------
        char* buf;
        int buf_size;
        //-------------------
        int CheckMemory( int size );
        void NewLine( int new_lines );
        void InternalPrint( char* msg, int size );
    
    public:
        HWND hWin;
        void Print( char* msg );    // Add text data through this methods
        void Print( char* msg, int size );
        cConsole( );
        ~cConsole( );
    };
    
    int cConsole::CheckMemory( int size ) {
    int rv = 1;
    if( size + 16 >= buf_size ) {
        int new_buf_size = size + 1024;
        char* new_buf = ( char* )realloc( buf, new_buf_size );
        if( new_buf != NULL ) {
            buf = new_buf;
            buf_size = new_buf_size;
        } else {
            rv = 0;
        }
    }
    return rv;
    }
    
    void cConsole::NewLine( int new_lines ) {
    int rem_lines = ( new_lines + lines + 1 ) - max_lines;
    if( rem_lines <= 0 ) {
        lines += new_lines;
    } else {
        int sel = SendMessage( hWin, EM_LINEINDEX, rem_lines, 0 );
    
        SendMessage( hWin, EM_SETSEL, 0, (LPARAM)sel );
        SendMessage( hWin, EM_REPLACESEL, FALSE, (LPARAM)"" );
        SendMessage( hWin, WM_VSCROLL, SB_BOTTOM, NULL );
    
        lines = max_lines - 1;
    }
    }
    
    void cConsole::Print( char* msg ) { InternalPrint( msg, -1 ); }
    void cConsole::Print( char* msg, int size ) { if( size < 0 ) size = 0; InternalPrint( msg, size ); }
    
    void cConsole::InternalPrint( char* msg, int size ) {
    int s, t = 0;
    int new_lines = 0;
    char* tb;
    
    // msg only mode
    if( size == -1 ) size = 0x7fffffff;
    
    if( msg != NULL && size && CheckMemory( t ) ) {
        for( s = 0; msg[ s ] && ( s < size ); s++ ) {
            if( msg[ s ] == '\r' ) continue;
            if( !CheckMemory( t ) ) break;
            if( msg[ s ] == '\n' ) {
                ++new_lines;
                buf[ t++ ] = '\r';
            }
            buf[ t++ ] = msg[ s ];
        }
        buf[ t ] = '\0';
    }
    if( t && msg != NULL ) {
        tb = buf;
    } else {
        ++new_lines;
        tb = "\r\n";
    }
    
    SendMessage( hWin, EM_SETSEL, (WPARAM)-2, (LPARAM)-1 );
    SendMessage( hWin, EM_REPLACESEL, FALSE, (LPARAM)tb );
    SendMessage( hWin, WM_VSCROLL, SB_BOTTOM, NULL );
    
    if( new_lines ) NewLine( new_lines );
    }
    

    建立你自己的类,并检查这个!

    推荐文章