为ListBox关联控件变量m_ListBox
for (int i=0; i<=20; ++i) { m_List.AddString(L"123456789123456789123456789123456789123456789"); } |
当ListBox控件加入的字符串宽度过长时,新加入的有些字符串就不能显示,此时就需要加入水平滚动条
先将ListBox的Horizontal Scroll勾选上
在对话框CTestDlg类中加入SetHScroll成员函数
void CTestDlg::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); } |