List Box ---如何添加水平滚动条

任务17:Box组件和List组件的组合使用实现排行榜 文章目录一、任务目标二、任务分解2.1 如何让Box容器在List下渲染多个2.2 List常用属性的介绍2.3 Box常用属性的介绍2.4 排行榜示例三、参考资料 一、任务目标 从继承关系上来说,List组件是继承自Box组件的,但是从使用上来说,List组件是作为Box组件的父容器使用的,当我们需要将多个同类组件组合起来的时候,比如游戏中的排行榜,Box和List就派上了用场。 每个玩家的基本信息有头像,排名,得分,昵称等,将显示这些信息的组件放入一个Box容器中,如果此时我们希望节省一些力气,那么复制 阅读详情

前段时间做了一个DLL,输出一个Dialog,里面的ListBox用到了水平滚动条.

VC6.0中作出来水平滚动条无法自动做,网上Google了一吧,www.codeguru.com中有偏文章很是好用.

转贴到这里:

http://www.codeguru.com/cpp/controls/listbox/article.php/c4759/

Overview

CListBox: This is a wrapper class for the ListBox control, and it's used in almost every application. There is a little "gotcha" to this class: The horizontal scroll bar doesn't work. Okay, I created it with the WS_HSCROLL flag set, the scroll bar is visible, and strings that I'm adding to the box are obviously longer then the box's horizontal extent, but I'm unable to scroll. What's wrong?

My theory is that the guy who originally implemented that control had way too much Greek mythology in his childhood. Villainous Procrustes and his bed where he had to cut off travelers' legs if they didn't fit in had a huge impact on the poor guy. If something is too long, chop it off. Well, whatever the reason is, the control simply ignores the fact that the strings are longer then it can show. How can we fix it?

Well, first, we have to catch all messages sent to the control that affect the box's content, hence the strings that it has (behind such functions as AddString() or ResetContent(), is SendMessage() with LB_ADDSTRING and LB_RESETCONTENT respectively). There are five messages of that type (at least that's the number I've come with. There may be more; I'm not sure.). Once they are identified, they can be caught and modified the same way as the others:

  • LB_ADDSTRING
  • LB_INSERTSTRING
  • LB_DELETESTRING
  • LB_DIR
  • LB_RESETCONTENT

After the control is subclassed, we can catch any message that is sent to it through the control's message map:

...
BEGIN_MESSAGE_MAP(CHScrollListBox, CListBox)
  //{{AFX_MSG_MAP(CHScrollListBox)
  // NOTE - the ClassWizard will add and remove mapping macros here.
  //}}AFX_MSG_MAP
  ON_MESSAGE(LB_ADDSTRING, OnAddString)
  ON_MESSAGE(LB_INSERTSTRING, OnInsertString)
  ON_MESSAGE(LB_DELETESTRING, OnDeleteString)
  ON_MESSAGE(LB_DIR, OnDir)
  ON_MESSAGE(LB_RESETCONTENT, OnResetContent)
END_MESSAGE_MAP()
...

When we catch the message (for example, LB_ADDSTRING), we are going to call default processing first, and, if the returned code is okay (most of these messages return some kind of a completion indicator), call our functions, either SetNewHExtent() or ResetHExtent(), that will bring the horizontal extent member of ListBox (this is the parameter that affects the horizontal scroll bar, if the bar is present) in sync with the longest string that it contains:

...
////////////////////////////////////////////////////////////////
// OnAddString: wParam - none, lParam - string, returns - int
////////////////////////////////////////////////////////////////
LRESULT CHScrollListBox::OnAddString(WPARAM wParam, LPARAM lParam)
{
  LRESULT lResult = Default();
  if (!((lResult == LB_ERR) || (lResult == LB_ERRSPACE)))
    SetNewHExtent((LPCTSTR) lParam);
  return lResult;
}
...

SetNewHExtent() in turn will calculate in pixels the length of the supplied string in the given device context (GetTextLen()) and set a new horizontal extent for the control. The extent can be set through CListBox::SetHorizontalExtent(extent) and we do it only if it's bigger then the current extent:

...
////////////////////////////////////////////////////////////////
void CHScrollListBox::SetNewHExtent(LPCTSTR lpszNewString)
{
  int iExt = GetTextLen(lpszNewString);
  if (iExt > GetHorizontalExtent())
    SetHorizontalExtent(iExt);
}
...

Let's take a look at what GetTextLen() does:

...
int CHScrollListBox::GetTextLen(LPCTSTR lpszText)
{
  ASSERT(AfxIsValidString(lpszText));

  CDC *pDC = GetDC();
  ASSERT(pDC);

  CSize size;
  CFont* pOldFont = pDC->SelectObject(GetFont());
  if ((GetStyle() & LBS_USETABSTOPS) == 0)
  {
    size = pDC->GetTextExtent(lpszText, (int) _tcslen(lpszText));
    size.cx += 3;
  }
  else
  {
    // Expand tabs as well
    size = pDC->GetTabbedTextExtent(lpszText, (int)
          _tcslen(lpszText), 0, NULL);
    size.cx += 2;
  }
  pDC->SelectObject(pOldFont);
  ReleaseDC(pDC);

  return size.cx;
}
...

The first call is a sanity check with AfxIsValidString(). Then we get the control's DC through GetDC() and select the control's font in the context. After that, we look at the styles. If LBS_USETABSTOPS is set, we call GetTabbedTextExtent(); otherwise, it's GetTextExtent(), which returns the number of pixels that the specified string will occupy on the screen in a horizontal direction when drawn in that device context. I add a few more pixels here, just to make sure the string looks symmetrical when displayed, because the box has a margin on the left. That's it.

In calls OnDeleteString() and OnDir(), I use the function named ResetHExtent(). Because I do not know what string has the next longest extent from the one I'm deleting and since OnDir() will reset the content and fill it in with directory or file names, I'm simply going through the whole list, looking for the longest string the list has and setting its extent.

 

Usage

Okay, how one can utilize CHScrollListBox class? Here are some steps to take, sort of manual labor, that will get you there:

  1. In the resource editor, create a dialog template that will host a listbox control.
  2. Set properties of the list box according to the picture above (at least, Horizontal Scroll, Selection, and Owner Draw should match).
  3. In the header file of your dialog's class, associated with the template, add:
    #include "HScrollListBox.h"
  4. Define a member variable of CHScrollListBox, like so:
    CHScrollListBox m_listBox;
  5. In the implementation file, in DoDataExchange() function, add a line:
    DDX_Control(pDX, IDC_YOUR_LISTBOX_CTRL_ID, m_listBox);
    This call in turn calls SubclassDlgItem(), which does the trick of routing all messages destined to the control to our message map.

Of course, IDC_YOUR_LISTBOX_CTRL_ID and m_listBox are orbitrary names; you should use your own instead.

 

VC ListBox列表框实现自动水平滚动功能 利用CListBox::SetHorizontalExtent()成员可实现该功能。网上可行的方法主要是添加一个CListBox的派生类,并重载其AddString, InsertString方法。虽然使用方便,但是额外添加一个类。本文在不添加派生类的情况下实现该功能。 1. 首先建立一个对话框CMyDlg的应用程序,添加一个列表框、三个按钮(添加、编辑、删除),列表框右键属性-》Horizon... 阅读详情

相关推荐

UE4 UMG Scroll Box实战:打造高性能可滚动UI布局与优化指南

在游戏开发与交互界面设计中,可滚动容器是处理内容溢出的基础解决方案,其核心原理是通过视口裁剪与动态加载机制,仅渲染可见区域内容以优化性能。从技术价值看,高效的滚动实现直接关系到应用的流畅度与用户体验,尤其在数据密集型场景如游戏背包、列表视图中至关重要。在UE4的UMG框架下,Scroll Box控件正是这一理念的工程实践载体,它通过方向控制、滚动行为参数(如惯性滚动)与裁剪优化,为复杂UI布局提供了灵活支持。本文聚焦于Scroll Box的实战配置与性能调优,深入解析如何结合ListView实现动态条目复用

weixin_30542079的博客 421

为List组件添加可随内容变化的滚动条

 http://www.adobe.com/2006/mxml" creationComplete="init();">      import mx.collections.ArrayCollection;                  [Bindable]         private var listData:ArrayCollection = new ArrayCollecti

待定的专栏 1153

VB在ListBox列表上增加水平滚动条的功能

内容索引:VB源码,报表打印,ListBox,滚动条  VB在ListBox列表上增加一个水平滚动条的功能,这样更方便于用户的操作,大家都知道,原有的listbox控件一般都没有自带这种功能,这样将ListBox里的文字比较多时,就会被隐藏,如果有了滚动条,用户就可以拖动滚动条将内容阅读完,也算是扩展了一下ListBox的功能吧。

C++ List Box新增水平滚动条功能

1、在MFC里面先定义一个List Box对象,再在类里面关联一个对象变量class CDlg_Multi : public CDialog{public:CListBox    m_ListInfo;......}2、在初始化成员函数里:BOOL CDlg_Multi::OnInitDialog(){......    m_ListInfo.SetHorizontalExtent(1000); ...

weixin_40407666的博客 671

listbox 上下 左右 滚动条都出现

listbox 上下 左右 滚动条都出现

Ubuntu 20.04下Cube-Studio云原生平台单机部署全攻略(附避坑指南)

本文详细介绍了在Ubuntu 20.04系统上单机部署Cube-Studio云原生平台的完整流程,包括环境准备、Docker配置、Rancher与Kubernetes集群部署以及Cube-Studio核心组件安装。特别提供了官方文档未涵盖的实战技巧和常见问题解决方案,帮助开发者高效完成部署并避免常见陷阱。

weixin_29313423的博客 464

给 MHTML 添加滚动条.mhtml

摘要:本文提供了两种MHTML文件显示问题的解决方案。针对缺少滚动条的问题,建议在HTML头部添加CSS样式强制为根元素和body添加滚动条,并提供调试建议。对于水平宽度不足的问题,提供了扩展宽度并添加垂直滚动条的CSS方案,包括重置布局、移除宽度限制和确保内容换行等样式。两种方案都详细说明了具体添加位置,并提供了备选方案和调试方法,帮助用户优化MHTML文件的显示效果。

tiandyoin的专栏 840

EDSR图像超分辨程序复现(踩坑及解决记录)

EDSR图像超分辨程序复现(踩坑及解决记录)

Sunny_Coder的博客 3642

List Box 控件的水平滚动

在接收数据的时候,自动设置水平显示的长度  CSize   sz;  CPaintDC   dc(this);  sz=dc.GetTextExtent(str);  if(m_list_width  {   m_list_width=sz.cx + 100;   m_list_recv2.SendMessage(LB_SETHORIZONTALEXTENT,m_list_widt

badboyccj的专栏 533

LVGL 实战:虚拟列表与对象复用机制在嵌入式GUI中的性能优化

本文深入探讨LVGL虚拟列表与对象复用机制在嵌入式GUI中的性能优化实践。通过复用机制动态管理UI对象,大幅降低内存占用,实现无限滚动列表的流畅体验。文章详细解析核心算法、内存优化策略及跨平台适配技巧,帮助开发者在资源受限设备上高效处理大量数据展示。

rice5的博客 1106

【WorkBuddy专栏25】如何选择大模型——积分消耗、用途场景、选型决策全指南

【WorkBuddy专栏25】如何选择大模型——积分消耗、用途场景、选型决策全指南

AI人人懂 935

MFC中List box的用法

List Box应该是同时具有Edit Control和Combo Box的一些特性.说它像前者因为它可以把所有的内容分行显示出来(但不可编辑),所以一般起显示信息的作用.而Edit Control主要是让用户输入信息,它也可以让用户输入多行信息,要是把它设成Read Only的话初看起来跟List Box样子还真像的. 另外嘛List Box有类似Combo Box的一些操作,比如Ad

曲终人散 8772

华为OD机试双机位C卷:日志排序 (Py/C/C++/Java/Js/Go)

华为OD机试双机C卷真题【日志排序】,提供C语言、Java、C++、Python、Go、JavaScript源码实现,并提供完整思路讲解。华为OD机试-日志排序。

qq_45776114的博客 1754

修改滚动条

css3修改滚动条的样式 修改某个div的滚动条默认样式: #search-list::-webkit-scrollbar-track { border-radius: 10px; background-color: #F5F5F5; } #search-list::-webkit-scrollbar { width: 14px; background-color: #F5F5F5; }...

努力让自己幸运... 286

别再用LabelImg了!我手搓了一个C#“一站式”YOLO工具箱,标注训练推理全搞定

这个工具箱目前已经在两个小型产线项目中试用,反馈非常好。效率提升:原本需要 2 天完成的“数据准备 - 训练 - 验证”循环,现在缩短到了 2 小时。门槛降低:产线技术员经过 10 分钟培训,就能独立完成新产品的模型迭代。当然,它还有改进空间未来计划集成主动学习功能:自动筛选出置信度低的图片,推荐优先标注。支持多模型管理:方便在不同产品型号间快速切换。增加PLC 通信模块:直接在推理界面配置 IO 信号,实现真正的软硬联动。最后想说工具的本质是服务于人。

这里只有干货:从 Modbus 通信到 MES 对接,从 YOLO 训练到工控机部署,带你搞定工业软件开发全流程。 441

【A2DP】规范精讲[1]: 深入解析A2DP协议的核心架构

在蓝牙技术发展的早期阶段,音频传输主要通过SCO(同步面向连接)链路实现,这种设计主要针对语音通信,音质有限且功能单一。随着人们对无线高质量音频需求的增长,​​Advanced Audio Distribution Profile (A2DP)​​ 应运而生,成为蓝牙音频传输的技术基石。

byte轻骑兵的技术小窝 1万+

List Box 消息

 LBS_EXTENDEDSEL       使用键盘“SHIFT”键或另指定键加鼠标进行多选。就是VC  RC 编辑器上的 Selection 里面选上ExtendedLBS_MULTICOLUMN       多列列表框,带有水平滚动条。应用程序可以用 LB_SETCOLUMNWIDTH 消息来为列表框指定列数。 LBS_MULTIPLESEL       支持多重选择。LBS

The past will never return. 645

手把手教你学Simulink——基于全桥 LLC 谐振变换器的双向 DC-DC 建模与仿真

本文详细介绍了基于Simulink的双向全桥LLC谐振变换器建模与仿真方法。内容涵盖:1)LLC谐振变换器背景与双向化挑战;2)系统架构设计及关键数学推导(FHA增益分析、双向控制逻辑);3)Simulink建模步骤(功率级搭建、PWM生成、同步整流实现);4)仿真结果分析(正向/反向工作验证、ZVS特性);5)工程实践建议(增益微调、代码生成与HIL测试)。通过手把手教学,帮助读者掌握950V↔48V双向LLC变换器的完整设计流程,实现高效率能量双向流动的同时保持软开关特性,适用于储能系统、车载充电等应用

MHD0815的博客 175

VC++ List Box 控件 详解2

文章内容摘要: ListBox控件的使用 ListBox窗户用来列出一系列的文本,每条则本占一行。创建一个列表窗户可使用成员函数: BOOL CListBox::Create( LPCTSTR lpszText, DWORD dwStyle, const RECT rect, CWnd* pParentWnd, UINT nID = 0xffff ); 其中dwStyle将指明该窗户的风 Lis

1152

ET2012新版免狗免安装绿色软件

ET2012新版免狗免安祖昂绿色软件。,新界面,新视觉,

SAP B1中文培訓教材_SDK

公司内部培训资料,对开发工具 ,DIAPI, UIAPI,Add-ON有详细解说

MFC控件(4):List Box

List Box应该是同时具有Edit Control和Combo Box的一些特性.说它像前者因为它可以把所有的内容分行显示出来(但不可编辑),所以一般起显示信息的作用.而Edit Control主要是让用户输入信息,它也可以让用户输入多行信息,要是把它设成Read Only的话初看起来跟List Box样子还真像的. 另外嘛List Box有类似Combo Box的一些操作,比如AddStr...

weiwenhp 9602

霍尼韦尔SoftMaster使用说明

霍尼韦尔SoftMaster使用说明

asp.net中给listbox添加横向滚动条

经常在listbox中的数据项太长而不能看完,系统本身是有纵向滚动条,只能是对多项数据项有效,而如果太长,则只能自己解决,一种解决办法:<div id='hello' style="z-index: 102; left: 240px; overflow: auto; width: 300px; top: 70px;height: 200px"><asp:ListBox ID="l...

weixin_30457551的博客 212

VB摄像头监控

该VB模块可对摄像头捕获的画面进行录像等,可在创建的窗口进行控制

上一篇: 遁去的"一"
下一篇: SuSe上配置samba笔记
台风双头蛇
博客等级 码龄26年 7粉丝 13原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值