首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >TLocalizedComboBox不调用OnDrawItem

TLocalizedComboBox不调用OnDrawItem
EN

Stack Overflow用户
提问于 2021-01-30 21:06:54
回答 1查看 20关注 0票数 1

我正在尝试实现一个基于TComboBox类的自定义控件。下面的代码可以编译,但从未调用过OnDrawItem (MyDrawItem)。我做错了什么?

代码语言:javascript
复制
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.
EN

回答 1

Stack Overflow用户

发布于 2021-01-31 00:12:31

我在下面的代码中解决了大部分问题。仍然有一些事情不是我想要的方式。

  • 只有在设计器中没有设置不同的样式时,代码“different := csOwnerDrawFixed”才有效。我必须在设计器中将该属性设置为csOwnerDrawFixed,或者保留默认的“csDropDown”。(b.t.w.这就是为什么从未调用过DrawItem方法)。
  • 颜色与csDropDownList ComboBoxes不同,如下图所示。第三个是我在下面代码中的实现。

代码语言:javascript
复制
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.
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65968331

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档