delphi70实现控件工具箱功能可以拖控件功能
要在Delphi 7.0中实现控件工具箱功能和拖控件功能,可以按照以下步骤进行:
- 在Delphi 7.0中创建一个新的窗体应用程序。
- 在主窗体上添加一个TToolbox控件,用于显示控件列表。
- 在TToolbox控件中添加需要显示的控件,可以通过双击TToolbox控件,在Object Inspector中的Items属性中添加控件名称,每个名称一行。
- 在主窗体上添加一个TPanel控件,用于接收拖拽的控件。
- 在主窗体的OnCreate事件中,将TToolbox控件的DragMode属性设置为dmAutomatic,以启用拖拽功能。
- 在TToolbox控件的OnMouseDown事件中,设置其DragMode属性为dmAutomatic,并调用BeginDrag方法,启动拖拽操作。
- 在主窗体的OnDragOver事件中,设置Sender控件的DragMode属性为dmAutomatic,以便能够接受拖拽操作。
- 在主窗体的OnDragDrop事件中,将拖拽的控件实例化并设置Parent属性为接收拖拽的TPanel控件。
- 运行程序,可以从TToolbox控件中拖拽控件到TPanel控件上。
以下是一个示例代码:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls, ToolWin;
type
TForm1 = class(TForm)
Toolbox: TToolBox;
Panel1: TPanel;
procedure FormCreate(Sender: TObject);
procedure ToolboxMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
procedure FormDragOver(Sender, Source: TObject; X, Y: Integer;
State: TDragState; var Accept: Boolean);
procedure FormDragDrop(Sender, Source: TObject; X, Y: Integer);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject);
begin
Toolbox.DragMode := dmAutomatic;
Toolbox.Items.Add('Button');
Toolbox.Items.Add('Edit');
Toolbox.Items.Add('Label');
end;
procedure TForm1.ToolboxMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
Toolbox.DragMode := dmManual;
Toolbox.BeginDrag(False);
end;
procedure TForm1.FormDragOver(Sender, Source: TObject; X, Y: Integer;
State: TDragState; var Accept: Boolean);
begin
Accept := (Sender = Panel1) and (Source = Toolbox);
end;
procedure TForm1.FormDragDrop(Sender, Source: TObject; X, Y: Integer);
var
Control: TControl;
begin
if Source = Toolbox then
begin
if Toolbox.Selected >= 0 then
begin
case Toolbox.Selected of
0: Control := TButton.Create(Self);
1: Control := TEdit.Create(Self);
2: Control := TLabel.Create(Self);
end;
Control.Parent := Panel1;
Control.Left := X;
Control.Top := Y;
end;
end;
end;
end.
这样,你就可以在Delphi 7.0中实现控件工具箱功能和拖控件功能了
原文地址: https://www.cveoy.top/t/topic/hM5k 著作权归作者所有。请勿转载和采集!