首页 > 其他 > 详细

如何解决VS2012中listbox不能添加水平滚动条的问题

时间:2014-03-23 14:51:18      阅读:594      评论:0      收藏:0      [点我收藏+]

这几天在做一个基于对话框的小程序,结果在listbox中显示,但是数据宽度较大不能完全显示,只有垂直滚动条,设置Horizontial Scoll还是不能显示。后来查了一下MSDN,才发现必须要设置SetHorizontalExtent属性。查了好多资料终于找到了解决方法。

bubuko.com,布布扣
void CretrieveDlg::displayScroll(CListBox &listbox)
{
    
    CString str;
    CSize sz;
    int dx = 0;
    TEXTMETRIC tm;
    CDC* pDC = listbox.GetDC();
    CFont* pFont =listbox.GetFont();

    // Select the listbox font, save the old font
    CFont* pOldFont = pDC->SelectObject(pFont);
    //Get the text metrics for avg char width
    pDC->GetTextMetrics(&tm);

    for (int i=0;i<listbox.GetCount();i++)
    {
        listbox.GetText(i,str);
        sz = pDC->GetTextExtent(str);

        //Add the avg width to prevent clipping
        sz.cx+=tm.tmAveCharWidth;

        if (sz.cx>dx)
        dx=sz.cx;
    }

    //Select the old font back into the DC
    pDC->SelectObject(pOldFont);
    listbox.ReleaseDC(pDC);

    // Set the horizontal extent so every character of all strings 
    // can be scrolled to.
    listbox.SetHorizontalExtent(dx); 
}
bubuko.com,布布扣

直接用      displayScroll(m_listLike);调用即可。如果该函数不采用引用的话,就出现这样的错误

>g:\program files (x86)\microsoft visual studio 11.0\vc\atlmfc\include\afxwin.h(1977): error C2248: “CObject::CObject”: 无法访问 private 成员(在“CObject”类中声明)
1>          g:\program files (x86)\microsoft visual studio 11.0\vc\atlmfc\include\afx.h(560) : 参见“CObject::CObject”的声明
1>          g:\program files (x86)\microsoft visual studio 11.0\vc\atlmfc\include\afx.h(535) : 参见“CObject”的声明
1>          此诊断出现在编译器生成的函数“CCmdTarget::CCmdTarget(const CCmdTarget &)”中

后来才发现是

因为CObject定义的时候是:
private:
CObject(const CObject& objectSrc);              // no implementation
void operator=(const CObject& objectSrc);       // no implementation
把拷贝构造函数和赋值运算符定位为private,就是不允许拷贝和赋值。所以就要用引用了

终于成功显示了水平滚动条

如何解决VS2012中listbox不能添加水平滚动条的问题,布布扣,bubuko.com

如何解决VS2012中listbox不能添加水平滚动条的问题

原文:http://www.cnblogs.com/hueiweijiang/p/3618761.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!