发布一个显示点阵字的控件(100分)

L

LeonSu

Unregistered / Unconfirmed
GUEST, unregistred user!
Active是用来让字滚动的,暂时没写代码。
unit LeonSuMatrix;

interface

uses
Windows, Messages, SysUtils, Classes, Controls, Graphics, Dialogs, ExtCtrls, Math;

type
TLeonSuMatrix = class(TGraphicControl)
private
FActive: Boolean;
FText: String;
FImage: TBitmap;
FOffset_H: Integer;
FOffset_V: Integer;
procedure SetActive(Value: Boolean);
procedure SetText(Value: String);
procedure SetOffset_H(Value: Integer);
procedure SetOffset_V(Value: Integer);
{ Private declarations }
protected
{ Protected declarations }
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
procedure Paint; override;
{ Public declarations }
published
property Active: Boolean read FActive write SetActive default False;
property Color;//背景色
property Font;
property Offset_H: Integer read FOffset_H write SetOffset_H;
property Offset_V: Integer read FOffset_V write SetOffset_V;
property Text: String read FText write SetText;
{ Published declarations }
end;

const
BackColor=$007F7F7F;//背景的第二种颜色

implementation

constructor TLeonSuMatrix.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FActive:=False;
Color:=clBlack;
FImage:=TBitmap.Create;
Font.Color:=clYellow;
Font.Size:=12;
Font.Name:='宋体';
Offset_V:=1;
Offset_H:=1;
end;

destructor TLeonSuMatrix.Destroy;
begin
FImage.Destroy;
inherited Destroy;
end;

procedure TLeonSuMatrix.Paint;
Const
FScale=3;//放大倍数,最好为3,如果变大的话,会比较难看
var
i,j:Integer;
VOffSet,HOffset: Integer;
//VOffset:增加的高度,HOffset
trgRect,srcRect: TRect;
Bmp: TBitmap;
begin
Canvas.Lock;
Bmp:=TBitmap.Create;
FImage.Canvas.Font:=Font;
FImage.Canvas.Brush.Color:=Color;

VOffset:=FOffset_V*FScale;
HOffset:=FOffset_H*FScale;
Height:=Abs(Font.Height)*FScale+VOffset*2;

FImage.Width:=FImage.Canvas.TextWidth(FText);
FImage.Height:=FImage.Canvas.TextHeight(FText);
FImage.Canvas.TextOut(0,0,FText);

Bmp.Height:=Height;
Bmp.Width:=Width;
Bmp.Canvas.Brush.Color:=Color;
Bmp.Canvas.FillRect(Rect(0,0,Bmp.Width,Bmp.Height));

trgRect:=Rect(HOffset,VOffset-2,FImage.Width * FScale+HOffset,FImage.Height * FScale+Voffset-2);
srcRect:=Rect(0,0,FImage.Width,FImage.Height);

Bmp.Canvas.CopyRect(trgRect,FImage.Canvas,srcRect);
for i:=0 to Bmp.Width do
for j:=0 to Bmp.Height do
begin
If ((i mod FScale=1)and(j mod FScale=1))or((i mod FScale=2)and(j mod FScale=2)) then
begin
If Bmp.Canvas.Pixels[i,j]=Color then
Bmp.Canvas.Pixels[i,j]:=BackColor;
end
else
If (i mod FScale=0)or(j mod FScale=0) then
Bmp.Canvas.Pixels[i,j]:=Color;
end;
Canvas.Draw(0,0,BMP);
Bmp.Free;
Canvas.Unlock;
// inherited Paint;
end;

procedure TLeonSuMatrix.SetActive(Value: Boolean);
begin
If Value<>FActive then
begin
FActive:=Value;
end;
end;

procedure TLeonSuMatrix.SetText(Value: String);
begin
If Trim(Value)<>FText then
begin
FText:=Trim(Value);
Repaint;
// Invalidate;
end;
end;

procedure TLeonSuMatrix.SetOffset_V(Value: Integer);
begin
If Value<>FOffset_V then
begin
FOffset_V:=Value;
Repaint;
// Invalidate;
end;
end;

procedure TLeonSuMatrix.SetOffset_H(Value: Integer);
begin
If Value<>FOffset_H then
begin
FOffset_H:=Value;
Repaint;
// Invalidate;
end;
end;

end.
写这个控件完全是被迫的,当初想找一个,结果,未找到带源码的。
干脆自己写一个,花了一天时间,基本搞定了,速度还不错,比那些不带源码的快一点点。
是我自己的感觉,而且,界面自我感觉良好。
我自认为这个算法很独特,很有创意。
于是,发布上来,大家共享,如果有什么好的提议,可以来信交流。
leonsu@163.com
由于未开发完,所以,事件这些全部未加。
另外,Offset_V这个属性的作用我认为有点争议,理论上是指的纵轴偏移量,但我这里是
指上与下的空白值;Offset_H就是指的左边偏移。
 
试用。
怎样用?
procedure TForm1.Button1Click(Sender: TObject);
var
i: TLeonSuMatrix;
begin
i:=TLeonSuMatrix.Create(form1);
i.Left:=20;
i.Top:=30;
i.Parent:=form1;
i.Text:='测试';
i.Color:=clred;
i.Font.Size:=24;
i.Active:=True;
end;
没反应。
 
没反应![:(][:(][:(]
 
阿蛮,错怪人家啦,加上
i.Width :=100;
i.Height :=100;
就看见了,LeonSu也没注意这点,create后居然不给控件的高、宽赋默认值,写控件要
注意这点啊!
 
i.height:=100;
i.width:=400;
由于我在create事件中没有给height与width赋值,所以,动态创建时,要给他们赋值,
 
[:D]不错不错,但为什么不写滚动的代码呢?[^]
 
昨天搞得太晚了,所以没写,
准备今天补上。
 
大家说,如果改为从LABEL继承下来会好一些吗?
速度可以更快一点吗?
我试了一下,感觉差不多。
 
不錯!加油!!
 
多人接受答案了。
 
顶部