procedure TForm1.FormMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
if Button=mbLeft then
begin
isDown:=true;
hasMove:=false;
orgP:=Point(X,Y);
offsetP:=Point(X,Y);
self.Canvas.Rectangle(Rect(orgP,offsetP));
end;
if hasDone and assigned(EdtFont) then
begin
FreeAndNil(EdtFont);
hasDone:=false;
end;
end;
procedure TForm1.FormMouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
begin
if isDown then
begin
hasMove:=True;
self.Canvas.Rectangle(Rect(orgP,offsetP));
offsetP:=Point(X,Y);
self.Canvas.Rectangle(Rect(orgP,offsetP));
end;
end;
procedure TForm1.FormMouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
self.Canvas.Rectangle(Rect(orgP,offsetP));
isDown:=false;
hasDone:=true;
if hasMove then
begin
EdtFont:=TFontEdit.Create(self,self.Font);
EdtFont.Visible:=false;
EdtFont.Parent:=self;
EdtFont.Font:=Font;
EdtFont.Left:=OrgP.X;
EdtFont.Top:=OrgP.Y;
EdtFont.Width:=offsetP.X-OrgP.X;
EdtFont.Height:=offsetP.Y-OrgP.Y;
EdtFont.Visible:=true;
end
else
begin
EdtFont:=TFontEdit.Create(self,self.Font);
EdtFont.Visible:=false;
EdtFont.Parent:=self;
EdtFont.Font:=Font;
EdtFont.Left:=OrgP.X;
EdtFont.Top:=OrgP.Y;
EdtFont.Visible:=true;
end;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
self.Canvas.Pen.Style:=psDash;
self.Canvas.Pen.Mode:=pmNot;
self.Canvas.Brush.Style:=bsClear;
self.DoubleBuffered:=true;
end;
{ TFontEdit }
//移动和拉动编辑框的方法
procedure TFontEdit.ManipulateControl(WinControl: TWinControl; Shift: TShiftState;
X, Y: integer);
var SC_MANIPULATE: Word; isCan:Boolean;
begin
WinControl.Cursor := crDefault;
SC_MANIPULATE:=0;
isCan:=False;
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//光标在控件的最左侧**********************************************************
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
if (X<=MoveSize) and (Y>MoveSize) and (Y<WinControl.Height-MoveSize)
then begin
SC_MANIPULATE := $F001;
WinControl.Cursor := crSizeWE;
isCan:=True;
end
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//光标在控件的最右侧**********************************************************
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
else if (X>=WinControl.Width-MoveSize) and (Y>MoveSize) and (Y<WinControl.Height-MoveSize)
then begin
SC_MANIPULATE := $F002;
WinControl.Cursor := crSizeWE;
isCan:=True;
end
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//光标在控件的最上侧**********************************************************
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
else if (X>MoveSize) and (X<WinControl.Width-MoveSize) and (Y<=MoveSize)
then begin
SC_MANIPULATE := $F003;
WinControl.Cursor := crSizeNS;
isCan:=True;
end
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//光标在控件的左上角**********************************************************
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
else if (X<=MoveSize) and (Y<=MoveSize)
then begin
SC_MANIPULATE := $F009;
WinControl.Cursor := crSizeAll;
isCan:=True;
end
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//光标在控件的右上角**********************************************************
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
else if (X>=WinControl.Width-MoveSize) and (Y<=MoveSize)
then begin
SC_MANIPULATE := $F005;
WinControl.Cursor := crSizeNESW;
isCan:=True;
end
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//光标在控件的最下侧**********************************************************
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
else if (X>MoveSize) and (X<WinControl.Width-MoveSize) and (Y>=WinControl.Height-MoveSize)
then begin
SC_MANIPULATE := $F006;
WinControl.Cursor := crSizeNS;
isCan:=True;
end
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//光标在控件的左下角**********************************************************
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
else if (X<=MoveSize) and (Y>=WinControl.Height-MoveSize)
then begin
SC_MANIPULATE := $F007;
WinControl.Cursor := crSizeNESW;
isCan:=True;
end
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//光标在控件的右下角**********************************************************
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
else if (X>=WinControl.Width-MoveSize) and (Y>=WinControl.Height-MoveSize)
then begin
SC_MANIPULATE := $F008;
WinControl.Cursor := crSizeNWSE;
isCan:=True;
end;
if (Shift=[ssLeft])and(isCan=True)and(CanMove=true)then
begin
ReleaseCapture;
WinControl.Perform(WM_SYSCOMMAND, SC_MANIPULATE, 0);
end;
end;
//初始化参数
constructor TFontEdit.Create(AOwner: TComponent;Font:TFont);
begin
inherited Create(AOwner);
self.OnMouseMove:=FontEditMouseMove;
self.OnMouseDown:=FontEditMouseDown;
self.OnChange:=FontEditChange;
self.OnResize:=FontEditResize;
FFontHeight:=GetTextHeight(Font);
self.Height:=FFontHeight+2;
self.Width:=60;
MoveSize:=3;
end;
//设置编辑框的风格
procedure TFontEdit.CreateWnd;
begin
inherited;
self.Ctl3D:=false;
self.Constraints.MinHeight:=FFontHeight*(self.Lines.Count+1)+2;
self.Constraints.MinWidth:=60;
end;
//鼠标移动消息
procedure TFontEdit.FontEditMouseMove(Sender: TObject; Shift: TShiftState;
X, Y: Integer);
begin
ManipulateControl(self,Shift,X,Y);
end;
//判断鼠标是否在可移动范围内
procedure TFontEdit.FontEditMouseDown(Sender: TObject;
Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
if isInClient(X,Y) then
canMove:=false
else
canMove:=true;
end;
//判断鼠标是否在客户区内
function TFontEdit.isInClient(X,Y:Integer): Boolean;
begin
result:=false;
if (X>MoveSize)and(X<self.Width-MoveSize)
and(Y>MoveSize)and (Y<self.Height-MoveSize) then
result:=true;
end;
//取得一行文本的高度
function TFontEdit.GetTextHeight(Font:TFont): Integer;
var C:TCanvas; size:Tsize;
begin
result:=0;
C:=TCanvas.Create;
C.Handle:=GetDC(Application.Handle);
try
C.Font:=Font;
if GetTextExtentPoint32(C.Handle,'H',1,size)then
result:=size.cy;
finally
C.Free;
end;
end;
//文本改变时调整最小高度
procedure TFontEdit.FontEditChange(Sender: TObject);
begin
self.Constraints.MinHeight:=FFontHeight*(self.Lines.Count+1)+2;
end;
//当字体被改变时调用
procedure TFontEdit.CMFontChanged(var Message: TMessage);
begin
inherited;
FFontHeight:=GetTextHeight(self.Font);
self.Constraints.MinHeight:=FFontHeight*(self.Lines.Count+1)+2;
end;
//当改变大小时,调整最小高度
procedure TFontEdit.FontEditResize(Sender: TObject);
begin
self.Constraints.MinHeight:=FFontHeight*(self.Lines.Count+1)+2;
end;