简体   繁体   中英

How to display output from a MFC based application class file

I am a beginner to programming, I have made a MFC based GUI application and linked the variables by Cptr to a class file. Now i need to print the outputs of that class file. The outputs are variable values hence I am unable to display the string.

I had made a Output dialog box along with its class & header file, but am unable to transfer the values from one class to another and the values are not printed in the Output dialog box. I tried TRACE & OutputDebugString but I am unsuccessful, I am not sure if transferring the values is a correct option.

Class file code:

std::cout << HV_Tmid_1 << std::endl;
std::cout << HV_Tmid_2 << std::endl;
std::cout << HV_Tmid_3 << std::endl;
std::cout << HV_Tmid_4 << std::endl;
std::cout << LV_Tmid_1 << std::endl;
std::cout << LV_Tmid_2 << std::endl;
std::cout << LV_Tmid_3 << std::endl;
std::cout << LV_Tmid_4 << std::endl;

Output Dialog Class Code:

// Output.cpp : implementation file
//

#include "stdafx.h"
#include "Thermal Tool.h"
#include "Output.h"
#include "afxdialogex.h"
#include "TT.h"
#include <iostream>
#include <sstream>
#include <string>


// Output dialog

IMPLEMENT_DYNAMIC(Output, CDialog)

Output::Output(CWnd* pParent /*=nullptr*/)
    : CDialog(IDD_OUTPUT, pParent)

{
    m_d_hvmid1 = _T("");
    m_d_hvmid2 = _T("");
    m_d_hvmid3 = _T("");
    m_d_hvmid4 = _T("");
    m_d_lvmid1 = _T("");
    m_d_lvmid2 = _T("");
    m_d_lvmid3 = _T("");
    m_d_lvmid4 = _T("");
}

Output::~Output()
{

}

void Output::DoDataExchange(CDataExchange* pDX)
{
    CDialog::DoDataExchange(pDX);
    DDX_Text(pDX, IDC_HV_MID1, m_d_hvmid1);
    DDX_Text(pDX, IDC_HV_MID2, m_d_hvmid2);
    DDX_Text(pDX, IDC_HV_MID3, m_d_hvmid3);
    DDX_Text(pDX, IDC_HV_MID4, m_d_hvmid4);
    DDX_Text(pDX, IDC_LV_MID1, m_d_lvmid1);
    DDX_Text(pDX, IDC_LV_MID2, m_d_lvmid2);
    DDX_Text(pDX, IDC_LV_MID3, m_d_lvmid3);
    DDX_Text(pDX, IDC_LV_MID4, m_d_lvmid4);
}


BEGIN_MESSAGE_MAP(Output, CDialog)
END_MESSAGE_MAP()


// Output message handlers

void Output::AssignDlgToPtr1()
{
        m_d_hvmid1.Format(_T("%f"), Cptr.HV_Tmid_1);
        m_d_hvmid2.Format(_T("%f"), Cptr.HV_Tmid_2);
        m_d_hvmid3.Format(_T("%f"), Cptr.HV_Tmid_3);
        m_d_hvmid4.Format(_T("%f"), Cptr.HV_Tmid_4);
        m_d_lvmid1.Format(_T("%f"), Cptr.LV_Tmid_1);
        m_d_lvmid2.Format(_T("%f"), Cptr.LV_Tmid_2);
        m_d_lvmid3.Format(_T("%f"), Cptr.LV_Tmid_3);
        m_d_lvmid4.Format(_T("%f"), Cptr.LV_Tmid_4);

}

The Output is also shown empty due to

void CThermalToolDlg::OnCalculate()
{
    UpdateData();
    if (!Validate()) return;
    AssignDlgToPtr();
    Cptr.OnCalculate();
    // need to add something here to run Output Dialog class before displaying output box
    // AssignDlgToPtr1() is not a member in this class;
    Output Dlg;
    Dlg.DoModal();
    UpdateData(FALSE);
}

I expect to print the string values in an output dialog box. If someone could provide me with a correct code or point me to a proper tutorial I would be thankful.

You mentioned that the members of CPtr are doubles. Assuming the m_d_?vmid? member variables are declared as CString , you could use:

void Output::AssignDlgToPtr1()
{
    m_d_hvmid1.Format( _T("%f"), Cptr.HV_Tmid_1 );
    m_d_hvmid2.Format( _T("%f"), Cptr.HV_Tmid_2 );
    m_d_hvmid3.Format( _T("%f"), Cptr.HV_Tmid_3 );
    m_d_hvmid4.Format( _T("%f"), Cptr.HV_Tmid_4 );
    m_d_lvmid1.Format( _T("%f"), Cptr.LV_Tmid_1 );
    m_d_lvmid2.Format( _T("%f"), Cptr.LV_Tmid_2 );
    m_d_lvmid3.Format( _T("%f"), Cptr.LV_Tmid_3 );
    m_d_lvmid4.Format( _T("%f"), Cptr.LV_Tmid_4 );
}

UPDATE after more code was added to the question: AddignDlgToPtr1() is never called. The Dialog box don't know on it's own when to call it. Best way is probably to overwrite OnInitDialog :

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

   AssignDlgToPtr1();

   UpdateData(FALSE);

   return TRUE;  // return TRUE unless you set the focus to a control
   // EXCEPTION: OCX Property Pages should return FALSE
}

Alternatively, you could also overwrite the UpdateData(...) method, but as you only want to show the results and looks like you don't care for edits, the above way is easiest.

Further, "UpdateData(FALSE)" is executed in the caller code after the dialog is closed... That line of code is not required.

Another option is to change the calling code to:

void CThermalToolDlg::OnCalculate()
{
    UpdateData();
    if (!Validate()) return;
    AssignDlgToPtr();
    Cptr.OnCalculate();
    // need to add something here to run Output Dialog class before displaying output box
    // AssignDlgToPtr1() is not a member in this class;
    Output Dlg;
    Dlg.AssignDlgToPtr1();
    Dlg.DoModal();
    // UpdateData(FALSE);
}

... assuming AddignDlgToPtr1() is public...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM