1. unit Unit3;
  2. interface
  3. uses
  4. Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants,
  5. System.Classes, Vcl.Graphics,
  6. Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls;
  7. type
  8. TForm3 = class(TForm)
  9. ComboBox1: TComboBox;
  10. private
  11. { Private declarations }
  12. public
  13. { Public declarations }
  14. end;
  15. var
  16. Form3: TForm3;
  17. implementation
  18. uses
  19. Vcl.Themes,
  20. Vcl.Styles;
  21. {$R *.dfm}
  22. type
  23. TComboBoxStyleHookFix = class(TComboBoxStyleHook)
  24. protected
  25. procedure DrawItem(Canvas: TCanvas; Index: Integer; const R: TRect;
  26. Selected: Boolean); override;
  27. end;
  28. procedure TComboBoxStyleHookFix.DrawItem(Canvas: TCanvas; Index: Integer;
  29. const R: TRect; Selected: Boolean);
  30. var
  31. DIS: TDrawItemStruct;
  32. Text: string;
  33. begin
  34. begin
  35. FillChar(DIS, SizeOf(DIS), 0);
  36. DIS.CtlType := ODT_COMBOBOX;
  37. DIS.CtlID := GetDlgCtrlID(Handle);
  38. DIS.itemAction := ODA_DRAWENTIRE;
  39. DIS.hDC := Canvas.Handle;
  40. DIS.hwndItem := Handle;
  41. DIS.rcItem := R;
  42. Text := TComboBox(Control).Items[Index];
  43. DIS.rcItem.Left := DIS.rcItem.Left +
  44. (DIS.rcItem.Width - Canvas.TextWidth(Text) - 5);
  45. DIS.itemID := Index;
  46. DIS.itemData := SendMessage(ListHandle, LB_GETITEMDATA, 0, 0);
  47. if Selected then
  48. DIS.itemState := DIS.itemState { or ODS_FOCUS } or ODS_SELECTED;
  49. SendMessage(Handle, WM_DRAWITEM, Handle, LPARAM(@DIS));
  50. end;
  51. end;
  52. initialization
  53. TStyleManager.Engine.RegisterStyleHook(TComboBox, TComboBoxStyleHookFix);
  54. end.