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);
}
ReleaseBuffer只有一个作用,就是更新字符串的长度
最新推荐文章于 2025-04-11 16:55:25 发布