简体   繁体   English

光标位置的运行时组件

[英]Run-time Component at cursor position

I couldn't find the answer anywhere on the web. 我在网络上的任何地方都找不到答案。 Tried Google and many others. 尝试过Google和其他许多产品。

In Delphi 7 how to create a run-time component at cursor position? 在Delphi 7中如何在光标位置创建运行时组件?

I tried a simple code: 我尝试了一个简单的代码:

procedure TForm1.TButton1Click(Sender: TObject);
var NewCheckBox: TCheckBox;
MB: TMouseButton;
CPos: TPoint;
begin
  GetCursorPos(CPos);
  NewCheckBox:=TCheckBox.Create(Self);
  NewCheckBox.Parent:=Form1;
  NewCheckBox.Caption:='NewCheckBox';
  NewCheckBox.Left:=CPos.X;
  NewCheckBox.Top:=CPos.Y;
end;

But this doesn't work right. 但这行不通。 The components appear not at the cursor and I cannot place them wherever I want. 组件没有出现在光标处,我无法将它们放置在所需的任何位置。 The code places the component just as I click the button not when I click on the form where I want it to be placed. 该代码就像单击按钮一样放置该组件,而不是单击要放置该组件的表单。 I want to create a visual of the component that is about to be created and drag it all the way to the form from the button on a toolbar. 我想创建将要创建的组件的外观,并将其从工具栏上的按钮一直拖动到窗体。

I tried Drag-And-Drop but nothing works then, the Drop procedure always shows me a deny sign and does nothing. 我尝试了“拖放”操作,但随后无济于事,“放下”过程始终向我显示拒绝标志,并且不执行任何操作。

The code below will create your checkbox when you right-click on the form. 右键单击表单时,下面的代码将创建您的复选框。 It could do with a bit of refinement, eg to handle adding multiple checkboxes, etc, but might help you get going in the right direction. 它可以做一些改进,例如处理添加多个复选框等,但可能有助于您朝正确的方向前进。

procedure TForm1.CreateCheckBox(X, Y : Integer);
begin
  // NewCheckBox is a Form variable
  NewCheckBox:=TCheckBox.Create(Self);
  NewCheckBox.Parent:=Form1;
  NewCheckBox.Caption:='NewCheckBox';
  NewCheckBox.Left:= X;
  NewCheckBox.Top:= Y;
end;

procedure TForm1.FormMouseUp(Sender: TObject; Button: TMouseButton; Shift:
    TShiftState; X, Y: Integer);
begin
   if Button = mbRight then
     if NewCheckbox = Nil then
       CreateCheckBox(X, Y);
end;

Btw, when you use drag & drop on your form, getting the entry sign means that you have not set up an OnDragOver event for the form which sets the Accept parameter to True. 顺便说一句,当您在窗体上使用拖放时,获得输入符号表示您尚未为该窗体设置OnDragOver事件,该事件将Accept参数设置为True。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM