0
votes

I have two variables, m_GridSize and m_TimeDisplay, which update themselves according to a variable called "world" here. Now, the MFC program will display the words "Grid size: " and "Time: ", but it will not display the actual values of the doubles. I am using Visual Studio Community 2013 to make a Win32 GUI application.

I am having trouble with the CString Format function.

EDIT to include full code:

// smart_parking_guiDlg.cpp : implementation file
//

#include "stdafx.h"
#include "smart_parking_gui.h"
#include "smart_parking_guiDlg.h"
#include "afxdialogex.h"
#include "Cadd_Destination.h"
#include "Cadd_Lot.h"
#include "Cadd_Driver.h"
#include "Commands.h" // Used to handle commands
#include "Grid.h" // Contains the grid
#include <string>
#include <io.h>
#include <fcntl.h>

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// Csmart_parking_guiDlg dialog



Csmart_parking_guiDlg::Csmart_parking_guiDlg(CWnd* pParent /*=NULL*/)
    : CDialogEx(Csmart_parking_guiDlg::IDD, pParent)
    , m_EchoSize(_T("Grid size: "))
    , m_EchoTime(_T("Time: "))
    , m_EchoStatus(_T("Open"))
{
    m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
        this->world = new Grid(10, 5); // default grid
}

void Csmart_parking_guiDlg::DoDataExchange(CDataExchange* pDX)
{
    CDialogEx::DoDataExchange(pDX);
    DDX_Text(pDX, IDC_ST_GRIDSIZE, m_EchoSize);
    DDX_Text(pDX, IDC_ST_TIME, m_EchoTime);
    DDX_Text(pDX, IDC_ST_STATUS, m_EchoStatus);
}

BEGIN_MESSAGE_MAP(Csmart_parking_guiDlg, CDialogEx)
    ON_WM_PAINT()
    ON_WM_QUERYDRAGICON()
    ON_BN_CLICKED(IDC_B_OPEN_CONFIG, &Csmart_parking_guiDlg::OnBnClickedBOpenConfig)
    ON_BN_CLICKED(IDC_B_SAVECONFIG, &Csmart_parking_guiDlg::OnBnClickedBSaveconfig)
    ON_BN_CLICKED(IDC_B_NEXTEVENT, &Csmart_parking_guiDlg::OnBnClickedBNextevent)
    ON_BN_CLICKED(IDC_B_NEWDEST, &Csmart_parking_guiDlg::OnBnClickedBNewdest)
    ON_BN_CLICKED(IDC_B_NEWLOT, &Csmart_parking_guiDlg::OnBnClickedBNewlot)
    ON_BN_CLICKED(IDC_B_NEWDRIVER, &Csmart_parking_guiDlg::OnBnClickedBNewdriver)
    ON_BN_CLICKED(IDC_B_SIMEND, &Csmart_parking_guiDlg::OnBnClickedBSimend)
    ON_BN_CLICKED(IDC_B_SHOWSTATUS, &Csmart_parking_guiDlg::OnBnClickedBShowstatus)
END_MESSAGE_MAP()


// Csmart_parking_guiDlg message handlers

BOOL Csmart_parking_guiDlg::OnInitDialog()
{
    CDialogEx::OnInitDialog();

    // Set the icon for this dialog.  The framework does this automatically
    //  when the application's main window is not a dialog
    SetIcon(m_hIcon, TRUE);         // Set big icon
    SetIcon(m_hIcon, FALSE);        // Set small icon

    // TODO: Add extra initialization here

    return TRUE;
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

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

        SendMessage(WM_ICONERASEBKGND, reinterpret_cast<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
    {
        CDialogEx::OnPaint();
    }
}

// The system calls this function to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR Csmart_parking_guiDlg::OnQueryDragIcon()
{
    return static_cast<HCURSOR>(m_hIcon);
}



void Csmart_parking_guiDlg::OnBnClickedBOpenConfig()
{
    wchar_t szFilters[] = _T("Text Files (*.txt)|*.txt|All Files (*.*)|*.*||");
    // Create an Open dialog
    CFileDialog fileDlg(TRUE, _T("txt"), _T("*.txt"),
        OFN_FILEMUSTEXIST | OFN_HIDEREADONLY, szFilters, this); // Display the file dialog. 

    // When user clicks OK, fileDlg.DoModal() returns IDOK.
    if (fileDlg.DoModal() == IDOK)
    {
        CString m_strPathname = fileDlg.GetPathName();
        CT2CA converter(m_strPathname);
        std::string fileToOpen(converter);
        // TODO: Open Grid file
        open_file(*world, fileToOpen);
        //Change the window's title to the opened file's title.
        CString fileName = fileDlg.GetFileTitle();

        SetWindowText(fileName);
    }
}


void Csmart_parking_guiDlg::OnBnClickedBSaveconfig()
{
    // TODO: Add your control notification handler code here
    // szFilters is a text string that includes two file name filters:
    // "*.my" for "MyType Files" and "*.*' for "All Files."
    TCHAR szFilters[] = _T("Text Files (*.txt)|*.txt|All Files (*.*)|*.*||");

    // Create a Save dialog
    CFileDialog fileDlg(FALSE, _T("txt"), _T("*.txt"),
        OFN_FILEMUSTEXIST | OFN_HIDEREADONLY, szFilters);

    // Display the file dialog. When user clicks OK, fileDlg.DoModal() 
    // returns IDOK.
    if (fileDlg.DoModal() == IDOK)
    {
        CString pathName = fileDlg.GetPathName();
        CT2CA converter(pathName);
        std::string fileToWrite(converter);
        // Implement opening and reading file in here.
        write_file(*world, fileToWrite);
        //Change the window's title to the opened file's title.
        CString fileName = fileDlg.GetFileTitle();

        SetWindowText(fileName);
    }
}


void Csmart_parking_guiDlg::OnBnClickedBNextevent()
{
    // TODO: Add your control notification handler code here
    run_simulation(*world);
    m_GridSize = world->getGridSize(); // double
    m_TimeDisplay = world->getTime(); // double
    // THIS DOESN'T WORK
    m_EchoSize.Format(_T("Grid size: %g"), m_GridSize);
    m_EchoTime.Format(_T("Time: %g"), m_TimeDisplay);
    UpdateData(FALSE);
    GetDlgItem(IDC_ST_GRIDSIZE)->InvalidateRect(NULL);
    GetDlgItem(IDC_ST_TIME)->InvalidateRect(NULL); 
}

void Csmart_parking_guiDlg::OnBnClickedBSimend() // On clicking, simulation jumps to the very end.
{
    jump_to_end(*world);
    m_GridSize = world->getGridSize();
    m_TimeDisplay = world->getTime();
    // THIS DOESN'T WORK
    m_EchoSize.Format(_T("Grid size: %g"), m_GridSize);
    m_EchoTime.Format(_T("Time: %g"), m_TimeDisplay);
    UpdateData(FALSE);
    GetDlgItem(IDC_ST_GRIDSIZE)->InvalidateRect(NULL);
    GetDlgItem(IDC_ST_TIME)->InvalidateRect(NULL);
}

void Csmart_parking_guiDlg::OnBnClickedBNewdest()
{
    // TODO: Add your control notification handler code here
    Cadd_Destination Dlg;
    Dlg.DoModal();
}


void Csmart_parking_guiDlg::OnBnClickedBNewlot()
{
    // TODO: Add your control notification handler code here
    Cadd_Lot Dlg;
    Dlg.DoModal();
}


void Csmart_parking_guiDlg::OnBnClickedBNewdriver() // Opens a dialog to input a new driver. Only works with added destination.
{
    if (world->getDestinationCount() != 0) {
        Cadd_Driver Dlg;
        Dlg.DoModal();
    }
}

void Csmart_parking_guiDlg::OnBnClickedBShowstatus()
{
    // TODO: Add your control notification handler code here

}

Is there any way to fix this in order to make the values of the doubles show up in the GUI? I have tried the answer shown here

C++ MFC double to CString

but the numbers do not show up at all. There are no syntax errors. The code works if I use %d and replace the values with integers, but it doesn't work with double values, which is what I used in my initial classes.

2
What kind of controls are m_EchoSize and m_EchoTime tied to? For that matter are you missing the DDX_ statements for those variables? I presume you've used the debugger to inspect the strings to see that they contain the correct value. - Mark Ransom
CDialogEx::DoDataExchange(pDX); DDX_Text(pDX, IDC_ST_GRIDSIZE, m_EchoSize); DDX_Text(pDX, IDC_ST_TIME, m_EchoTime); are the controls for it. Also, I don't know how to use the Visual Studio debugger. - more whirlpools
You really need to learn to use the debugger, it's indispensable! Easy too. Can I assume by the naming convention IDC_ST that these are static text controls? - Mark Ransom
Yes, these are static text controls. The idea was that they listed the size and the current time listed in the simulation. - more whirlpools
What data type are m_GridSize and m_TimeDisplay, and what data type do the respective get-methods return? A double as indicated in the comment seems like an extremely awkward choice. - IInspectable

2 Answers

1
votes

I have fixed the problem myself.

As it turns out, the issue had more to do with my GUI. It turns out that the Static Text has a set length set in the Visual Studio Dialog Editor (accessed through the Resource View) and the length was too short to hold both the string containing "Grid size:" and the actual numbers. (The first Static Text initially could only hold one digit for both of them) I fixed it by extending the width of the Static Text in my GUI, and that fixed the problem.

The size of these should be stretched to contain more digits

0
votes

As implied by the name, Static text controls are not expected to change once they are created. They don't automatically repaint when you change their content with SetWindowText, which is what DDX_Text calls to set the new text. You need to inform Windows that the contents have changed and the control needs repainting:

GetDlgItem(IDC_ST_GRIDSIZE)->InvalidateRect(NULL);