首先是設置listbox控件的屬性 Horizontal Scroll設為TRUE;

然后添加函數到CUighurRecognitionDlg.cpp(在CUighurRecognitionDlg.h中聲明函數)
void CUighurRecognitionDlg::SetHScroll() { CDC* dc = GetDC(); SIZE s; int index; CString str; long temp; for (index = 0; index< m_list.GetCount(); index++) { m_list.GetText(index, str); s = dc->GetTextExtent(str, str.GetLength() + 1); // 獲取字符串的像素大小 // 如果新的字符串寬度大於先前的水平滾動條寬度,則重新設置滾動條寬度 // IDC_LISTBOX為m_List的資源ID temp = (long)SendDlgItemMessage(IDC_LIST1, LB_GETHORIZONTALEXTENT, 0, 0); //temp得到滾動條的寬度 if (s.cx > temp) { SendDlgItemMessage(IDC_LIST1, LB_SETHORIZONTALEXTENT, (WPARAM)s.cx, 0); } } ReleaseDC(dc); }
m_list為listbox的關聯變量;IDC_LIST1為listbox的ID
添加關聯變量的方法:右擊listbox控件->類向導->成員變量->添加變量->確定

直接調用此函數就行
