如题

解决方案 »

  1.   

    先插入ComboBox,然后从ComboBox选上内容后,ComboBox消失。
    两种情况:
    1。开始显示ComboBox时,字符往后移
    2。ComboBOX消失,字符往后移
      

  2.   

    //参考如下代码~~
    unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls, ComCtrls;type
      TForm1 = class(TForm)
        RichEdit1: TRichEdit;
        ComboBox1: TComboBox;
        procedure RichEdit1KeyDown(Sender: TObject; var Key: Word;
          Shift: TShiftState);
        procedure ComboBox1Exit(Sender: TObject);
        procedure ComboBox1KeyDown(Sender: TObject; var Key: Word;
          Shift: TShiftState);
        procedure FormCreate(Sender: TObject);
      private
        { Private declarations }
        FSelStart: Integer;
        FSelLength: Integer;
      public
        { Public declarations }
      end;var
      Form1: TForm1;implementation{$R *.dfm}procedure TForm1.RichEdit1KeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    var
      vPoint: TPoint;
    begin
      case Key of
        VK_INSERT: begin
          RichEdit1.Perform(EM_POSFROMCHAR, Integer(@vPoint), RichEdit1.SelStart);
          RichEdit1.SelLength := 0;
          FSelStart := RichEdit1.SelStart;
          ///////
          Canvas.Font.Assign(RichEdit1.SelAttributes);
          FSelLength := 1;
          while Canvas.TextWidth(StringOfChar(#32, FSelLength + 1)) < ComboBox1.Width do
            Inc(FSelLength);
          ///////
          RichEdit1.SelText := StringOfChar(#32, FSelLength);
          ComboBox1.Left := RichEdit1.Left + vPoint.X + 2;
          ComboBox1.Top := RichEdit1.Top + vPoint.Y + 1;
          ComboBox1.Visible := True;
          ComboBox1.SetFocus;
          ComboBox1.DroppedDown := True;
        end;
      end;
    end;procedure TForm1.ComboBox1Exit(Sender: TObject);
    begin
      RichEdit1.SelStart := FSelStart;
      RichEdit1.SelLength := FSelLength;
      RichEdit1.SelText := ComboBox1.Text;
      RichEdit1.ReadOnly := False;
      ComboBox1.Visible := False;
    end;procedure TForm1.ComboBox1KeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    begin
      case Key of
        VK_RETURN: RichEdit1.SetFocus;
        VK_ESCAPE: begin
          ComboBox1.Text := '';
          RichEdit1.SetFocus;
        end;
      end;
    end;procedure TForm1.FormCreate(Sender: TObject);
    begin
      ComboBox1.Visible := False;
    end;end.