Delphi以其優秀的界面和簡單的用法深受廣大程序員的喜愛.筆者經過摸索,自做了一個具有動態顯示特性的控件。只需在主程序中調用該控件的一個方法即可實現動態顯示。在動態顯示的同時,為了不影響主程序做其他的事情,筆者采用了比較流行的線程技術。
一. 方案
自做一個父類為TEdit的控件,應該有一個Text屬性,能自由地輸入要動態顯示的內容; 并且有一個MoveShow方法,使的Text的內容能動態的顯示。在主程序中創建一個線程,啟動線程時,調用該控件的MoveShow方法。
二. 制作控件 啟動New Component,選Tedit為父類,建立L_Tedit1類,并創建L_edit.pas. 再編寫L_edit.pas 如下:
unit L_Edit; interface uses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls;
type L_TEdit1 = class(TEdit) private { Private declarations } protected { Protected declarations } public { Public declarations } constructor Create(AOwner:TComponent); override; procedure MoveShow; published { Published declarations } property Text; end;
procedure Register;
implementation constructor L_TEdit1.Create(AOwner:TComponent); begin inherited create(aowner); color:=clblue; font.Color:=clyellow; font.Size:=12; font.Name:= '@仿宋_GB2312'; tabstop:=false; update; end;
procedure L_TEdit1.MoveShow; var edit_length,i:integer; edit_char:char; chars: string; begin chars:=''; if (length(text)=0) then text:=’Welcom you to use the software!’; edit_length:=length(text); for i:=1 to edit_length do begin edit_char:=text[1]; if (Ord(edit_char) >127) then if length(chars) >1 then begin text:=copy(text,2,edit_length-2)+chars; chars:=''; end else begin chars:=copy(text,1,2); text:=copy(text,2,edit_length-1); end else begin text:=copy(text,2,edit_length-1)+edit_char; end; update; sleep(100); end; end;
procedure Register; begin RegisterComponents('Samples', [L_TEdit1]); end;
end. 再保存該文件。 啟動Image Editor 創建L_Edit.dcr , 選New- >Bitmap,自己做一個圖標,保存名為L_TEDIT1(與新建的類同名)。注意L_Edit.dcr 與L_Edit.pas 要在同一個目錄中(缺省為\delphi\lib目錄中。再單擊Install Component. 選Into new package屬性頁,填上L_Edit.pas 的路徑和文件名,并在該路徑下新建L_Edit1.dpk 文件。之后一直單擊OK即可。此時我們可以在Delphi 的工具欄Sample 一項中看到自己創建的圖標。
三. 編寫主程序
在主窗體Form1中放一自己創建的控件,在Text的屬性中填上要顯示的文字(中英文都可)。與該窗體對應的L_unit1.pas內容如下:
unit L_Unit1; interface uses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, L_Edit;
type Tmythread=class(TThread) protected procedure Execute; override; end; TForm1 = class(TForm) L_TEdit11: L_TEdit1; Button1: TButton; procedure FormCreate(Sender: TObject); procedure Button1Click(Sender: TObject); private { Private declarations } public { Public declarations } end;
var Form1: TForm1; MyThread1:TMyThread; implementation
{$R *.DFM} Procedure TMyThread.Execute; begin while true do form1.L_TEdit11.MoveShow; end;
procedure TForm1.FormCreate(Sender: TObject); begin MyThread1:=TMyThread.Create(false); end;
procedure TForm1.Button1Click(Sender: TObject); begin showmessage('Welcome You!'); end;
end.
|