16,748
社区成员
发帖
与我相关
我的任务
分享
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
procedure FormMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
private
public
function CreateButton(const x, y: integer): TButton;
procedure ButtonRightClick(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
function TForm1.CreateButton(const x, y: integer): TButton;
begin
result := TButton.Create(nil);
result.Parent := self;
result.Left := x;
result.Top := y;
result.OnMouseDown := ButtonRightClick;
end;
procedure TForm1.FormMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
if button = mbLeft then
CreateButton(x, y);
end;
procedure TForm1.ButtonRightClick(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
if (button = mbRight) and (Sender is TButton) then
FreeAndNil(Sender);
end;
end.
public
procedure MyMouseDown(Sender: TObject; Button: TMouseButton;Shift: TShiftState; X, Y: Integer); //注意参数要和原有事件过程一样
procedure TForm1.MyMouseDown(Sender: TObject; Button: TMouseButton;Shift: TShiftState; X, Y: Integer);
begin
if Button=mbLeft then
begin
(Sender as TEdit).Free;
end;
end;
procedure TForm1.FormMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
//创建TEdit的对象实例,之后加上如下代码
Object.OnMouseDown:=MyMouseDown; //自定义一个Mousedown事件,创建组件后,把组件的mousedown事件指向你自定义的事件,然后在自定义的事件中处理删除
end;
type
TControlHack = class(TControl);
begin
TControlHack(YourObject).OnMouseDown := MyMouseDown;
end;