首页 > 代码库 > CString用法总结?.xml
CString用法总结?.xml
<style
type="text/css">
pre{
line-height:1;
color:#1e1e1e;
background-color:#e9e9ff;
font-size:16px;}.sysFunc{color:#627cf6;font-style:italic;font-weight:bold;}
.selfFuc{color:#800080;}
.bool{color:#d2576f;}
.condition{color:#000080;font-weight:bold;}
.key{color:#000080;}
.var{color:#800000;font-style:italic;}
.Digit{color:#ff00ff;font-weight:bold;}
.includePre{color:#1e1e1e;}
.operator?{color:#008000;font-weight:bold;}
</style>
1?CString中?保存着?路径+文件名?如:?c:\目录\file.txt
获得目录和文件名的方法:
????CString?path=_T("c:\\目录\\file.txt");?? ?????? ????CString?directory;?? ????CString?fileName;?? ?????? ????int?i=path.ReverseFind(_T(‘\\‘));?? ?????? ????directory=path.Left(i+1);????????//??directory?此时为目录?c:\目录\?? ?????? ????fileName=path.Right(path.GetLength()-i-1);??//?fileName??为文件名??file.txt?? ? ?在现有目录下?新增一个子目录 ? ?????????????????现有目录?c:\目录?????????????????想生成的目录??c:\目录\目录?
程序如下:
注意添加头文件???#include?"Shlwapi.h"
????CString?path=_T("c:\\目录\\file.txt");?? ?????? ????CString?directory;?? ????CString?fileName;?? ?????? ????int?i=path.ReverseFind(_T(‘\\‘));?? ?????? ????directory=path.Left(i+1);????????//??directory?此时为目录?c:\目录\?? ?????? ????fileName=path.Right(path.GetLength()-i-1);??//?fileName??为文件名??file.txt?? ????directory+=_T("目录\\");?? ?????? ????if?(!PathIsDirectory(directory))?? ????{?? ????????if?(!CreateDirectory(directory,NULL))?? ????????{?? ????????????CString?errorInfo;?? ????????????errorInfo.Format(L"无法创建目录?%s?",directory);?? ????????????AfxMessageBox(errorInfo);?? ????????????return?;?? ????????}?? ????}?? ???????//?directory?此时为c:\目录\目录\?? ?
2??创建文件夹路径
? ????CString?保存着一文件名??c:\d\1.txt??或?一文件夹名:?c:\d ????由这些信息?创建路径??c:\d ?
????CreatePathFolder(CString?path)?? ????{?? ????????int?nFind?=?-1,?nStart?=?0;?? ????????CString?str;?? ????????nFind?=?path.Find(L‘\\‘,nStart);?? ????????while?(nFind>=0)?? ????????{?? ????????????str?=?path.Left(nFind);?? ????????????if(!PathFileExists(str))?? ????????????????CreateDirectory(str,NULL);?? ????????????nStart?=?nFind?+?1;?? ????????????nFind?=?path.Find(L‘\\‘,nStart);?? ????????}?? ????}?? ? ?
3?Mid??获得字串
? Extracts?a?substring?of?length?nCount?characters?from?thisCStringT?object,?starting?at?positioniFirst?(zero-based). ? CStringT?Mid( ???int?iFirst, ???int?nCount )?const; CStringT?Mid( ???int?iFirst )?const; Parameters iFirst ????The?zero-based?index?of?the?first?character?in?this?CStringT?object?that?is?to?be?included?in?the?extracted?substring. nCount ????The?number?of?characters?to?extract?from?this?CStringT?object.?If?this?parameter?is?not?supplied,?then?the?remainder?of?the?string?is?extracted. return A?CStringT?object?that?contains?a?copy?of?the?specified?range?of?characters.?Note?that?the?returnedCStringT?object?may?be?empty. remark The?function?returns?a?copy?of?the?extracted?substring.?Mid?is?similar?to?the?Basic?Mid?function?(except?that?indexes?in?Basic?are?one-based). For?multibyte?character?sets?(MBCS),?nCount?refers?to?each?8-bit?character;?that?is,?a?lead?and?trail?byte?in?one?multibyte?character?are?counted?as?two?characters. MSDN示例: ?
????CAtlString?s(?_T("abcdef")?);?? ????_ASSERT(?s.Mid(?2,?3?)?==?_T("cde")?);?? ?
自己的例子:
VECTOR容器中保存着删除线,?mixString为源字符串,?trueString为删除了删除线以后的字符串 ?
????trueString=_T("");?? ????CString?mixString;?? ????GetRichEditCtrl().GetWindowText(mixString);?? ?????? ????int?nStart=0;?? ????for?(iter1=erasePosition.begin();iter1!=erasePosition.end();iter1++)?? ????{?? ????????trueString+=mixString.Mid(nStart,iter1->nStart-nStart);?? ????????nStart=iter1->nEnd;?? ????}?? ?????? ????trueString+=mixString.Mid(nStart);?? ? ?
4??CString?转换成?LPTSTR?(char?*??or?wchar?*)
????(LPTSTR)(LPCTSTR)??
如:
????CString?leftMargin,rightMargin,topMargin,bottomMargin,middleMargin;?? ????CString?pathSet;?? ????pathSet=m_exePath+L"UnionPicSet.ini";?? ?????? ????if?(PathFileExists(pathSet))?? ????{?? ?????????? ????????GetPrivateProfileString(_T("Margin"),_T("LeftMargin"),NULL,(LPTSTR)(LPCTSTR)leftMargin,MAX_PATH,pathSet);?? ???????????m_LeftMargin=_ttoi(leftMargin);?? ????????GetPrivateProfileString(_T("Margin"),_T("RightMargin"),NULL,(LPTSTR)(LPCTSTR)rightMargin,MAX_PATH,pathSet);?? ????????m_RightMargin=_ttoi(rightMargin);?? ????????GetPrivateProfileString(_T("Margin"),_T("TopMargin"),NULL,(LPTSTR)(LPCTSTR)topMargin,MAX_PATH,pathSet);?? ????????m_TopMargin=_ttoi(topMargin);?? ????????GetPrivateProfileString(_T("Margin"),_T("BottomMargin"),NULL,(LPTSTR)(LPCTSTR)bottomMargin,MAX_PATH,pathSet);?? ????????m_BottomMargin=_ttoi(bottomMargin);?? ????????GetPrivateProfileString(_T("Margin"),_T("MiddleMargin"),NULL,(LPTSTR)(LPCTSTR)middleMargin,MAX_PATH,pathSet);?? ????????m_MiddleMargin=_ttoi(middleMargin);?? ????????GetPrivateProfileString(_T("Prefix"),_T("PagePrefix"),NULL,(LPTSTR)(LPCTSTR)m_PagePrefix,MAX_PATH,pathSet);?? ????}?? ?注:?CString?经?(LPTSTR)(LPCTSTR)?转换成指针类型后,CSting?对象内部结构遭到破坏?,此时,调用??CString::GetLength()?会显示0,?调用CString::IsEmpty()也会显示空,?但实际上,其内部还保存着数据,可对数据进行运算。 ?
????//?不能使用下面方式赋值?? ?????? ???????/*???? ????if?(!rightMargin.IsEmpty())??//?始终显示为空,即使rightMargin保存有数据?也显示为空? ????????{? ????????????m_RightMargin=_ttoi(rightMargin);? ????????}? ????*/?? ? ??
5?求取CString中字的个数
????CString?str=L"你好";?? ????int?num=0;?? ????wchar_t?ch;?? ????for?(int?i=0;i<str.GetLength();i++)?? ????{?? ????????ch=str.GetAt(i);?? ????????if?(ch>=0XD800&&ch<=0XDBFF)?? ????????{?? ????????????i++;?//??BMP之外的字?? ????????}?? ?????? ????????num++;?? ????}??
6?判断CString中是否有UNICODE>0X30000的字
????//自定义的异体字UNICODE编码?>0x30000?? ????//判断字符串中是否包含异体字?? ????BOOL?bExsitYitizi(CString?str)????? ????{?? ????????int?num=0;???? ????????WORD?w1,w2;???? ????????DWORD?w;?? ????????for?(int?i=0;i<str.GetLength();i++)???? ????????{???? ????????????w1=str.GetAt(i);???? ????????????if?(w1>=0XD800&&w1<=0XDBFF)???? ????????????{???? ????????????????i++;?//??BMP之外的字??占4个字节?? ?????? ????????????????w2=str.GetAt(i);?? ?????? ????????????????DeCode(w,w1,w2);?? ?????? ????????????????if?(w>0x30000)?? ????????????????{?? ????????????????????return?TRUE;?? ????????????????}?? ????????????}???? ?????? ????????????num++;???? ????????}???? ????????return?FALSE;?? ????}??
7?CString中?占位符个数?---汉字2个?字母等1个
????//求取占位符个数?? ????int?NumOfPosition(CString?str)?? ????{?? ????????int?nCount=0;?? ????????int?unicode=0;?? ????????for?(int?i?=?0?;?i?<str.GetLength();i++)?????? ????????{?? ????????????unicode?=?(int)str.GetAt(i);??? ?????? ????????????if?(unicode?>?255)?????? ????????????{?????? ????????????????nCount?+=2?;????//汉字?? ????????????}else?? ????????????????nCount++;???????//字母数字等?? ????????}?? ????????return?nCount;?? ????}??
在中间位置书写
????glPushMatrix();?? ????//姓名?? ????numPosition=NumOfPosition?(m_NameStr);?? ????glColor3f(1.0,1.0,0.0);?? ????glTranslatef(-numPosition/2.0*0.2,-0.1,-0.2);?? ????glScalef(0.4,0.4,0.4);?? ????Print3D(m_NameStr,hFont,0.2);?? ????glPopMatrix();??
本文使用?书画小说软件?发布,内容与软件无关,书画小说软件?更惬意的读、更舒心的写、更轻松的发布。
CString用法总结?.xml
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。