简体   繁体   English

Visual Studio MFC Cstring格式

[英]Visual Studio MFC Cstring format

I am developing an MFC Interface with Visual Studio, but the output is not as it should. 我正在使用Visual Studio开发MFC接口,但输出不是应该的。 I am using the same code as the on used in codeblocks but the output here is different and i think it's because of the format. 我使用与代码块中使用的相同的代码,但这里的输出是不同的,我认为这是因为格式。 What is the correct way to enter 'e' and 'd' in my 'IDC_Values' ? 在'IDC_Values'中输入'e'和'd'的正确方法是什么? I searched online but couldn't find much about MFC 我在网上搜索但是找不到很多关于MFC的信息

int e[100], d[100];

CString Text;
Text.Format((LPCWSTR)L"%d \t%d", e, d);
SetDlgItemText(IDC_Values, Text);

CString is CStringW on UNICODE builds and CStringA on non UNICODE builds. CString是UNICODE构建的CStringW和非UNICODE构建的CStringA。 So you should not mix wide literals with non wide, for example you have: 所以你不应该将宽文字与非宽文字混合,例如你有:

Text.Format((LPCWSTR)L"%d \t%d", e, d);
                     ^ ~~~~ this requires that CString is wide

this should be (minus the fact that e and d are arrays!!): 这应该是(减去e和d是数组的事实!!):

Text.Format(_T("%d \t%d"), e, d);

Now if you want to format a string, and set it to widget, then you must iterate it (warning: I have not compiled this code): 现在,如果要格式化字符串并将其设置为窗口小部件,则必须对其进行迭代(警告:我还没有编译此代码):

CString Text;
CString tmp;
for (size_t i = 0; i < sizeof(e)/sizeof(e[0]); ++i) {
  tmp.Format(_T("%d,"), e[i]);
  Text += tmp;
}

// here the same for d

I am not saying its the most efficent way. 我并不是说它最有效的方式。

If you want to print the content of an array you must iterate the array and build the string I guess you want a list having rows and columns containg e[] and d[]? 如果你想打印一个数组的内容你必须迭代数组并构建字符串我想你想要一个包含行和列的列表包含e []和d []? I assume that e. 我假设e。 d are filled completely. d完全填满。 If so you need to code like 如果是这样,你需要编码

int e[100], d[100];
CString Text;
CString Line
for(int i=0;i<100;i++){
    Line.Format((LPCWSTR)L"%d \t%d\r\n", e, d);
    Text+=Line;
}
SetDlgItemText(IDC_Values, Text);

This will generate a multiline string containing the values of e and d. 这将生成包含e和d值的多行字符串。 If this is what you want. 如果这是你想要的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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