调整下拉框的宽度

The TComboBox component combines an edit box with a scrollable "pick" list. Users can select an item from the list or type directly into the edit box.

TComboBox组件将编辑框与可滚动的“选择”列表结合在一起。 用户可以从列表中选择一项,也可以直接在编辑框中键入内容 。

下拉列表 ( Drop Down List )

When a combo box is in dropped down state Windows draws a list box type of control to display combo box items for selection.

当组合框处于下拉状态时,Windows会绘制列表框的控件类型以显示组合框项目以供选择。

The DropDownCount property specifies the maximum number of items displayed in the drop-down list.

DropDownCount属性指定下拉列表中显示的最大项目数。

The width of the drop-down list would, by default, equal the width of the combo box.

默认情况下, 下拉列表的宽度将等于组合框的宽度。

When the length (of a string) of items exceeds the width of the combobox, the items are displayed as cut-off!

当项目的长度(一串)超过组合框的宽度时,项目显示为截止值!

TComboBox does not provide a way to set the width of its drop-down list :(

TComboBox没有提供一种设置其下拉列表宽度的方法:(

固定ComboBox下拉列表宽度 ( Fixing The ComboBox Drop-Down List Width )

We can set the width of the drop-down list by sending a special Windows message to the combo box. The message is CB_SETDROPPEDWIDTH and sends the minimum allowable width, in pixels, of the list box of a combo box.

我们可以通过向组合框发送特殊的Windows消息来设置下拉列表的宽度。 消息为CB_SETDROPPEDWIDTH,并发送组合框列表框的最小允许宽度(以像素为单位)。

To hardcode the size of the drop-down list to, let's say, 200 pixels, you could do:

要将下拉列表的大小硬编码为200像素,您可以执行以下操作:

SendMessage(theComboBox.Handle, CB_SETDROPPEDWIDTH, 200, 0);

This is only ok if you are sure all your theComboBox.Items are not longer than 200 px (when drawn).

仅当您确定所有theComboBox.Items的长度不超过200 px(绘制时)时,此方法才可以。

To ensure we always have the drop-down list display enough wide, we can calculate the required width.

为了确保下拉列表始终显示足够的宽度,我们可以计算所需的宽度。

Here's a function to get the required width of the drop-down list and set it:

这是一个函数,用于获取所需的下拉列表宽度并进行设置:

procedure ComboBox_AutoWidth(const theComboBox: TCombobox);
const
HORIZONTAL_PADDING = 4;
var
itemsFullWidth: integer;
idx: integer;
itemWidth: integer;
begin
itemsFullWidth := 0;
// get the max needed with of the items in dropdown state
for idx := 0 to -1 + theComboBox.Items.Count do
begin
itemWidth := theComboBox.Canvas.TextWidth(theComboBox.Items[idx]);
Inc(itemWidth, 2 * HORIZONTAL_PADDING);
if (itemWidth > itemsFullWidth) then itemsFullWidth := itemWidth;
end;
// set the width of drop down if needed
if (itemsFullWidth > theComboBox.Width) then
begin
//check if there would be a scroll bar
if theComboBox.DropDownCount < theComboBox.Items.Count then
itemsFullWidth := itemsFullWidth + GetSystemMetrics(SM_CXVSCROLL);
SendMessage(theComboBox.Handle, CB_SETDROPPEDWIDTH, itemsFullWidth, 0);
end;
end;

The width of the longest string is used for the width of the drop-down list.

最长字符串的宽度用于下拉列表的宽度。

When to call ComboBox_AutoWidth?If you pre-fill the list of items (at design time or when creating the form) you can call the ComboBox_AutoWidth procedure inside the form's OnCreate event handler.

何时调用ComboBox_AutoWidth? 如果您预先填写了项目列表(在设计时或在创建窗体时),则可以在窗体的OnCreate事件处理程序中调用ComboBox_AutoWidth过程。

If you dynamically change the list of combo box items, you can call the ComboBox_AutoWidth procedure inside the OnDropDown event handler - occurs when the user opens the drop-down list.

如果您动态更改组合框项目的列表,则可以在OnDropDown事件处理程序中调用ComboBox_AutoWidth过程-在用户打开下拉列表时发生。

A TestFor a test, we have 3 combo boxes on a form. All have items with their text more wide than the actual combo box width. The third combo box is placed near the right edge of the form's border.

测试对于一个测试,我们在表单上有3个组合框。 所有项目的文本都比实际组合框的宽度宽。 第三个组合框放置在窗体边框的右边缘附近。

The Items property, for this example, is pre-filled - we call our ComboBox_AutoWidth in the OnCreate event handler for the form:

在此示例中,Items属性是预先填充的-我们在OnCreate事件处理程序中将表单的ComboBox_AutoWidth称为:

//Form's OnCreate
procedure TForm.FormCreate(Sender: TObject);
begin
ComboBox_AutoWidth(ComboBox2);
ComboBox_AutoWidth(ComboBox3);
end;

We've not called ComboBox_AutoWidth for Combobox1 to see the difference!

我们没有为Combobox1调用ComboBox_AutoWidth来看到区别!

Note that, when run, the drop-down list for Combobox2 will be wider than Combobox2.

请注意,运行时,Combobox2的下拉列表将比Combobox2宽。

整个下拉列表被切除为“在右边缘附近放置” ( The Entire Drop-Down List Is Cut Off For "Near Right Edge Placement" )

For Combobox3, the one placed near the right edge, the drop-down list is cut off.

对于Combobox3(位于右边缘附近的那个),下拉列表被切除。

Sending the CB_SETDROPPEDWIDTH will always extend the drop-down list box to the right. When your combobox is near the right edge, extending the list box more to the right would result in the display of the list box being cut off.

发送CB_SETDROPPEDWIDTH将始终将下拉列表框扩展到右侧。 当您的组合框位于右边缘附近时,将列表框向右扩展更多将导致该列表框的显示被切断。

We need to somehow extend the list box to the left when this is the case, not to the right!

在这种情况下,我们需要以某种方式将列表框扩展到左侧,而不是右侧!

The CB_SETDROPPEDWIDTH has no way of specifying to what direction (left or right) to extend the list box.

CB_SETDROPPEDWIDTH无法指定向哪个方向(向左或向右)扩展列表框。

解决方案:WM_CTLCOLORLISTBOX ( Solution: WM_CTLCOLORLISTBOX )

Just when the drop-down list is to be displayed Windows sends the WM_CTLCOLORLISTBOX message to the parent window of a list box - to our combo box.

Windows仅在显示下拉列表时,将WM_CTLCOLORLISTBOX消息发送到列表框的父窗口-到我们的组合框。

Being able to handle the WM_CTLCOLORLISTBOX for the near-right-edge combobox would solve the problem.

能够处理近右边缘组合框的WM_CTLCOLORLISTBOX将解决该问题。

The Almighty WindowProcEach VCL control exposes the WindowProc property - the procedure that responds to messages sent to the control. We can use the WindowProc property to temporarily replace or subclass the window procedure of the control.

万能的WindowProc每个VCL控件都公开WindowProc属性,该属性是响应发送给该控件的消息的过程。 我们可以使用WindowProc属性临时替换或子类化控件的窗口过程。

Here's our modified WindowProc for Combobox3 (the one near the right edge):

这是我们为Combobox3修改的WindowProc(靠近右边缘的那个):

//modified ComboBox3 WindowProc
procedure TForm.ComboBox3WindowProc(var Message: TMessage);
var
cr, lbr: TRect;
begin
//drawing the list box with combobox items
if Message.Msg = WM_CTLCOLORLISTBOX then
begin
GetWindowRect(ComboBox3.Handle, cr);
//list box rectangle
GetWindowRect(Message.LParam, lbr);
//move it to left to match right border
if cr.Right <> lbr.Right then
MoveWindow(Message.LParam,
lbr.Left-(lbr.Right-clbr.Right),
lbr.Top,
lbr.Right-lbr.Left,
lbr.Bottom-lbr.Top,
True);
end
else
ComboBox3WindowProcORIGINAL(Message);
end;

If the message our combo box receives is WM_CTLCOLORLISTBOX we get its window's rectangle, we also get the rectangle of the list box to be displayed (GetWindowRect). If it appears that the list box would appear more to the right - we move it to the left so that combo box and list box right border is the same. As easy as that :)

如果组合框收到的消息是WM_CTLCOLORLISTBOX,则获得其窗口的矩形,我们还获得要显示的列表框的矩形(GetWindowRect)。 如果列表框看起来更向右显示-我们将其向左移动,以便组合框和列表框的右边框相同。 那样简单:)

If the message is not WM_CTLCOLORLISTBOX we simply call the original message handling procedure for the combo box (ComboBox3WindowProcORIGINAL).

如果消息不是WM_CTLCOLORLISTBOX,我们只需调用组合框的原始消息处理过程(ComboBox3WindowProcORIGINAL)。

Finally, all this can work if we have set it correctly (in the OnCreate event handler for the form):

最后,如果我们已正确设置它(在表单的OnCreate事件处理程序中),那么所有这些方法都可以使用:

//Form's OnCreate
procedure TForm.FormCreate(Sender: TObject);
begin
ComboBox_AutoWidth(ComboBox2);
ComboBox_AutoWidth(ComboBox3);
//attach modified/custom WindowProc for ComboBox3
ComboBox3WindowProcORIGINAL := ComboBox3.WindowProc;
ComboBox3.WindowProc := ComboBox3WindowProc;
end;

Where in the form's declaration we have (entire):

在表单的声明中,我们有(整个):

type
TForm = class(TForm)
ComboBox1: TComboBox;
ComboBox2: TComboBox;
ComboBox3: TComboBox;
procedure FormCreate(Sender: TObject);
private
ComboBox3WindowProcORIGINAL : TWndMethod;
procedure ComboBox3WindowProc(var Message: TMessage);
public
{ Public declarations }
end;

And that's it. All handled :)

就是这样。 全部处理:)

翻译自: https://www.thoughtco.com/sizing-the-combobox-drop-down-width-1058301

调整下拉框的宽度

调整下拉框的宽度_调整组合框下拉宽度的大小相关推荐

  1. r 数据框选子集_在带有组合框的值列表的下拉列表中显示显示属性的子集

    r 数据框选子集 组合框值列表(inputComboboxListOfValues)应该是使用LOV的非常流行的ADF Faces组件. 坦白说,这是我最喜欢的值列表方法. 在这篇简短的文章中,我将重 ...

  2. excel 表做下拉框_Excel工作表的组合框下拉列表

    excel 表做下拉框 Would you prefer a bigger font size for items in a data validation drop down list? Would ...

  3. 易语言写组合框内容MySQL_易语言组合框赋值的几种方法

    在易语言中,给组合框赋值就是给组合框加上许多项目,这些项目我们可以通过编写源代码的时候就事先修改设置好它,同样我们也可以通过后期使用时让代码来修改它,今天就教大家事先给组合框赋值和事后给组合框赋值的两 ...

  4. JAVA组合框怎么添加加减乘除,[C#]组合框设计windows加减乘除简单计算器应用

    组合框使用方法,按照下面图片上提示,设计一个windows应用程序,实现不同运算. 界面设计: 代码: using System; using System.Collections.Generic; ...

  5. java组合框添加函数_Java Swing组合框选择并链接到其他组合框

    这是场景:我在数据库中有一个包含3列(id,name,age)的表.我创建了3个swing组合框和一个按钮,该按钮向数据库发送了一个" select语句",并用addItem(-) ...

  6. ios微信小程序下拉刷新怎么配_为什么他的下拉刷新是个动画效果?

    看到别人的下拉刷新动画!是不是蠢蠢欲动!也想马上拥有一个~~ 比如: 再比如: "小二,上菜!" 1            引入下拉刷新组件(组件代码可联系作者获取)  代码:  ...

  7. jq苹果手机全屏下点击无效果_看剧、下视频、看小说,这一个app就可以~

    推荐看剧.搜索.看小说的app那么多!!! 难道就没有一款可以满足所有需求的app吗? 当然有!!! 今天推荐的这款可以看剧.看小说,还能下载网页视频的浏览器,就可以满足你的所有需求. 有了它,你的手 ...

  8. svn拉取文件合并_四、Jenkins+SVN拉取文件下发

    一.安装SVN [root@docker02 ~]# yum -y install subversion [root@docker02 ~]# mkdir -pv /data/svn [root@do ...

  9. plantuml最大宽度_设置TH最小和最大宽度非常缓慢

    给定一个固定头HTML表,我试图排队标题列与正文行列.我这样做是因为我使用CSS来使头部固定,而头部与身体其他部分不一致.设置TH最小和最大宽度非常缓慢 我正在使用的JavaScript工程,但速度非 ...

最新文章

  1. vc6静态库的生成和调用
  2. 神策数据成为 Adjust 在中国首家数据分析合作伙伴
  3. 赞!Google 资深软件工程师 LeetCode 刷题笔记首次公开
  4. XCode中设置字体大小
  5. api 请求 fail_谈一谈定位api的使用
  6. [渝粤教育] 新乡医学院 医学免疫学 参考 资料
  7. MATLAB库函数resample(重新采样序列)的C语言实现
  8. asp.net接受表单验证格式后再提交数据_看滴普科技大前端如何玩转el-form-renderer 表单渲染器1.14.0
  9. 在XNA 3.0 项目添加声音——播放MP3或WMA声音文件
  10. 【重磅推出】推荐系统系列教程之九:解密“看了又看”和“买了又买”(Item-Based)...
  11. 微信小程序选择图片,查看图片信息,浏览图片,图片上传
  12. 使用python对mysql主从进行监控,并调用钉钉发送报警信息
  13. 视频教程-Protel99se电路设计速成-智能硬件
  14. 自强学堂mysql_Django ——自强学堂学习笔记
  15. 数据挖掘实验(三)Matlab初步实现ID3算法【决策树根节点选择】
  16. android图片颜色识别器,颜色识别器APP
  17. 【coq】函数语言设计 笔记 11 - rel
  18. python循环代码优化技巧_记一次优化python循环代码逻辑的过程
  19. 给宝宝用乳糖酶还在不停地换品牌吗?一文读懂如何正确使用
  20. 小程中的web-view缓存问题

热门文章

  1. 零基础学UI设计,学费要多少?
  2. 【转载】SAP Smartform二维码打印实例教程
  3. JAVA入门[18]-JdbcTemplate简单实例
  4. 分享--python学习网站论坛大全
  5. Nexus与Ubnunt兼容问题
  6. java 自定义AfterReturning切面注解
  7. canvas之文字换行
  8. python脚本根据mask掩码/像素数组裁剪图片
  9. Origin科学绘图软件Origin下载附安装教程
  10. java九九乘法表怎么弄_java怎么实现九九乘法表