MFC CString:双不显示

Posted

技术标签:

【中文标题】MFC CString:双不显示【英文标题】:MFC CString: double not displaying 【发布时间】:2015-07-06 15:22:05 【问题描述】:

我有两个变量,m_GridSize 和 m_TimeDisplay,它们根据此处称为“世界”的变量进行自我更新。现在,MFC 程序将显示单词“Grid size:”和“Time:”,但它不会显示双精度数的实际值。我正在使用 Visual Studio Community 2013 制作一个 Win32 GUI 应用程序。

我在使用 CString Format 函数时遇到问题。

编辑以包含完整代码:

// 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


有什么办法可以解决这个问题,以使双打的值显示在 GUI 中?我已经尝试过这里显示的答案

C++ MFC double to CString

但数字根本不显示。没有语法错误。如果我使用 %d 并将值替换为整数,则该代码有效,但它不适用于双值,这是我在初始类中使用的。

【问题讨论】:

m_EchoSizem_EchoTime 与哪些控件相关联?就此而言,您是否缺少这些变量的 DDX_ 语句?我假设您已经使用调试器检查字符串以查看它们是否包含正确的值。 CDialogEx::DoDataExchange(pDX); DDX_Text(pDX, IDC_ST_GRIDSIZE, m_EchoSize); DDX_Text(pDX, IDC_ST_TIME, m_EchoTime);是它的控件。另外,我不知道如何使用 Visual Studio 调试器。 你真的需要学会使用调试器,缺一不可!也很容易。我可以按照命名约定IDC_ST 假设这些是静态文本控件吗? 是的,这些是静态文本控件。他们的想法是他们列出了模拟中列出的大小和当前时间。 m_GridSizem_TimeDisplay 是什么数据类型,get 方法各自返回什么数据类型?评论中指出的double 似乎是一个非常尴尬的选择。 【参考方案1】:

我自己解决了这个问题。

事实证明,这个问题更多地与我的 GUI 有关。事实证明,静态文本在 Visual Studio 对话框编辑器中设置了一个长度(通过资源视图访问),长度太短,无法容纳包含“网格大小:”的字符串和实际数字。 (第一个静态文本最初只能为它们两个保留一个数字)我通过在我的 GUI 中扩展静态文本的宽度来修复它,从而解决了问题。

【讨论】:

我也应该想到这个,这是一个典型的错误。附:如果您删除我建议的InvalidateRect 代码,它仍然有效吗?【参考方案2】:

正如名称所暗示的那样,Static 文本控件一旦创建就不会发生变化。当您使用SetWindowText 更改其内容时,它们不会自动重新绘制,这是DDX_Text 调用设置新文本的方法。您需要通知 Windows 内容已更改,控件需要重新绘制:

GetDlgItem(IDC_ST_GRIDSIZE)->InvalidateRect(NULL);

【讨论】:

我刚试过这个,但它不起作用。我最初的代码适用于整数。但是,它不适用于双精度值,这是我试图添加到我的程序中的。 @morewhirlpools 那么你没有分享足够的代码来让这个问题变得有意义,对不起。 @morewhirlpools 你把那行放在UpdateData调用之后,对吧? 我没有,但即使我这样做了,代码也不起作用。我将使用完整的对话框代码编辑该文件。 @morewhirlpools 谢谢。试试GetDlgItem(IDC_ST_GRIDSIZE)-&gt;RedrawWindow() 而不是InvalidateRect 看看是否有帮助。

以上是关于MFC CString:双不显示的主要内容,如果未能解决你的问题,请参考以下文章

MFC char*如何转换为CString,并显示在edit box中

关于MFC中CString的用法

关于MFC中CString的用法

MFC中ListBox如何实时显示要输入的数据?

MFC中的CString类使用方法指南

CString 在 MFC 中编辑控件