ReleaseBuffer只有一个作用,就是更新字符串的长度
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);	
}

阅读更多
个人分类: c++MFC学习笔记
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

不良信息举报

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

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭