Listing 4: ConStreamDemoDlg.cpp — sample use of the ConStream class

// This file implements most of the interesting code in the
// ConStreamDemo program. Note first that the ConStream object
// is implemented as a member called m_Log in this program. m_Log
// is created when the dialog object is created, and destroyed
// when the dialog object is destroyed. No code is needed in the
// app to make this happen.
//
// The Console window is opened and closed in response to a 
// modification of the check box on the main dialog. It displays
// some data in the console window in response to a button press,
// and to a change in focus in the edit window. The four member
// functions that implement this are the last four functions in 
// this file.
//
#include "stdafx.h"
#include "ConStreamDemo.h"
#include "ConStreamDemoDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////
// CConStreamDemoDlg dialog

CConStreamDemoDlg::CConStreamDemoDlg(CWnd* pParent /*=NULL*/)
    : CDialog(CConStreamDemoDlg::IDD, pParent)
{
    //{{AFX_DATA_INIT(CConStreamDemoDlg)
        // NOTE: the ClassWizard will add member initialization
        // here
    //}}AFX_DATA_INIT
    m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CConStreamDemoDlg::DoDataExchange(CDataExchange* pDX)
{
    CDialog::DoDataExchange(pDX);
    //{{AFX_DATA_MAP(CConStreamDemoDlg)
    DDX_Control(pDX, IDC_NUMBER, m_Number);
    DDX_Control(pDX, IDC_USE_DEBUG_WINDOW, m_UseDebugWindow);
    //}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CConStreamDemoDlg, CDialog)
    //{{AFX_MSG_MAP(CConStreamDemoDlg)
    ON_WM_PAINT()
    ON_WM_QUERYDRAGICON()
    ON_BN_CLICKED(IDC_USE_DEBUG_WINDOW, OnUseDebugWindow)
    ON_EN_KILLFOCUS(IDC_NUMBER, OnKillfocusNumber)
    ON_EN_SETFOCUS(IDC_NUMBER, OnSetfocusNumber)
    ON_BN_CLICKED(IDC_DISPLAY, OnDisplay)
    //}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////
// CConStreamDemoDlg message handlers

BOOL CConStreamDemoDlg::OnInitDialog()
{
    CDialog::OnInitDialog();

    SetIcon(m_hIcon, TRUE);            // Set big icon
    SetIcon(m_hIcon, FALSE);        // Set small icon
    
    // TODO: Add extra initialization here
    
    return TRUE;  // return TRUE  unless you set the focus to a
                  // control
}

void CConStreamDemoDlg::OnPaint() 
{
    if (IsIconic())
    {
        CPaintDC dc(this); // device context for painting

        SendMessage(WM_ICONERASEBKGND,
                    (WPARAM) dc.GetSafeHdc(), 0);

        // Center icon in client rectangle
        int cxIcon = GetSystemMetrics(SM_CXICON);
        int cyIcon = GetSystemMetrics(SM_CYICON);
        CRect rect;
        GetClientRect(&rect);
        int x = (rect.Width() - cxIcon + 1) / 2;
        int y = (rect.Height() - cyIcon + 1) / 2;

        // Draw the icon
        dc.DrawIcon(x, y, m_hIcon);
    }
    else
    {
        CDialog::OnPaint();
    }
}

HCURSOR CConStreamDemoDlg::OnQueryDragIcon()
{
    return (HCURSOR) m_hIcon;
}

//
// When the "Use Debug Window" checkbox is modified, we
// either open or close the console window.
//
void CConStreamDemoDlg::OnUseDebugWindow() 
{
    int i = m_UseDebugWindow.GetState() & 0x3;
    if ( i == 1 )
        m_Log.Open();
    else
        m_Log.Close();
}

//
// A debug message is written when the edit control
// either gains or loses the focus.
//
void CConStreamDemoDlg::OnKillfocusNumber() 
{
    m_Log << _T( "Lost focus in number field\n" );
}

void CConStreamDemoDlg::OnSetfocusNumber() 
{
    m_Log << _T( "Got focus in number field\n" );
}

//
// Pushing the "Display" button invokes this handler, 
// which expects to find a numberic value in the edit 
// field. The field and its validity are printed out
// in the debug console.
//
void CConStreamDemoDlg::OnDisplay() 
{
    BOOL success;
    int value = GetDlgItemInt( IDC_NUMBER, &success );
    m_Log << _T( "Success = " ) << success << endl;
    m_Log << _T( "Value = " ) << value << endl;
}
— End of Listing —