如题?
var FreeButton:TButton;
begin
FreeButton := TButton(FindComponent('Test1000'));
if freebutton<>nil then
freebutton.Free;
end;这个button动态的画在了一个panel上的
var FreeButton:TButton;
begin
FreeButton := TButton(FindComponent('Test1000'));
if freebutton<>nil then
freebutton.Free;
end;这个button动态的画在了一个panel上的
begin
FreeButton := TButton(YourPanel.FindChildControl('Test1000'));
if freebutton <>nil then
freebutton.Free;
end;
1.在删除这些动态控件上老是出问题,不知道怎么改?
2.自定义的一些控件,比如继承自Tgraphiccontrol新建的时候没有画出来?
下面是源程序
unit Unit1;interfaceuses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
ExtCtrls, StdCtrls, Buttons;type TForm1 = class(TForm)
procedure FormPaint(Sender: TObject);
procedure BitBtn1Click(Sender: TObject);
procedure FormMouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
procedure FormMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
procedure FormResize(Sender: TObject);
private
bInsert : Boolean;
procedure DrawBackground;
{ Private declarations }
public
{ Public declarations }
end; TA = class(TPanel)
private
FDown :Boolean;
FOldX : TPoint;
FOldY : TPoint;
ShapeList : Array[1..8] of TShape; FRectList: array[1..8] of TRect;
FPosList: array[1..8] of Integer;
procedure MouseDown(Button: TMouseButton; Shift: TShiftState;
X, Y: Integer); override;
procedure MouseMove(Shift: TShiftState; X, Y: Integer); override;
procedure MouseUp(Button: TMouseButton; Shift: TShiftState;
X, Y: Integer); override;
procedure CMExit(var Message: TCMExit); message CM_EXIT;
procedure aaa(var a :TWMERASEBKGND); message WM_ERASEBKGND;
procedure WmNcHitTest(var Msg: TWmNcHitTest); message wm_NcHitTest;
procedure WmSize(var Msg: TWmSize); message wm_Size;
procedure WmLButtonDown(var Msg: TWmLButtonDown); message wm_LButtonDown;
procedure WmMove(var Msg: TWmMove); message Wm_Move;
public
FCanvas : TCanvas;
FControl : TControl;
procedure Paint; override; constructor Create(AOwner: TComponent); override;
end;var
Form1: TForm1;implementationuses Unit2, Types;{$R *.DFM}{ TA }procedure TA.WmNcHitTest(var Msg: TWmNcHitTest);
var
Pt: TPoint;
I: Integer;
begin Pt := Point(Msg.XPos, Msg.YPos);
Pt := ScreenToClient(Pt);
Msg.Result := 0;
//??¹«?¦ì¸m¦}§ï???
for I := 1 to 8 do
if PtInRect(FRectList[I], Pt) then
Msg.Result := FPosList[I];
if Msg.Result = 0 then
inherited;
end;
procedure TA.WmSize(var Msg: TWmSize);
var
R: TRect;
begin
// R := BoundsRect;
// InflateRect(R, -2, -2);
// FControl.BoundsRect := R;
//?ºâ8?¶Â¤è®Ø
FRectList[1] := Rect(0, 0, 5, 5);
FRectList[2] := Rect(Width div 2 - 3, 0, Width div 2 + 2, 5);
FRectList[3] := Rect(Width - 5, 0, Width, 5);
FRectList[4] := Rect(Width - 5, height div 2 - 3, Width, Height div 2 + 2);
FRectList[5] := Rect(Width - 5, Height - 5, Width, Height);
FRectList[6] := Rect(Width div 2 - 3, Height - 5, Width div 2 + 2, Height);
FRectList[7] := Rect(0, Height - 5, 5, Height);
FRectList[8] := Rect(0, Height div 2 - 3, 5, Height div 2 + 2);
Paint;end;
procedure TA.WmLButtonDown(var Msg: TWmLButtonDown);
begin
//?¦æ©ì?©R¥O
// Perform(Wm_SysCommand, $F012, 0); inherited;end;procedure TA.WmMove(var Msg: TWmMove);
var
R: TRect;
begin
R := BoundsRect;
InflateRect(R, -2, -2);
// FControl.Invalidate;
// FControl.BoundsRect := R; Paint;
end;
constructor TA.Create(AOwner: TComponent);
var I : integer;
begin inherited;
FullRepaint := False;
FPosList[1] := htTopLeft;
FPosList[2] := htTop;
FPosList[3] := htTopRight;
FPosList[4] := htRight;
FPosList[5] := htBottomRight;
FPosList[6] := htBottom;
FPosList[7] := htBottomLeft;
FPosList[8] := htLeft;
for I := 1 to 8 do
begin
ShapeList[I] := Tshape.Create(Self);
ShapeList[I].Parent := Self;
ShapeList[I].Brush.Color := clBlack;
ShapeList[I].Visible := False;
end;
end;procedure TA.MouseDown(Button: TMouseButton; Shift: TShiftState; X,
Y: Integer);
begin
inherited; FDown := True;
FOldX := Point(X,Y);
SetFocus;
Paint;
end;procedure TA.MouseMove(Shift: TShiftState; X, Y: Integer);
var NewPoint : TPoint; function findnearest(X,Y:Integer):TPoint;
begin
Result.X := (X div 5) * 5 + Round((X mod 5)/5)*5;
Result.Y := (Y div 5) * 5 + Round((Y mod 5)/5)*5;
end;
begin
inherited;
if FDown
then
begin
NewPoint := findnearest(Left + X- FOldX.x,Top + Y- FOldX.Y);
with Self do
SetBounds(NewPoint.x, NewPoint.Y , Width, Height);
Paint;
end;
end;procedure TA.MouseUp(Button: TMouseButton; Shift: TShiftState; X,
Y: Integer);
begin
inherited;
FDown := False;
end;procedure TA.Paint;
var
I: Integer;
begin
inherited;
// for I := 1 to 8 do
// begin
// ShapeList[I].Left := FRectList[I].Left;
// ShapeList[I].Top := FRectList[I].Top;
// ShapeList[I].Width := FRectList[I].Right - FRectList[I].Left;
// ShapeList[I].Height := FRectList[I].Bottom - FRectList[I].Top;
// ShapeList[I].BringToFront;
// ShapeList[I].Update;
// end;
FControl.Width := BoundsRect.right - BoundsRect.Left- 10;
FControl.Height := BoundsRect.Bottom - BoundsRect.Top - 10;
Canvas.Lock;
TWinControl(FControl).PaintTo(Canvas.Handle,5,5);
Canvas.Unlock;
if Focused then
begin
Canvas.Brush.Color := clBlack;
Canvas.Pen.Color := clblack;
end else
begin
Canvas.Brush.Color := Self.Color;
Canvas.Pen.Color := Self.Color;
end;
// if Focused then
for I := 1 to 8 do
with FRectList[I] do
Canvas.Rectangle(Left, Top, Right, Bottom);
end;
procedure TForm1.DrawBackground;
var FScale : integer;
FGridX :double;
FGridY :double; procedure Line(x, y, x1, y1: Integer);
begin
Canvas.MoveTo(x, y);
Canvas.LineTo(x1, y1);
end; procedure DrawPoints;
var
GridBmp: TBitmap;
i: Extended;
c: TColor;
dx, dy: Extended;
begin
//if FGridType = gtDialog then
// c := clBlack else
c := clBlack;
FScale := 1;
FGridX := 5; // 96 / 25.4
FGridY := 5; // 96 / 25.4
dx := FGridX * FScale;
dy := FGridY * FScale;
if (dx > 2) and (dy > 2) then
begin
GridBmp := TBitmap.Create;
GridBmp.Width:= Width;
GridBmp.Height := 1; GridBmp.Canvas.Pen.Color := clBtnFace;
GridBmp.Canvas.MoveTo(0, 0);
GridBmp.Canvas.LineTo(Width, 0); i := 0;
while i < Width do
begin
GridBmp.Canvas.Pixels[Round(i), 0] := c;
i := i + dx;
end; i := 0;
while i < Height do
begin
Canvas.Draw(0, Round(i), GridBmp);
i := i + dy;
end; GridBmp.Free;
end;
end;begin
// Canvas.Brush.Color := clBtnFace;
// Canvas.Brush.Style := bsSolid;
// Canvas.FillRect(Rect(0, 0, Width, Height));
DrawPoints;end;procedure TForm1.FormPaint(Sender: TObject);
var I : Integer;
begin
DrawBackground;
// for I := 0 to Self.ComponentCount - 1 do
// begin
// if Self.Components[I] is TA then
// TA(Self.Components[I]).Paint;
// end;
end;procedure TForm1.BitBtn1Click(Sender: TObject);
begin
//Self.Canvas.CopyRect(Rect(10,10,CheckBox1.BoundsRect.Right - CheckBox1.BoundsRect.Left + 10 ,
// CheckBox1.BoundsRect.Bottom - CheckBox1.BoundsRect.Top + 10),Self.Canvas,CheckBox1.BoundsRect)
end;procedure TForm1.FormMouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
begin
if Form2.SpeedButton8.down then
Cursor := crDefault else
Cursor := crCross;
end;
Shift: TShiftState; X, Y: Integer);
var AControl : TControl;
ARect : TRect;begin if Cursor = crCross then
begin
with TA.Create(Form2) do
begin if Form2.SpeedButton1.Down then
begin
FControl := TCheckBox.Create(Self);
end
else
if Form2.SpeedButton2.Down then
begin
FControl := TButton.Create(Self);
end
else
if Form2.SpeedButton3.Down then
begin
FControl := tPanel.Create(Self);
tPanel(FControl).Alignment := taLeftJustify;
tPanel(FControl).BevelOuter := bvNone;
end else
if Form2.SpeedButton4.Down then
begin
FControl := tEdit.Create(Self);
end else
if Form2.SpeedButton5.Down then
begin
FControl := tMemo.Create(Self);
end else
if Form2.SpeedButton6.Down then
begin
FControl := tPanel.Create(Self);
end else
if Form2.SpeedButton7.Down then
begin
FControl := tComboBox.Create(Self);
end;
//FControl := TEdit.Create(Self);
FControl.Visible := False;
FControl.Parent := Form1;
AControl := FControl;
AControl. Left := -10000;
FControl.Visible := True;
// TEdit(AControl).Text := 'dtdddd';
AControl.Name := 'Test' + Inttostr(Random(100000)); ARect := AControl.BoundsRect;
InflateRect(ARect,5,5);
//SetBounds(AControl.BoundsRect .Left -20,AControl.BoundsRect.Top - 20,
// AControl.BoundsRect.Right - AControl.BoundsRect.Left + 40,
// AControl.BoundsRect.Bottom - AControl.BoundsRect.Top + 40);
BoundsRect := ARect;
//flateRect(ClientRect,2,2);
ShowHint := True;
Hint := Acontrol.Name;
Left := x;
Top := Y;
BevelOuter := bvNone;
Parent := Self;
Self.Cursor := crDefault;
SetFocus;
end;
end;
end;
procedure TA.aaa(var a: TWMERASEBKGND);
beginend;
procedure TA.CMExit(var Message: TCMExit);
begin
Inherited;
Paint;
end;procedure TForm1.FormResize(Sender: TObject);
begin
DrawBackground;
end;end.unit Unit2;interfaceuses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, Buttons, ExtCtrls;type
TForm2 = class(TForm)
CheckBox1: TCheckBox;
SpeedButton1: TSpeedButton;
Button1: TButton;
SpeedButton2: TSpeedButton;
Label1: TLabel;
SpeedButton3: TSpeedButton;
Edit1: TEdit;
Memo1: TMemo;
Panel1: TPanel;
ComboBox1: TComboBox;
SpeedButton4: TSpeedButton;
SpeedButton5: TSpeedButton;
SpeedButton6: TSpeedButton;
SpeedButton7: TSpeedButton;
SpeedButton8: TSpeedButton;
private
{ Private declarations }
public
{ Public declarations }
end;var
Form2: TForm2;implementation{$R *.DFM}end.
procedure TForm1.FormCreate(Sender: TObject);
begin
with TButton.Create(Panel1) do begin{注意Owner=Panel1}
Parent := Panel1;
Left := 50;
Top := 5;
Name := 'BtnTest';
Show;
end;
end;procedure TForm1.Button1Click(Sender: TObject);
var
P : Pointer;
begin
P := Panel1.FindComponent('BtnTest');
if Assigned(P) then FreeAndNil(P);
end;
但楼主测试失败,我想,问题应该出在Create时,Create(nil)如果Create时Owner=nil,可以这样变通一下:
procedure TForm1.FormCreate(Sender: TObject);
begin
with TButton.Create(nil) do begin{注意Owner=nil}
Parent := Panel1;
Left := 50;
Top := 5;
Name := 'BtnTest';
Show;
end;
end;procedure TForm1.Button1Click(Sender: TObject);
var
P : Pointer;
begin
{注意,下面的BtnTest是它的Text而非Name}
P := FindControl(FindWindowEx(Panel1.Handle, 0, 'TButton', 'BtnTest'));
if Assigned(P) then FreeAndNil(P);
end;
不过对于贴出源码后,大家都没有说到点子上,很遗憾
1.纠正你的说法:你是使用FindComponent来取得已存在的控件,并非如你标题所言"动态创建的控件".
2.纠正你的做法:只有是你自己创建的控件才能去free,要有始有终;否则不要像你的示例代码那样去free,那样会引起未知错误,系统紊乱.
TWinControl(FControl).PaintTo(Canvas.Handle,5,5);
Canvas.Unlock;
这行代码在Paint里的就有明确控件继承自TWinControl,所以Tgraphiccontrol没有画出来,它没有TWinControl的PaintTo