デフォルトのコンボボックスで RichtText を使用する簡単な方法を探していましたが、何も見つかりませんでした。そこで、この小さな Delphi(7) コンポーネントを書きましたが、これは今のところ機能しています。
仕組み: 「init」を呼び出して、デフォルトのコンボボックス内の「編集」ウィンドウをランタイムで作成されたリッチエディットに置き換えます。サイズは編集から取得され、編集は最終的に非表示になります。変更検出などのために、いくつかのイベント ハンドラーが含まれています。
問題: ドロップダウン リストの項目をクリックすると、テキストが RichEdit に表示されます。RichEdit 内に何らかのテキストが入力され、ドロップダウン ボタンが再度押されると、ドロップダウン リストが開かれ、次の瞬間に閉じられます。数回クリックした後、リストは開いたままになり、期待どおりに機能します。リストをクリックして RichEdit を再度変更するたびに、同じことが起こっています。
多分私はそれを修正するためにコンボボックスにいくつかのメッセージを送る必要がありますか?
これまでのところ、Web上で解決策は見つかりませんでした。多分あなたは考えを持っています。
ご協力いただきありがとうございます !
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.