工控编程吧

标题: 上位机MFC如何设置组合框列表宽度 [打印本页]

作者: qq263946146    时间: 2019-7-27 18:45
标题: 上位机MFC如何设置组合框列表宽度
我们知道组合框在下拉时,内容可能不能完全显示出来,
这时我们就要根据文本宽度,设置组合 框下拉列表的宽度。
我们可以通过初始化时调用下面代码来运行程序查看效果。
主要用到的函数为SetDroppedWidth。
  1.        
  2.         //向组合框中列表框添加文本
  3.         for (int n = 0 ; n < 10; n++)
  4.         {
  5.                 m_ctrlComboBox.AddString(_T("Hello World!"));
  6.         }

  7.         int nWidth = 0;

  8.         CDC* pDC = m_ctrlComboBox.GetDC();

  9.         for (n = 0; n < m_ctrlComboBox.GetCount(); n++)
  10.         {
  11.                 CString strText = _T("");
  12.                 int nTextWidth = 0;

  13.                 //获得组合框中列表框文本
  14.                 m_ctrlComboBox.GetLBText(n, strText);

  15.                 //获得组合框中列表框文本宽度
  16.                 nTextWidth = pDC->GetTextExtent(strText).cx;

  17.                 nWidth = max(nWidth, nTextWidth);
  18.         }

  19.         m_ctrlComboBox.ReleaseDC(pDC);

  20.         //加滚动条宽度和左右边界宽度
  21.         nWidth += ::GetSystemMetrics(SM_CXVSCROLL)
  22.                 + ::GetSystemMetrics(SM_CXEDGE) * 2;

  23.         //设置组合框中列表框宽度
  24.         m_ctrlComboBox.SetDroppedWidth(nWidth);
复制代码
(, 下载次数: 1)