zoukankan      html  css  js  c++  java
  • delphi自动调整combobox下拉列表宽度(PostMessage CB_SETDROPPEDWIDTH)

    在combobox所在的窗口的Formshow事件里调用即可

    [delphi] view plain copy
     
    1. procedure SetComboBoxListWidth( AComboBox: TComboBox );  
    2. var  
    3.   i: Integer;  
    4.   nMaxLen, nMinWidth: integer;  
    5.   nFontWidth: Integer;  
    6.   nCboLeft: integer;  
    7.   ctlCustom: TControl;  
    8. begin  
    9.   nCboLeft := AComboBox.Left;  
    10.   ctlCustom := AComboBox;  
    11.   with AComboBox do  
    12.   begin  
    13.     nFontWidth := Round( Abs(Font.Height / 2 ) );  
    14.     nMaxLen:= 0;  
    15.     for i:=to Items.Count-do  
    16.     begin  
    17.       if length(Items[i])* nFontWidth > nMaxLen then  
    18.         nMaxlen:= length(Items[i])* nFontWidth+5;  
    19.     end;  
    20.     if Items.Count > DropDownCount then  
    21.       nMaxLen := nMaxLen + 20;  
    22.     if nMaxLen > Width then  
    23.     begin  
    24.       if Items.Count>DropDownCount then  
    25.       begin  
    26.         SendMessage( Handle, CB_SETHORIZONTALEXTENT, nMaxLen+5, 0 );  
    27.   
    28.         {解决分辨率小导致的ComboboxList的宽度超出屏幕}  
    29.         //while 部分是用来取出combobox控件相对与窗体的横坐标  
    30.         while not (ctlCustom.Parent is TForm) do  
    31.         begin  
    32.           nCboLeft := nCboLeft + ctlCustom.Parent.Left;  
    33.           ctlCustom := ctlCustom.Parent;  
    34.         end;  
    35.         nMinWidth := Min(400, nMaxLen);                      // 使用不大于nMaxLen的数做比较  
    36.         if (nCboLeft + nMinWidth) > Screen.Width-25 then     // 不超出屏幕,并保留窗口滚动条宽度,约25  
    37.           nMinWidth := Screen.Width-25-nCboLeft;  
    38.         nMinWidth := Max(nMinWidth, Width);                  // 不小于控件自身宽度  
    39.   
    40.         PostMessage(Handle, CB_SETDROPPEDWIDTH, nMinWidth, 0);   
    41.       end  
    42.       else  
    43.         PostMessage(Handle, CB_SETDROPPEDWIDTH, nMaxLen , 0);  
    44.           
    45.       ShowHint := True;  
    46.     end  
    47.     else  
    48.     begin  
    49.       SendMessage( Handle, CB_SETHORIZONTALEXTENT, 0, 0 );  
    50.       PostMessage(Handle, CB_SETDROPPEDWIDTH, Width , 0);  
    51.     end;  
    52.   end;  
    53. end;  

    http://blog.csdn.net/youthon/article/details/8179348

  • 相关阅读:
    LeetCode---Remove Duplicates from Sorted List II
    hackerrank---Sets
    hackerrank---Find a string
    push的时候隐藏底部的tabbar
    选择性编译代码:如 #ifdef __IPHONE_7_0
    客户端的文件存储文件夹代表意义
    设置非ARC
    KVC设置系统自带属性,不管是不是私有的属性
    一些关于队列,同步,异步的概念
    要求两个异步任务都完成后, 才能回到主线程:dispatch_group_t
  • 原文地址:https://www.cnblogs.com/findumars/p/7230697.html
Copyright © 2011-2022 走看看