嗨。
当 BiDiMode= bdRightToLeft 和 Style= csDropDownList 且应用程序使用 VCL 样式时,TComboBoxEx 项目无法正确绘制;在下拉列表中,左侧绘制的图标和文字,当选择一个项目时,左侧绘制的图标和文字将会消失!
我看到了Right to left ComboBox in Delphi XE2 with styles但没有帮助我。
我应该如何纠正它并在 ComboBoxEx 右侧绘制图标和文本(第一个图标和下一个图标)?
这正是我所需要的,我用 Photoshop 设计了这个示例:
我使用Delphi XE8
请帮助我。
最佳答案
BiDiMode 适用于从右向左书写的语言,因此并不真正适合您的需求。
我找不到使用 TComboBoxEx 实现此操作的方法,但您可以相当轻松地使用 TComboBox 实现此操作。
添加一个TComboBox并使其样式为csOwnerDrawFixed。我在下面的代码中假设了 TImageList(您必须已经拥有)和 TComboBox 的基本名称。您需要将其修改为您自己的名称。添加一个 OnDrawItem 事件,类似于下面的事件。 (你可能想稍微调一下)。
procedure TForm1.ComboBox1DrawItem(Control: TWinControl; Index: Integer;
Rect: TRect; State: TOwnerDrawState);
var
iImageWidth, iTextWidth, iMargin : integer;
iText : string;
iCanvas : TCanvas;
begin
// draw image at right and text right justify
// assume image index = Item for now.
iCanvas := ComboBox1.Canvas;
// need to check state; Just ignore for now.
iImageWidth := ImageList1.Width;
iMargin := 4; // pixels - can calculate instead
iText := ComboBox1.Items[ Index ];
iTextWidth := iCanvas.TextWidth( iText);
ImageList1.Draw( iCanvas, Rect.Right - iImageWidth - iMargin, Rect.Top, Index );
iCanvas.TextOut( Rect.Right - 2 * iMargin - iTextWidth - iImageWidth, Rect.Top, iText);
end;
我测试过,效果很好
更新
关于delphi - 当 BiDiMode= bdRightToLeft 和 Style= csDropDownList 且应用程序使用 VCL 样式时,TComboBoxEx 项目无法正确绘制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32337779/