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.txtdirectory+=_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 ; //漢字}elsenCount++; //字母數字等}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();