Delphi automatically adjusts the ComboBox drop-down list width (PostMessage cb_setdroppedwidth)

Source: Internet
Author: User

Called in the Formshow event of the window where the ComboBox is located

[Delphi]View PlainCopy
  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:=0 to Items. count-1 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. {Resolution Small causes the width of the comboboxlist to exceed the screen}
  28. The //while section is used to remove a ComboBox control relative to the horizontal axis of the form
  29. While not (Ctlcustom. Parent is tform) do
  30. begin
  31. Ncboleft: = Ncboleft + Ctlcustom. Parent.  Left;
  32. Ctlcustom: = Ctlcustom.  Parent;
  33. end;
  34. Nminwidth: = Min (n, Nmaxlen); //Use a number less than Nmaxlen to compare
  35. if (ncboleft + nminwidth) > Screen. width- then//do not exceed the screen and keep the window scroll bar width, approx.
  36. Nminwidth: = Screen.  width-25-ncboleft;
  37. Nminwidth: = Max (Nminwidth, Width); //not less than the width of the control itself
  38. PostMessage (Handle, Cb_setdroppedwidth, Nminwidth, 0);
  39. End
  40. Else
  41. PostMessage (Handle, Cb_setdroppedwidth, Nmaxlen, 0);
  42. Showhint: = True;
  43. End
  44. Else
  45. begin
  46. SendMessage (Handle, Cb_sethorizontalextent, 0, 0);
  47. PostMessage (Handle, Cb_setdroppedwidth, Width, 0);
  48. end;
  49. end;
  50. End

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

Delphi automatically adjusts the ComboBox drop-down list width (PostMessage cb_setdroppedwidth)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.