我正在尝试实现一个基于TComboBox类的自定义控件。下面的代码可以编译,但从未调用过OnDrawItem (MyDrawItem)。我做错了什么?
unit TLocalizedComboBox_unit;
interface
uses
System.SysUtils, System.Classes, Vcl.Controls, Vcl.StdCtrls, System.Types;
type
TLocalizedComboBox = class(TComboBox)
private
{ Private declarations }
protected
{ Protected declarations }
published
{ Published declarations }
public
{ Public declarations }
constructor Create(AOwner: TComponent); override;
procedure MyDrawItem(Control: TWinControl; Index: Integer; Rect: TRect; State: TOwnerDrawState);
end;
procedure Register;
implementation
constructor TLocalizedComboBox.Create(AOwner: TComponent);
begin
Style := csOwnerDrawFixed;
OnDrawItem := Self.MyDrawItem;
inherited Create(AOwner);
end;
procedure TLocalizedComboBox.MyDrawItem(Control: TWinControl; Index: Integer; Rect: TRect; State: TOwnerDrawState);
begin
Canvas.TextRect(Rect, Rect.Left, Rect.Top, Items.Names[Index]);
end;
procedure Register;
begin
RegisterComponents('MyProjectsComponents', [TLocalizedComboBox]);
end;
end.发布于 2021-01-31 00:12:31
我在下面的代码中解决了大部分问题。仍然有一些事情不是我想要的方式。

unit TLocalizedComboBox_unit;
interface
uses
System.SysUtils, System.Classes, Vcl.Controls, Vcl.StdCtrls, System.Types, Vcl.Forms,
TLanguagesManager_6, _LanguageManagerConstants;
type
TLocalizedComboBox = class(TComboBox)
private
function GetForm(control: TControl) : TForm;
published
procedure DrawItem(Index: Integer; Rect: TRect; State: TOwnerDrawState); override;
public
constructor Create(AOwner: TComponent); override;
end;
procedure Register;
implementation
constructor TLocalizedComboBox.Create(AOwner: TComponent);
begin
inherited;
inherited Style := csOwnerDrawFixed;
end;
// Custom draw routine for the item. The displayed text is translated
procedure TLocalizedComboBox.DrawItem(Index: Integer; Rect: TRect; State: TOwnerDrawState);
var
itemToDisplay: string;
translation : string;
form : TForm;
formName : string;
begin
itemToDisplay := Items[Index];
translation := itemToDisplay;
if Length(itemToDisplay) > 0 then
begin
form := GetForm(Self);
if form <> nil then formName := form.Name + '.' else formName := '';
translation := TLanguagesManager.GetSingleton.GetText(formName + Name + '.' + itemToDisplay, itemToDisplay);
end;
Canvas.TextRect(Rect, Rect.Left + 2, Rect.Top, translation);
end;
// Find the Form this control is on
function TLocalizedComboBox.GetForm(control: TControl) : TForm;
var
form: TControl;
begin
form := control.Parent;
while form <> nil do
begin
if form is TForm then
begin
Result := form as TForm;
form := nil;
end
else
form := form.Parent;
end;
end;
procedure Register;
begin
RegisterComponents('MyProjectsComponents', [TLocalizedComboBox]);
end;
end.https://stackoverflow.com/questions/65968331
复制相似问题