一、类型说明
1、CString 是一个完全独立的类,类中使用动态的TCHAR数组,且封装了"+"等操作符和字符操作方法。
2、char* 是指向ANSI字符数组的指针与传统的C、C++兼容。
3、LPSTR是指向'/0'结尾的ANSI字符数组的指针,LP的含义是长指针(Long Pointer),在32位机上与P(矩指针)没有区别,与char* 能互换使用。
4、LPCSTR中增加的'C',其含义为Constant(常量),表明这种类型的实例为能被使用函数改变,除此之外同LPSTR。
5、LPWSTR、LPCWSTR,其含义类似于LPSTR与LPCSTR,只是其字符数据为wchar_t而不是char。
6、TCHAR的定义:
#ifdef _UNICODE
typedef wchar_t TCHAR;
#else
typedef char TCHAR;
#endif
7、LPTSTR与LPCTSTR的含义就是每个字符是TCHAR类型。
#ifdef _UNICODE
typedef const wchar_t* LPCTSTR;
#else
typedef const char* LPCTSTR;
#endif
8、CString类中的字符被声明为THCAR,可以根据不同的环境来适应,无须调整。
二、类型之间的转换使用(从其它包含字符串的变量中获取指向该字符串的指针)
1、CString与LPSTR、LPCTSTR之间转换
CString str = "This is Jarry's Object!";
//char* Buf = (LPSTR)str;(出错)
//char* Buf = (LPCTSTR)str;
//char*Buf = (LPSTR)(LPCTSTR)str;
三、WPARAM、LPARAM类型的使用(32位)
LPARAM lParma;
WORD loValue = LOWWORD(lParam); //取低16位
WORD hiValue = HIWORD(lParam); //取高16位
WORD wValue; //16位
BYTE loValue = LOBYTE(wValue); //取低8位
BYTE hiValue = HIBYTE(wValue); //取高8位
四、将CString类型的变量赋给char*类型的变量
1、CString::GetBuffer()函数
char* p;
CString str = "This is Jarry's Object";
p = str.GetBuffer(str.GetLength());
str.ReleaseBuffer();
2、memcpy()函数
CString mCS = _T("This is Jarry's Object!");
char mch[30];
memcpy(mch, mCS, 30);
3、用LPCTSTR强制转换,尽量不用
char* ch;
CString str("This is Jarry's Object!");
sprintf(ch, "%s", (LPSTR)(LPCTSTR)str);
4、CString类型转换LPCTSTR(const char*)
char chstr[100];
CString str("This is Jarry's Object!");
strncpy(chstr,(LPCTSTR)str, sizeof(chstr));
或者
strncpy(chstr, str, sizeof(chstr));
CString str("This is Jarry's Object!");
const char* lpctStr = (LPCTSTR)cStr;
5、LPCTSTR转换CString
LPCTSTR lpctStr;
CString cStr = lpctStr;

763

被折叠的 条评论
为什么被折叠?



