德尔福的自定义组合框下拉列表即将关闭

时间:2012-02-09 19:49:07

标签: delphi combobox richedit

我一直在寻找一种在默认组合框中使用RichtText的简单方法,但一无所获。 所以我写了这个小的Delphi(7)组件,到目前为止一直在工作。

工作原理: 我正在调用“init”来替换默认组合框中的“编辑”窗口 运行时创建的RichEdit。大小取自编辑,最后隐藏编辑。 包括一些事件处理程序用于更改检测等。

问题: 如果单击下拉列表中的项目,则文本将显示在RichEdit中。 如果在RichEdit中输入了一些文本并再次按下拉按钮, 下拉列表在下一刻打开和关闭。点击一下后,列表 仍然开放,并按预期工作。 每次单击列表并再次更改RichEdit时,都会发生同样的情况。

也许我必须向组合框发送一些消息才能解决问题?

到目前为止,我在网上找不到任何解决方案。也许你有个主意。

感谢您的帮助!

unit RichTextComboBox;

interface

uses  SysUtils, Classes, Controls, StdCtrls, Windows, Messages, forms, Graphics, ComCtrls;

type
    TRichTextComboBox = class(TComboBox)
    private
        FOnChange     :TNotifyEvent;
        EditHandle :Integer;
        procedure proc_FOnComboChange(Sender: TObject);
    protected
    public
        Rich :TRichEdit;             // accessable from outside
        constructor Create(AOwner: TComponent); override;
        destructor  Destroy; override;
        procedure   Init;            // replace Edit in combobox with RichEdit
    published
    end;

procedure Register;

implementation



constructor TRichTextComboBox.Create(AOwner: TComponent);
begin
    inherited Create(AOwner);
end;


// click in Combo-Drop-Down-List
procedure TRichTextComboBox.proc_FOnComboChange(Sender :TObject);
begin
    if Rich.Text <> Items.Strings[ItemIndex] then begin
        Rich.Text:=  Items.Strings[ItemIndex];
    end;
    if assigned (FOnChange) then FOnChange(sender);
end;


procedure Register;
begin
    RegisterComponents('TEST', [tRichTextComboBox]);
end;


destructor TRichTextComboBox.Destroy;
begin
    if Rich <> nil then begin
        RemoveControl(rich);
        Rich.destroy;
    end;
    inherited Destroy;
end;


// Replace "Edit" with "RichEdit" in ComboBox
//
procedure TRichTextComboBox.init;
var       h      :integer;
          rect   :trect;
          wndpos :TWindowPlacement;
begin
    h:= FindWindowEx(
        self.Handle,
        0,              // handle to a child window
        'Edit',         // class name
        nil
    );

    Rich:= TRichEdit.create(self);
    rich.Parent:= self;

    if h <> 0 then begin
        EditHandle:= h;
        GetWindowRect(h, rect);

        // configure RichEdit

        GetWindowPlacement(h, @wndpos);        // RichEdit with position and size of Edit
        rich.BorderStyle:= bsNone;
        rich.Text:= self.Text;
        rich.Font.Style:= [fsbold, fsItalic];
        rich.Top:=   wndpos.rcNormalPosition.top;
        rich.Left:=  wndpos.rcNormalPosition.Left;
        rich.Width:= rect.Right - rect.Left;
        rich.Height:= rect.Bottom-rect.Top;
        rich.WantReturns:= false;              // just one line
        rich.WordWrap:= false;                 // just one line
        rich.ParentColor:= true;               // just one line
        rich.Visible:= true;
        showwindow(h, sw_hide);                // hide Edit
    end;

    // if drop-down-combo-list is clicked
    // change the string of the RichEdit
    FOnChange:=     self.OnChange;             // save original OnChange of ComboBox
    rich.OnChange:= FOnChange;
    self.OnChange:= proc_FOnComboChange;
end;

end.

1 个答案:

答案 0 :(得分:0)

最后我找到了解决方案: - )

RichEdit持有Focus,导致下拉列表在进入s.th后不再保持打开状态。在RichEdit中。 此过程在打开之前将Focus设置回Combobox。所以一切都按预期工作。


要插入的代码:

protected输入后

procedure DropDown; override;

程序如下:

procedure TRichTextComboBox.DropDown;
begin
  Self.SetFocus;
  inherited DropDown;
end;

我更喜欢这种方法,因为我不想弄乱我们可以在许多页面上阅读的OwnerDraw问题。 (有些事情仍然缺失:Upkey / Downkey ......)