Can you help translating this very small C++ component to Delphi?

前端 未结 2 1246
慢半拍i
慢半拍i 2021-01-13 11:06

I\'m translating the following C++ component to Delphi:

http://borland.newsgroups.archived.at/public.delphi.vcl.components.using.win32/200708/0708225318.html

相关标签:
2条回答
  • 2021-01-13 11:42

    @Steve's answer works fine, but with a simple adding you can create an actual line seperator between two items.

    procedure WndProc(var Message: TMessage); override;
    procedure ListWndProc(var Message: TMessage);
    procedure DrawItem(Index: Integer; Rect: TRect;
      State: TOwnerDrawState); override;
    

    Change the last part of DrawItem to:

    if( not Boolean(Items.Objects[Index]) ) then
      Canvas.TextOut(Rect.Left + 3, Rect.Top + (((Rect.Bottom - Rect.Top) div 2) -
        (Canvas.TextHeight('Wg') div 2)), Items.Strings[Index])
    else
    begin
      Canvas.Pen.Color := clSilver;
      Canvas.Pen.Width := 1;
      Canvas.Pen.Style := psSolid;
      Canvas.MoveTo(Rect.Left + 3, Rect.Top + ((Rect.Bottom - Rect.Top) div 2));
      Canvas.LineTo(Rect.Right - 3, Rect.Top + ((Rect.Bottom - Rect.Top) div 2));
    end;
    

    It helps me alot when I can see how the class can be used. So for others I added an example on how to use it:

    uses
      Forms, o_comboboxplus;
    
    var
     fComboPlus: TComboBoxPlus;
    
    begin
      fComboPlus := TComboBoxPlus.Create(Form1);
      with(fComboPlus) do
      begin
        Parent := Form1;
        Left := 10;
        Top := 10;
        Items.Add('Test1');
        Items.Add('Test2');
        Items.Add('Test3');
        Items.Add('Test4');
        Enabled[2] := false;    //'Test3' will become a line seperator
      end;
    end;
    
    0 讨论(0)
  • 2021-01-13 11:56

    It's after midnight I'm tired - sorry about my stupidity. It's working with the following modifications:

    procedure WndProc(var Message: TMessage); override;
    procedure ListWndProc(var Message: TMessage);
    procedure DrawItem(Index: Integer; Rect: TRect;
      State: TOwnerDrawState); override;
    

    (add two overrides and take out the virtual)

    The last thing to sort out is not to let the combobox close up if the disabled item is selected without keyboard keys!

    0 讨论(0)
提交回复
热议问题