在VC編程中,經常要用到字符串的轉換,可以說,字符串轉換對於VC新手甚至是老手也都是一個比較頭疼的問題。在多字節環境下下也就是用ASCII環境下,Cstring轉char*可以使用如下代碼:
CString strXcoord;
GetDlgItemText(IDC_EDIT_XCOORD,strXcoord);
CString strYcoord;
GetDlgItemText(IDC_EDIT_YCOORD,strYcoord);
if (strXcoord.IsEmpty() || strYcoord.IsEmpty())
{
AfxMessageBox(TEXT("必須指定X和Y坐標"));
}
USES_CONVERSION;
char* col = T2A(strXcoord.GetBuffer(0));
char* row = T2A(strYcoord.GetBuffer(0));
CString strXcoord;
GetDlgItemText(IDC_EDIT_XCOORD,strXcoord);
CString strYcoord;
GetDlgItemText(IDC_EDIT_YCOORD,strYcoord);
if (strXcoord.IsEmpty() || strYcoord.IsEmpty())
{
AfxMessageBox(TEXT("必須指定X和Y坐標"));
}
USES_CONVERSION;
char* col = T2A(strXcoord.GetBuffer(0));
char* row = T2A(strYcoord.GetBuffer(0));
哦,還要strXcoord.ReleaseBuffer();,防止內存洩露。