首页 > 代码库 > MFC之控件和Cstring类型转换篇
MFC之控件和Cstring类型转换篇
1.打开文件
CFileDialog dlg(TRUE,NULL,NULL,OFN_ALLOWMULTISELECT,_T("All File |*.*|Jpeg File(*.jpg;*.jpeg;*.jpe)|*.jpg;*.jpeg;*.jpe|Windows(*.bmp)|*.bmp|CompuServe GIF(*.gif)|*.gif|Png文件(*.png)|*.png||"),this); dlg.m_ofn.lpstrTitle =_T("Open"); if(dlg.DoModal() == IDOK) { m_FileStr =dlg.GetFileName();//文件名 strFilePath = dlg.GetPathName();//路径 }
2.控件显示
Static Text
Edit Control
SetDlgItemText(IDC_Name,m_FileStr); //m_filestr为CString
listBox
CListBox* pListBox; pListBox = (CListBox*) GetDlgItem(IDC_ListShow); pListBox->AddString(str); pListBox->DeleteString(index);//删除指定index pListBox->ResetContent();//清空
从控件中获取数据
Static Text
Edit ControlCEdit* pBoxOne; CString str; pBoxOne = (CEdit*) GetDlgItem(IDC_Opacity); pBoxOne-> GetWindowText(str);
ListBox
CListBox* pListBox; pListBox = (CListBox*) GetDlgItem(IDC_ListShow); int index = pListBox->GetCurSel(); if (index>=0) { pListBox->GetText(index,str); }
3.CString的转换
UTF8下
Cstring To Int
_ttoi(str)
Int to CString
int num = 100;str.Format(_T("%d"),num);
Char To Cstring
TCHAR MuName[100]; char tchar[100]; MultiByteToWideChar(CP_ACP, 0,tchar , -1, MuName, 100); str.Format(_T("%s"),MuName);
Cstring To Char
void CStringToChar(CString str,char* dst = NULL){ char* src; int len = WideCharToMultiByte( CP_UTF8 , 0 , str , str.GetLength() , NULL , 0 , NULL , NULL ); src = http://www.mamicode.com/(char*)malloc(len+1*sizeof(char*));>还用一些其他函数
Char To Wchar_t
void CharToWChar_t(char* src,wchar_t* dst){ DWORD dwNum = MultiByteToWideChar(CP_ACP,0,src,-1,NULL,0); int nlen = MultiByteToWideChar (CP_ACP, 0, src, -1, dst, dwNum+10); dst[nlen] = 0; }十六进制转十进制
int CharHex16ToInt(char* str) { int len = strlen(str); int k = 1,sum = 0; if (len>2&&str[0]=='0'&&str[1]=='x') { for (int i = len-1; i>=2; i--,k*=16) { int tmp = 0; if (str[i]>='0'&&str[i]<='9') { tmp = str[i]-'0'; }else if (str[i]>='A'&&str[i]<='F') { tmp = str[i]-'A'+10; }else if (str[i]>='a'&&str[i]<='f') { tmp = str[i]-'a'+10; } sum += tmp*k; } } else { for (int i = len-1; i>=0; i--,k*=10) { int tmp = 0; if (str[i]>='0'&&str[i]<='9') { tmp = str[i]-'0'; } sum += tmp*k; } } return sum; }MFC之控件和Cstring类型转换篇
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。