源码如下:谢谢大家参与。
type
Twriteimage= class(Timage)
private
oldx,oldy: integer;//记录鼠标上一次的位置
blDown: bool; //记录用户是否按下鼠标
distance: integer; //记录鼠标的移动距离
stream: TMemoryStream; //记录手写数据
procedure ImageMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
procedure ImageMouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
procedure ImageMouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
public
blWrite: bool; //记录用户是否已书写完毕
constructor create(Aowner:tcomponent);override;
end;
Thandwrite = class(TPanel)
private
font: TFont;
imgWrite: TwriteImage;
btClear: TButton;
btOk: TButton;
procedure btClearClick(Sender: TObject);
procedure btOkClick(Sender: TObject);
procedure handeriteresize(Sender:Tobject);
public
streamdata: TMemoryStream;
constructor create(Aowner:Tcomponent);override;
end;
type
TfrmPen = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
frmPen: TfrmPen;
implementation
{$R *.DFM}
procedure Thandwrite.btClearClick(Sender: TObject);
begin
imgWrite.Canvas.FillRect(Rect(imgWrite.Left,imgWrite.Top,imgWrite.Width,imgWrite.Height));
imgWrite.blWrite:=false;
end;
procedure Thandwrite.btOkClick(Sender: TObject);
begin
ImgWrite.blWrite:=true;
ImgWrite.Picture.Bitmap.SaveToStream(imgwrite.stream);
streamdata:=imgwrite.stream;
end;
procedure Thandwrite.handeriteresize(Sender:Tobject);
var
comcount,i:integer;
havebutton:boolean;
begin
havebutton:=false;
comcount:=self.ControlCount;
for i:=0 to comcount-1 do
begin
if self.Controls is Timage then
begin
Timage(self.Controls).width:=self.width-100;
Timage(self.Controls).height:=self.height;
end;
if self.controls is Tbutton then
begin
if not havebutton then
begin
Tbutton(self.controls).left:=self.Width-90;
Tbutton(self.controls).top:=trunc(self.height*0.25)-trunc(self.controls.height*0.5);
end else begin
Tbutton(self.controls).left:=self.Width-90;
Tbutton(self.controls).top:=trunc(self.height*0.75)-trunc(self.controls.height*0.5);
end;
havebutton:=true;
end;
end;
end;
constructor THandwrite.create(Aowner:Tcomponent);
begin
inherited;
onResize:=handeriteresize;
font:=TFont.Create;
font.Charset:=GB2312_CHARSET;
font.Size:=12;
caption:=' ';
imgWrite:=TwriteImage.Create(self);
with imgwrite do
begin
parent:=self; //imgWrite.CreateParentedControl(Twincontrol(Aowner).handle);
blWrite:=false; //用户未书写完毕
imgWrite.stream:=TMemoryStream.create;
streamdata:=TMemoryStream.Create;
end;
btClear:=TButton.create(self);
with btClear do
begin
parent:=self;//CreateParentedControl(Twincontrol(self).handle);
width:=75;
height:=25;
Font:=font;
top:=self.Top+20;
left:=self.width-90;
caption:='重写';
OnClick:=btClearClick;
end;
btOk:=TButton.create(self);
with btOK do
begin
parent:=self;// CreateParentedControl(Twincontrol(self).handle);
width:=75;
height:=25;
Font:=font;
top:=btClear.Top+50;
left:=btClear.Left;
top:=self.Top+100;
left:=self.width-90;
caption:='确定';
OnClick:=btOkclick;
end;
end;
procedure Twriteimage.ImageMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
if blWrite=false then
begin
blDown:=true;
oldx:=x;
oldy:=y;
end;
end;
procedure Twriteimage.ImageMouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
begin
if blWrite=false then
begin
if blDown then
begin
distance:=(x-oldx)*(x-oldx)+(y-oldy)*(y-oldy);
with canvas do
begin
if distance<9 then distance:=12;
if trunc(10/ln(distance))=1 then pen.Width:=2
else pen.Width:=trunc(10/ln(distance));
moveto(oldx,oldy);
lineto(x,y);
end;
oldx:=x;
oldy:=y;
end;
end;
end;
procedure Twriteimage.ImageMouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
if blWrite=false then
blDown:=false;
end;
constructor Twriteimage.create(Aowner:tcomponent);
begin
inherited;
onmousedown:=ImageMouseDown;
onmousemove:=ImageMouseMove;
onmouseup:=imageMouseUp;
end;
procedure TfrmPen.Button1Click(Sender: TObject);
begin
with Thandwrite.create(self) do
begin
parent:=self;
// width:=400;
// height:=300;
end;
end;