简体   繁体   English

Delphi中的设计时拖放?

[英]Design-time drag and drop in Delphi?

Before Delphi 2006 (I think) introduced the TFlowPanel and TGridPanel, I did a control that was similar in concept. 在Delphi 2006(我认为)介绍TFlowPanel和TGridPanel之前,我做了一个概念相似的控件。 It still does a couple of things those controls do not do, and when upgrading my code to Delphi 2009, I decided to add a couple of enhancements to that as well. 它仍然做了一些控件不能做的事情,当我将代码升级到Delphi 2009时,我决定添加一些增强功能。

Right now, the order of the child controls is determined by their creation order. 现在,子控件的顺序由其创建顺序决定。 The FlowPanel and GridPanel show a better way with ControlIndex and other filtered properties, but I was wondering if there is a way to handle drag and drop reordering in design-time? FlowPanel和GridPanel使用ControlIndex和其他过滤属性显示更好的方法,但我想知道是否有办法在设计时处理拖放重新排序? As far as I can tell, dragging an edit control and dropping it onto my panel doesn't call anything that I can access at design-time. 据我所知,拖动编辑控件并将其拖放到我的面板上并不会调用我在设计时可以访问的任何内容。

I was half-fantasising about a way to either detect the drop operation directly, or to perhaps detect when a control is moved so I can determine where it should go. 我想要一种方法来直接检测掉落操作,或者可能检测控件何时被移动,以便我可以确定它应该去哪里。

Any ideas? 有任何想法吗?

Update: OK, got it working. 更新:好的,让它工作。 The container control was already overriding AlignControls to manage the placement of the controls. 容器控件已经重写AlignControls来管理控件的放置。 When you drag the nested control and drop it, AlignControls is again called. 拖动嵌套控件并将其删除时,再次调用AlignControls。 I then compared the new coordinates of the control with the other controls in the list and moved it to the appropriate position. 然后,我将控件的新坐标与列表中的其他控件进行比较,并将其移动到适当的位置。

There were a couple of problems that I had to work through (mostly related to the many calls to AlignControls) but the basic concept is simple enough. 我必须解决几个问题(主要与AlignControls的许多调用有关),但基本概念很简单。 Thanks to all the commenters for all the help. 感谢所有评论者提供的所有帮助。

You can't drag a control that's already on the form and drop it onto your panel. 您无法拖动表单上已有的控件并将其拖放到面板上。 Dragging is only for moving a control, not for changing its parent. 拖动仅用于移动控件,而不用于更改其父控件。 To change the parent, cut and paste. 要更改父级,剪切和粘贴。

If the control is already on your panel, and you want to move it to another position on your panel, then the panel can control the layout by overriding the TWinControl.AlignControls method. 如果控件已经在面板上,并且您想将其移动到面板上的其他位置,则面板可以通过覆盖TWinControl.AlignControls方法来控制布局。 When a control is moved, its SetBounds method is called, and among the things tha happens is that it calls AlignControl(Self) on its parent window. 移动控件时,会调用其SetBounds方法,并且在其中发生的事情是它在其父窗口上调用AlignControl(Self) That calls AlignControls . 这称为AlignControls Look in Controls.pas , and you'll see that that's a complicated method, but it's what is responsible for the layout of the children on a control, and that's exactly what you're planning to change. 看看Controls.pas ,你会发现这是一个复杂的方法,但它是控制器上孩子布局的原因,而这正是你计划改变的。

Perhaps some of these suggestions might help. 也许其中一些建议可能有所帮助。

You can re-parent a control in the designer without having to do cut-and-paste. 您可以在设计器中重新控制控件,而无需进行剪切和粘贴。 View the structure pane, and simply drag the visual control to the node of another parent in the structure pane. 查看结构窗格,只需将可视控件拖到结构窗格中另一个父节点的节点即可。 If you have things in a flowpanel, drag everything out of the flow panel and drag them back in the order that you want them to be. 如果您在流程面板中有东西,请将所有内容拖出流程面板,然后按照您希望的顺序将它们拖回。

(You can re-parent ANY visual control this way, without changing anything other than its parent. I highly recommend doing it this way.) (您可以通过这种方式重新使用任何可视控件,而不会更改除其父级之外的任何内容。我强烈建议您这样做。)

You can view the form as text, and move the declaration order around in there -- but obviously you'll need to be careful when editing the "resource" file directly. 您可以将表单视为文本,并在那里移动声明顺序 - 但显然您在编辑“资源”文件时需要小心。

You can set tab order in the designer, so you could make a different control based on tab order that works as you want. 您可以在设计器中设置Tab键顺序,这样您就可以根据所需的Tab键顺序进行不同的控制。 You can right click on the form and change the creation order of the non-visual controls, but that doesn't work with visual controls. 您可以右键单击表单并更改非可视控件的创建顺序,但这不适用于可视控件。

Have you tried to write an "OnDragDrop" event for your grid component, where you check if your component is in design mode? 您是否尝试为网格组件编写“OnDragDrop”事件,在其中检查组件是否处于设计模式?

I haven't written such a component yet, but I don't see why the event shouldn't trigger. 我还没有编写这样的组件,但我不明白为什么不应该触发该事件。

暂无
暂无

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

相关问题 在设计时在Delphi中检测新组件的插入 - Detecting insertion of new components at design-time in Delphi Delphi:如何为自定义组件创建额外的设计时菜单? - Delphi: How to create an extra design-time menu for a custom component? 如何在设计时更改 Delphi FireDAC tFDMemTable 字段名称 - How to change Delphi FireDAC tFDMemTable field name in design-time 知道是否在运行时或设计时打开了Delphi表单 - Knowing if a Delphi form was opened at run or design-time 在Delphi中在设计时双击对象时出现不合理的错误 - Unreasonable error when double-clicking on Object in Design-time in Delphi 如何在Delphi / C ++ Builder中对设计时包进行故障排除? - How can I troubleshoot design-time packages in Delphi/C++Builder? 如何在使用多线程时使用Delphi设计时FireDac TFDQuery? - How to use Delphi design-time FireDac TFDQuery when using multiple threads? 如何在设计时在delphi中的自定义控件中选择子控件 - How to select sub-control in my custom control in design-time in delphi Delphi,在设计时,当使用 nil 所有者创建时,ComponentState、ControlState 的 state 是什么? - Delphi, at design-time, what is the state of ComponentState, ControlState when created with a nil owner? 创建安装程序时如何从设计时程序包内确保正确的“需要” Delphi运行时程序包定义 - How to ensure correct 'requires' definition of a Delphi run-time package from within a design-time package when creating an installer
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM