EditLog类的使用-将C++标准输出cout重定向到edit控件

原文地址:https://blog.csdn.net/vrix/article/details/1808421

原文中的下载地址已失效,重新给出csdn的下载:https://download.csdn.net/download/augusdi/4012663


要解决的问题:

我曾经想使用EDIT控件来快速的处理异步文本输出。我需要在很多线程里记录大量的信息。有时候每秒1000个Log信息。自从我创建了一些函数把我要的对象信息输出到 std::cout ,我也想吧edit控件作为std::ostream.

特点:

  • 异步, 快速的输出信息到任何的edit 控件
  • 你可以在打印信息的时候查看记录,选择和复制文本等等。
  • 可以支持MFC和飞MFC程序
  • 使用了子类化,你可以把任何CEdit派生出的类和一同使用
  • 支持多线程
  • basic_streambuf 用来创建和 重定向 std::ostream (such as std::cout) 的输出到 edit控件

使用 CEditLog

创建CEditLog 的一个实例并通过SetEditControl() 方法把它和EDIT控件绑定,然后你就可以用AddText()成员函数来添加一些文本到EDIT控件了。

class CEditLog : protected CSubclassWnd
{
public:
    typedef CSubclassWnd root_type;

    // Constructs the CEditLog. You can pass the edit controls handle
    // here or set it later using the SetEditCtrl() function.
    // To increase performance CEditLog repaints the edit-control
    // only if the caret resides in the last line. Otherwise the
    // control will be refreshed only every nMaxRefreshDelay msec.
    CEditLog( HWND hEdit = NULL, UINT nMaxRefreshDelay = 500 );
    
    // Adds some text to the end of the edit control. Works asynchronously
    // (using PostMessage()) and is save to be called by multiple threads.
    // If you pass true for bLFtoCRLF every LF (ASCII 10) (as it is used in
    // most cases for end-of-line) will be converted to a CR/LF (ASCII 10/13)
    // sequence as it is needed by the windows edit control.
    virtual void AddText( LPCWSTR pwszAdd, bool bLFtoCRLF = false );
    
    // Converts pszAdd to UNICODE and calls the above
    void AddText( LPCSTR pszAdd, bool bLFtoCRLF = false );

    // Sets the edit-control to be used for logging.
    // Pass NULL to stop text logging.
    virtual void SetEditCtrl( HWND hEdit );

    HWND GetEditCtrl() const

    ...
};

实例代码说明

实例代码中演示了CEditLog 的用法以及basic_editstrbuf 如何重定向 std::cout 到EDIT控件.

CEditLog 用到了William E. Kempf 写的CSubclassWnd 类,实例代码中也包含了CSubclassWnd 类

-------------以上翻译由vrix.yan完成于2007.09.30 21:18

在使用此类的过程当中,我总结了一下的步骤:

1>将实例工程中的code子目录,复制到你的工程目录中

2>在你的对话框xxDlg.h 里引入头文件,并定义一些变量

#include "EditLog.h"
#include "editlog_stream.h"

 // The logger object
 CEditLog   m_EditLogger;
 // ANSI and UNICODE stream buffers for the EditLogger
 std::editstreambuf m_EditStrBuf;
 std::weditstreambuf m_EditStrBufW;

 // Used to save the previos values for cout and wcout
 std::basic_streambuf<char>*  m_pOldBuf;
 std::basic_streambuf<wchar_t>* m_pOldBufW;

3>在你的stdafx.h中加入

// Include core STL header
#include <string>
#include <streambuf>
#include <iostream>

4>在你的xxDlg.cpp 中记得添加一下的代码

CEditLogDlg::CEditLogDlg(CWnd* pParent /*=NULL*/)
 : CDialog(CEditLogDlg::IDD, pParent), m_EditStrBuf( m_EditLogger ), m_EditStrBufW( m_EditLogger )

5>在你的xxDlg.cpp里面OnInitDialog初始化

 // 设置EDIT控件为LOG的目标
 m_EditLogger.SetEditCtrl( m_ctrlEdit.m_hWnd );  //记得要先定义Edit 控件


 // 重定向cout 的输出到 Edit控件
 m_pOldBuf = cout.rdbuf( &m_EditStrBuf );
 // unicode 版本
 m_pOldBufW = wcout.rdbuf( &m_EditStrBufW );

 // 用法
 cout << "cout (ANSI) works :-)" << endl;
 wcout << L"wcout (UNICODE) works also ;-)" << endl;
 cout << "/n/n";

6>在你的WM_DESTROY消息里加入

void CEditLogDlg::OnDestroy() 
{
 // Restore the previous stream state for cout and wcout
 cout.rdbuf( m_pOldBuf );
 wcout.rdbuf( m_pOldBufW );

 CDialog::OnDestroy();
}

我通过以上的步骤,成功的在我的程序里使用了EDITLOG类,希望对大家有所帮助。


C语言标准有一个函数叫做freopen(),它可以用于重定向C程序的输入和输出流。通过调用freopen()函数,我们可以将标准输入流stdin和标准输出stdout重定向到文件或者管道。 要将C程序的标准输入流重定向到管道,可以使用如下命令: ``` freopen("input.txt", "r", stdin); ``` 这会将程序的标准输入流重定向到名为"input.txt"的文件。这样,程序执行时会将文件的内容作为输入。 同样地,要将C程序的标准输出重定向到管道,可以使用如下命令: ``` freopen("output.txt", "w", stdout); ``` 这会将程序的标准输出重定向到名为"output.txt"的文件。这样,程序执行后的输出会被写入到文件。 需要注意的是,freopen()函数也可以对C的cin和cout进行重定向使用方法与上述似,只需要将文件名替换为管道的名称即可。 通过这种方式,我们可以在C程序实现输入输出重定向,从而实现输入和输出的管道操作。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [C++输入输出重定向(3种方法)](https://blog.csdn.net/weixin_39536630/article/details/116964030)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值