工控编程吧
标题: 上位机MFC如何设置组合框列表宽度 [打印本页]
作者: qq263946146    时间: 2019-7-27 18:45
标题: 上位机MFC如何设置组合框列表宽度
我们知道组合框在下拉时,内容可能不能完全显示出来,
这时我们就要根据文本宽度,设置组合 框下拉列表的宽度。
我们可以通过初始化时调用下面代码来运行程序查看效果。
主要用到的函数为SetDroppedWidth。
-         
 -         //向组合框中列表框添加文本
 -         for (int n = 0 ; n < 10; n++)
 -         {
 -                 m_ctrlComboBox.AddString(_T("Hello World!"));
 -         }
 
-         int nWidth = 0;
 
-         CDC* pDC = m_ctrlComboBox.GetDC();
 
-         for (n = 0; n < m_ctrlComboBox.GetCount(); n++)
 -         {
 -                 CString strText = _T("");
 -                 int nTextWidth = 0;
 
-                 //获得组合框中列表框文本
 -                 m_ctrlComboBox.GetLBText(n, strText);
 
-                 //获得组合框中列表框文本宽度
 -                 nTextWidth = pDC->GetTextExtent(strText).cx;
 
-                 nWidth = max(nWidth, nTextWidth);
 -         }
 
-         m_ctrlComboBox.ReleaseDC(pDC);
 
-         //加滚动条宽度和左右边界宽度
 -         nWidth += ::GetSystemMetrics(SM_CXVSCROLL)
 -                 + ::GetSystemMetrics(SM_CXEDGE) * 2;
 
-         //设置组合框中列表框宽度
 -         m_ctrlComboBox.SetDroppedWidth(nWidth);
 
 复制代码 
(, 下载次数: 1)