delphi中有没有类似控制台那样的控件,可以处理每个字符的字体颜色和背景色?
我现在暂时用控制台实现了这个功能,但是界面上看起来很别扭,因为控制台总是弹出一个独立的窗口,有没有办法将控制台嵌入到窗体上?
大家帮我想想办法,谢谢!

解决方案 »

  1.   

    理论上是可行的
    1.利用控制台程序输入输出重定向,将控制台的输入输出重定向到管道
    2.将一可显示控件将输入写入管道,并将管道的输出显示
    如果要显示个每字符的字体颜色和背景色,可直接用TImage画出来(以前我用BCB写的模拟器就是这样做的)
    但具体要实现有相当的工作量
      

  2.   

    照楼上的说法,用个Memo1就可虚拟出来,
    如果要原色的,用RichEdit之类,也是同样的道理
      

  3.   

    不行啊,你是不是要改变每个字体的颜色啊,调用Word好了!!!!!!!!!!!!!!!!!!
      

  4.   

    仅供参考
       
    请参看下面的代码:procedure TForm1.Button1Click(Sender: TObject);vars: string;beginAllocConsole;tryWrite('Type here your words and press ENTER: ');Readln(s);ShowMessage(Format('You typed: "%s"', [s]));finallyFreeConsole;end;end;
     
      

  5.   

    其实我用控制台就是用来显示的,不涉及数据的传输(那样会影响通信),就是要对每个字符都可以
    设置字体色和背景色,用控制台是不行了,现在正在试验在RichEdit上扩展一些功能来实现,不知
    大家有没有这方面的好的建议???
    欢迎讨论!
      

  6.   

    至于 tengfeng8888() 所说的调用word,太占资源了...
      

  7.   

    给你一个例子,是我在别人程序上改的,可通过在输入时用[n]来改颜色(如[3]dir可在RichEdit1中用红色列出当前目录),另外你可通过返回字串(如ANSI的ESC系列)来修改 RichEdit1的颜色以达到你的要求unit ufmMain;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls, ExtCtrls, ComCtrls,StrUtils;type
      TForm1 = class(TForm)
        Timer1: TTimer;
        Button1: TButton;
        cbCMD: TComboBox;
        RichEdit1: TRichEdit;
        procedure Timer1Timer(Sender: TObject);
        procedure FormShow(Sender: TObject);
        procedure FormClose(Sender: TObject; var Action: TCloseAction);
        procedure Button1Click(Sender: TObject);
        procedure edCmdKeyDown(Sender: TObject; var Key: Word;
          Shift: TShiftState);
      private
        ReadOut, WriteOut: THandle;
        ReadIn, WriteIn: THandle;
        ProcessInfo: TProcessInformation;
        procedure InitConsole;
        function ReadFromPipe(Pipe: THandle): string;
        procedure WriteToPipe(Pipe: THandle; Value: string);
        procedure CloseConsole;
      Private
        { Private declarations }
          cl:integer;
      public
        { Public declarations }
      end;var
      Form1: TForm1;const
      ReadBuffer = 2400;implementation{$R *.dfm}procedure TForm1.InitConsole;
    var
      Security: TSecurityAttributes;
      start: TStartUpInfo;
    begin
      with Security do begin
        nlength := SizeOf(TSecurityAttributes);
        binherithandle := true;
        lpsecuritydescriptor := nil;
      end;  Createpipe(ReadOut, WriteOut, @Security, 0);
      Createpipe(ReadIn, WriteIn, @Security, 0);  with Security do begin
        nlength := SizeOf(TSecurityAttributes);
        binherithandle := true;
        lpsecuritydescriptor := nil;
      end;  FillChar(Start, Sizeof(Start), #0);
      start.cb := SizeOf(start);
      start.hStdOutput := WriteOut;
      start.hStdInput := ReadIn;
      start.hStdError := WriteOut;
      start.dwFlags := STARTF_USESTDHANDLES +
        STARTF_USESHOWWINDOW;
      start.wShowWindow := SW_HIDE;  CreateProcess(nil,
        PChar('cmd'),
        @Security,
        @Security,
        true,
        NORMAL_PRIORITY_CLASS,
        nil,
        nil,
        start,
        ProcessInfo)
    end;function TForm1.ReadFromPipe(Pipe: THandle): string;
    var
      Buffer: PChar;
      BytesRead: DWord;
    begin
      Result := '';
      if GetFileSize(Pipe, nil) = 0 then Exit;  Buffer := AllocMem(ReadBuffer + 1);
      repeat
        BytesRead := 0;
        ReadFile(Pipe, Buffer[0],
          ReadBuffer, BytesRead, nil);
        if BytesRead > 0 then begin
          Buffer[BytesRead] := #0;
          OemToAnsi(Buffer, Buffer);
          Result := string(Buffer);
        end;
      until (BytesRead < ReadBuffer);
      FreeMem(Buffer);
    end;procedure TForm1.Timer1Timer(Sender: TObject);
    var
      s: string;
    begin
      s := ReadFromPipe(ReadOut);
      if s <> '' then begin
        RichEdit1.SelAttributes.Color:=cl; //你还可以通过返回串改颜色
        RichEdit1.Lines.Add(s);
      end;
    end;procedure TForm1.WriteToPipe(Pipe: THandle; Value: string);
    var
      len: integer;
      BytesWrite: DWord;
      Buffer: PChar;
    begin
      len := Length(Value) + 1 ;
      Buffer := PChar(Value +#10);
      WriteFile(Pipe, Buffer[0], len, BytesWrite, nil);
    end;procedure TForm1.FormShow(Sender: TObject);
    begin
      self.OnShow := nil;
      cbCmd.Clear;
      cbCmd.SetFocus;  InitConsole;  Timer1.Enabled := True;
    end;procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
    begin
      Timer1.Enabled := False;
      CloseConsole;
    end;procedure TForm1.CloseConsole;
    begin
      CloseHandle(ProcessInfo.hProcess);
      CloseHandle(ProcessInfo.hThread);  CloseHandle(ReadIn);
      CloseHandle(WriteIn);  CloseHandle(ReadOut);
      CloseHandle(WriteOut);
    end;procedure TForm1.Button1Click(Sender: TObject);
    var
      s:string;
    begin
      s:=Trim(cbCmd.Text);
      if (length(s)>=3) and (s[1]='[') and (s[3]=']') and (s[2]>='0') and (s[2]<='4') then
      begin
        case StrToInt(s[2]) of
        0:
          cl:=clBlack;
        1:
          cl:=clBlue;
        2:
          cl:=clGreen;
        3:
          cl:=clRed;
        4:
          cl:=clYellow;
        end;
        s:=RightStr(s,length(s)-3);
      end ;
      if  s<> '' then begin
        WriteToPipe(WriteIn, s);
        if cbCMD.ItemIndex > -1 then
          cbCMD.Items.Delete(cbCMD.ItemIndex);
        cbcmd.Items.Insert(0, cbCmd.Text);
        cbCmd.Text:='';
      end;
    end;procedure TForm1.edCmdKeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    begin
      if Key = VK_RETURN then Button1.Click;
    end;end.
      

  8.   

    注:
      Form上有四个控件,分别为:
        Timer1: TTimer;
        Button1: TButton;
        cbCMD: TComboBox;
        RichEdit1: TRichEdit;