delphi自动调整combobox下拉列表宽度(PostMessage CB_SETDROPPEDWIDTH)
2017-07-24 19:46
357 查看
在combobox所在的窗口的Formshow事件里调用即可
[delphi] view plain copy
procedure SetComboBoxListWidth( AComboBox: TComboBox );
var
i: Integer;
nMaxLen, nMinWidth: integer;
nFontWidth: Integer;
nCboLeft: integer;
ctlCustom: TControl;
begin
nCboLeft := AComboBox.Left;
ctlCustom := AComboBox;
with AComboBox do
begin
nFontWidth := Round( Abs(Font.Height / 2 ) );
nMaxLen:= 0;
for i:=0 to Items.Count-1 do
begin
if length(Items[i])* nFontWidth > nMaxLen then
nMaxlen:= length(Items[i])* nFontWidth+5;
end;
if Items.Count > DropDownCount then
nMaxLen := nMaxLen + 20;
if nMaxLen > Width then
begin
if Items.Count>DropDownCount then
begin
SendMessage( Handle, CB_SETHORIZONTALEXTENT, nMaxLen+5, 0 );
{解决分辨率小导致的ComboboxList的宽度超出屏幕}
//while 部分是用来取出combobox控件相对与窗体的横坐标
while not (ctlCustom.Parent is TForm) do
begin
nCboLeft := nCboLeft + ctlCustom.Parent.Left;
ctlCustom := ctlCustom.Parent;
end;
nMinWidth := Min(400, nMaxLen); // 使用不大于nMaxLen的数做比较
if (nCboLeft + nMinWidth) > Screen.Width-25 then // 不超出屏幕,并保留窗口滚动条宽度,约25
nMinWidth := Screen.Width-25-nCboLeft;
nMinWidth := Max(nMinWidth, Width); // 不小于控件自身宽度
PostMessage(Handle, CB_SETDROPPEDWIDTH, nMinWidth, 0);
end
else
PostMessage(Handle, CB_SETDROPPEDWIDTH, nMaxLen , 0);
ShowHint := True;
end
else
begin
SendMessage( Handle, CB_SETHORIZONTALEXTENT, 0, 0 );
PostMessage(Handle, CB_SETDROPPEDWIDTH, Width , 0);
end;
end;
end;
http://blog.csdn.net/youthon/article/details/8179348
[delphi] view plain copy
procedure SetComboBoxListWidth( AComboBox: TComboBox );
var
i: Integer;
nMaxLen, nMinWidth: integer;
nFontWidth: Integer;
nCboLeft: integer;
ctlCustom: TControl;
begin
nCboLeft := AComboBox.Left;
ctlCustom := AComboBox;
with AComboBox do
begin
nFontWidth := Round( Abs(Font.Height / 2 ) );
nMaxLen:= 0;
for i:=0 to Items.Count-1 do
begin
if length(Items[i])* nFontWidth > nMaxLen then
nMaxlen:= length(Items[i])* nFontWidth+5;
end;
if Items.Count > DropDownCount then
nMaxLen := nMaxLen + 20;
if nMaxLen > Width then
begin
if Items.Count>DropDownCount then
begin
SendMessage( Handle, CB_SETHORIZONTALEXTENT, nMaxLen+5, 0 );
{解决分辨率小导致的ComboboxList的宽度超出屏幕}
//while 部分是用来取出combobox控件相对与窗体的横坐标
while not (ctlCustom.Parent is TForm) do
begin
nCboLeft := nCboLeft + ctlCustom.Parent.Left;
ctlCustom := ctlCustom.Parent;
end;
nMinWidth := Min(400, nMaxLen); // 使用不大于nMaxLen的数做比较
if (nCboLeft + nMinWidth) > Screen.Width-25 then // 不超出屏幕,并保留窗口滚动条宽度,约25
nMinWidth := Screen.Width-25-nCboLeft;
nMinWidth := Max(nMinWidth, Width); // 不小于控件自身宽度
PostMessage(Handle, CB_SETDROPPEDWIDTH, nMinWidth, 0);
end
else
PostMessage(Handle, CB_SETDROPPEDWIDTH, nMaxLen , 0);
ShowHint := True;
end
else
begin
SendMessage( Handle, CB_SETHORIZONTALEXTENT, 0, 0 );
PostMessage(Handle, CB_SETDROPPEDWIDTH, Width , 0);
end;
end;
end;
http://blog.csdn.net/youthon/article/details/8179348
相关文章推荐
- delphi自动调整combobox下拉列表宽度
- delphi自动调整combobox下拉列表宽度
- ComboBox 自动调整组合框下拉部分的宽度
- ComboBox 自动调整组合框下拉部分的宽度
- 如何重载ComboBox 使其下拉按钮(带下箭头的)和下拉列表的垂直滚动条的宽度改变?(自绘ComboBox)
- Delphi中实现DBGrid列宽度自动调整
- easyui combobox获取到焦点自动展开下拉列表
- Delphi实现DBGrid列宽度自动调整
- UILabell自动调整宽度width
- 在工具栏(ToolBar)上添加ComboBox控件,为何每次在下拉列表中选择时,都会自动的选中工具栏上的一个按钮?
- 如何让ComboBox的下拉列表宽度自适应内容的宽度
- ComboBox自动匹配下拉列表中的值
- 列表针对列宽度自动调整显示内容,超长以...缩写结尾
- 重写winform 的 ComboBox控件实现自动加载磁盘下拉列表框
- extjs combobox 仿百度查询功能,设置下拉列表宽度
- Flex ComboBox下拉列表不自动更新解决方法
- DataGridView的DataGridViewComboBoxColumn列在编辑时自动弹出下拉列表
- winform窗体中 ComboBox 控件的DropDownWidth属性根据显示的文本设置下拉列表的显示宽度
- 关于在程序中设定combobox的下拉菜单宽度[delphi]
- 增加Android下spinner控件的点击下拉列表自动更新事件并实例应用--USB摄像头在spinner内下拉更新(二)