Delphi源代码如下:
unit Commctrl;
type
{$EXTERNALSYM HTREEITEM}
HTREEITEM = ^_TREEITEM;
{$EXTERNALSYM _TREEITEM}
_TREEITEM = packed record
end;
unit ComCtrls;
type
TTreeNode = class(TPersistent)
...
FItemId: HTreeItem;
...
end;
ItemId属性根据TTreeNodes.AddItem的返回值得到,源代码如下:
function TTreeNodes.AddItem(Parent, Target: HTreeItem;
const Item: TTVItem
AddMode: TAddMode): HTreeItem;
begin
..
Result := TreeView_InsertItem(Handle, InsertStruct);
end;
其中TreeView_InsertItem源代码如下:
function TreeView_InsertItem(hwnd: HWND
const lpis: TTVInsertStruct): HTreeItem;
begin
Result := HTreeItem( SendMessage(hwnd, TVM_INSERTITEM, 0, Longint(@lpis)) );
end;
Win32 API中对TVM_InsertItem消息说明为:
...Returns the handle to the new item if successful...
也就是说Delphi向Win32标准控件TreeView发送TVM_InsertItem消息,实现InsertItem动作
并将返回值强制转换为HTreeItem类型
在Win32中,所有的Handle实际上都是32位整数(指针)
对于Delphi来说,因为TTreeNodes对象、TTreeNode对象在存取TreeItem(Win32的叫法)时
是根据ItemID属性(即TreeItem的Handle)发送消息实现的,
实际上只需要Handle而并不需要定义出HTreeItem指向的具体类型,
所以Delphi定义的最后结果就是
type
{$EXTERNALSYM HTREEITEM}
HTREEITEM = ^_TREEITEM;
{$EXTERNALSYM _TREEITEM}
_TREEITEM = packed record
end;