关闭

ReleaseBuffer只有一个作用,就是更新字符串的长度

518人阅读 评论(0) 收藏 举报
void CStringBufferDlg::OnButton1() 
{
	// TODO: Add your control notification handler code here


CString WeatherZone = "";
int strLen = this->m_WeatherZone.GetLength();
memcpy(WeatherZone.GetBuffer(strLen),this->m_WeatherZone.GetBuffer(strLen),strLen);
WeatherZone.ReleaseBuffer();

CString  WeatherInfo = "";
strLen = this->m_szWeatherInfo.GetLength();
memcpy(WeatherInfo.GetBuffer(strLen),this->m_szWeatherInfo.GetBuffer(strLen),strLen);
WeatherInfo.ReleaseBuffer();

CString  Weatherglass= "";
strLen = this->m_szWeatherglass.GetLength();
memcpy(Weatherglass.GetBuffer(strLen),this->m_szWeatherglass.GetBuffer(strLen),strLen);
Weatherglass.ReleaseBuffer();

 CString weather = WeatherZone +" " +WeatherInfo +" "+Weatherglass;	

 SetDlgItemText(IDC_STATIC1,weather);
 // 为啥得带的这个weather 是"  "呢?而不是 "北京多云转晴 23~35度"呢?
 /*
 很明显ReleaseBuffer只有一个作用,就是更新字符串的长度。
 CString内,GetLength获取字符串长度并不是动态计算的,
 而是在赋值操作后计算并保存在一个int变量内的,
 当通过GetBuffer直接修改CString时,那个int变量并不可能自动更新,
 于是便有了ReleaseBuffer。	 
    其实,计算长度还能用strlen(),这个就算不ReleaseBuffer也不会出错,
	但如果不ReleaseBuffer,在+=这种赋值时字符串很可能会跟想要得到的不同。 
 */
}

void CStringBufferDlg::OnButton2() 
{
	// TODO: Add your control notification handler code here
	CString s="hello";	
    LPTSTR ps=s.GetBuffer(10);
	MessageBox(ps);
    strcpy(ps,"hi");
	MessageBox(ps);	
	MessageBox(s);	
	int len=s.GetLength();
	char buff[256];
	MessageBox(itoa(len,buff,10));
    s.ReleaseBuffer();	
	len=s.GetLength();
	MessageBox(itoa(len,buff,10));
}

void CStringBufferDlg::OnButton3() 
{
	// TODO: Add your control notification handler code here
	CString WeatherZone = "";
	int strLen = this->m_WeatherZone.GetLength();
	memcpy(WeatherZone.GetBuffer(strLen),this->m_WeatherZone.GetBuffer(strLen),strLen);
	//WeatherZone.ReleaseBuffer();
	
	CString  WeatherInfo = "";
	strLen = this->m_szWeatherInfo.GetLength();
	memcpy(WeatherInfo.GetBuffer(strLen),this->m_szWeatherInfo.GetBuffer(strLen),strLen);
	//WeatherInfo.ReleaseBuffer();
	
	CString  Weatherglass= "";
	strLen = this->m_szWeatherglass.GetLength();
	memcpy(Weatherglass.GetBuffer(strLen),this->m_szWeatherglass.GetBuffer(strLen),strLen);
	//Weatherglass.ReleaseBuffer();
	
	char buff[256];
	wsprintf(buff,"%s %s %s",WeatherZone.GetBuffer(0),WeatherInfo.GetBuffer(0),Weatherglass.GetBuffer(0));
	//CString weather = WeatherZone +" " +WeatherInfo +" "+Weatherglass;	
	
 SetDlgItemText(IDC_STATIC1,buff);	
}

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:3093158次
    • 积分:41267
    • 等级:
    • 排名:第91名
    • 原创:399篇
    • 转载:4555篇
    • 译文:5篇
    • 评论:611条
    文章分类
    最新评论