在Console工程中引用CString

来源:互联网 发布:纸箱设计软件 编辑:程序博客网 时间:2024/06/06 17:18

CString 是封装的非常不错的一个类,相比于STL的string,它提供了更加丰富的成员方法。虽然在控制台工程中,string基本已经能够完成需要的功能,但是,如果能够偷懒一下的话 ...

在Console工程中引用CString方法如下:
    1. 工程设置: project/setting/General/中,
        把not using mfc改为usimg mfc in a shared dll 或者是 use mfc in a static library,均可
    2. 工程设置: project/setting/[C/C++]/中
       Code Genaration中,选择 多线程库
    3. 添加头文件:
       #include <afx.h>
       //#include <windows.h>   //如果有 windows.h, 确保 afx.h 在windows.h前面被包含

代码示例:

#include <iostream>
#include 
<afx.h>
using namespace std;

//检查CString str 是否完全由数字字符组成
int main()
...{
    CString temp, str
=_T("12345");
    LPTSTR t 
= str.GetBuffer(255);
    temp.Format(
"%ld", _tcstol(str.GetBuffer(255), NULL, 10) );
    
    
if (temp.GetLength() == str.GetLength())
        cout
<<"ALL digital numbers!"<<endl;

    
return 0;
}

 

PS:dll或者lib的工程,由于afx.h默认带了一个DllMain,致使要使用CString类需要几个步骤。
         具体的步骤请参见:在非MFC程序中引用CString

0 0