delphi中有没有类似控制台那样的控件,可以处理每个字符的字体颜色和背景色?
我现在暂时用控制台实现了这个功能,但是界面上看起来很别扭,因为控制台总是弹出一个独立的窗口,有没有办法将控制台嵌入到窗体上?
大家帮我想想办法,谢谢!
我现在暂时用控制台实现了这个功能,但是界面上看起来很别扭,因为控制台总是弹出一个独立的窗口,有没有办法将控制台嵌入到窗体上?
大家帮我想想办法,谢谢!
解决方案 »
- dbgrid column实时模糊查询
- 恢复控件!!!
- 程序快开发完毕。怎么测试我的程序最少要消耗多少内存?在多快的CPU上跑才比较流畅?期望量化指标
- 寻求解决方法,数据插入
- vb.net中如何调用delphi中的oleVariant类型数据?在vb.net中调用delphi开发的ActiveX控件时,怎样用数组作为参数传递?
- 怎样算出某年某月的某天是星期几?
- 用Delphi的QuickReport动态制作--流水式报表
- 打印!
- 关于使用word的问题,请有经验的大虾帮帮忙
- 在Delphi中能使用宏吗?
- 请问谁有关于文件流内存流等资料教材/电子书呢?肯定给分!
- 读窗体时出错,如保添加class
1.利用控制台程序输入输出重定向,将控制台的输入输出重定向到管道
2.将一可显示控件将输入写入管道,并将管道的输出显示
如果要显示个每字符的字体颜色和背景色,可直接用TImage画出来(以前我用BCB写的模拟器就是这样做的)
但具体要实现有相当的工作量
如果要原色的,用RichEdit之类,也是同样的道理
请参看下面的代码: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;
设置字体色和背景色,用控制台是不行了,现在正在试验在RichEdit上扩展一些功能来实现,不知
大家有没有这方面的好的建议???
欢迎讨论!
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.
Form上有四个控件,分别为:
Timer1: TTimer;
Button1: TButton;
cbCMD: TComboBox;
RichEdit1: TRichEdit;