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 Control

CEdit* 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 = (char*)malloc(len+1*sizeof(char*));
	len = WideCharToMultiByte(  CP_UTF8 , 0 , str , str.GetLength() , src , len +1 , NULL ,NULL );
	
	src[len] = 0;
	if (dst!=NULL)
	{
		strcpy(dst,src);
	}
	free(src);
}


还用一些其他函数


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;
}


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值