我在表单上有一个TGridPanel
,并希望将控件添加到单击的特定“单元格”。在TGridPanel中点击单元格
我能得到一点很容易不够:
procedure TForm1.GridPanel1DblClick(Sender: TObject);
var
P : TPoint;
InsCol, InsRow : Integer;
begin
P := (Sender as TGridPanel).ScreenToClient(Mouse.CursorPos);
if (Sender as TGridPanel).ControlAtPos(P) = nil then
begin
InsCol := ???;
InsRow := ???;
(Sender as TGridPanel).ControlCollection.AddControl(MyControl, InsCol, InsRow)
end;
end;
我可能不需要if ControlAtPos(P) = nil then
线,但我要确保我不会插入一个已经具有一个小区内的控制它。
那么......我用什么代码来获取InsCol和InsRow?我已经上下了TGridPanel
和TControlCollection
类代码,并且找不到任何能够从鼠标坐标中获得列或行值的内容。它们似乎也不是使用OnDblClick()
以外的相关事件。
任何帮助将不胜感激。
编辑:将变量结果更改为MyControl以避免混淆。
我希望能有一个更有效的方式,但是这似乎是一种可靠的方法。顺便说一句,Windows有一个名为'PtInRect()'的API,它可以完成你的'PointInRect()'函数的功能,但是参数的顺序是相反的。 –
更正:您的PointInRect()和Windows的PtInRect()之间的区别在于Windows版本排除了右侧和底部边缘。 –
我知道存在像PointInRect这样的东西。因为我曾经见过它,但我找不到它。谢谢杰里让我想起这个功能 – Ravaut123