到 TreeView 的备注行

Memo lines to TreeView

朋友们晚安,

我目前正在从事一个涉及枚举可见 windows 及其后代(也可见)的项目。

并且我可以将 TreeView 的所有节点(以文本格式)一个一个地传输,但现在我正在尝试做相反的事情(项目的必要性)。

有人可以在 Whosebug 中帮我解决这个问题吗?

这是在 TreeView 上列出 windows 并将其转移到一个备忘录后的所有代码。

    function GetWindowTitle(hwnd: HWND): string; 
    begin 
      SetLength(Result, 255); 
      SetLength(Result, GetWindowText(hwnd, PChar(Result), 255)); 
    end; 

    function GetWindowClass(hwnd: HWND): string; 
    begin 
      SetLength(Result, 255); 
      SetLength(Result, GetClassName(hwnd, PChar(Result), 255)); 
    end; 

    function GetWindowInfo(hwnd: HWND): string; 
    begin 
      Result := GetWindowTitle(hwnd) + ' [' + GetWindowClass(hwnd) + 
        '] (' + {IntToStr}IntToHex(hwnd, 8) + ')'; 
    end; 

    function EnumChildProc(hwnd: HWND; lParam: Integer): BOOL; stdcall; 
    var 
      NewNode, ParentNode: TTreeNode; 
    begin 
      Result := True; 
      ParentNode := TTreeNode(lParam); 

      if IsWindowVisible(hwnd) then 

      NewNode := ParentNode.Owner.AddChild(ParentNode, 
        GetWindowInfo(hwnd)); 
      EnumChildWindows(hwnd, @EnumChildProc, Integer(NewNode)); 
    end; 

    function EnumWindowsProc(hwnd: HWND; lParam: Integer): BOOL; stdcall; 
    var 
      NewNode: TTreeNode; 
    begin 
      Result := True; 
      if IsWindowVisible(hwnd) then 
      NewNode := TTreeView(lParam).Items.Add(nil, GetWindowInfo(hwnd)); 
      EnumChildWindows(hwnd, @EnumChildProc, Integer(NewNode)); 
    end; 

    procedure EnumWindowsTree(Tree: TTreeView); 
    begin 
      EnumWindows(@EnumWindowsProc, Integer(Tree)); 
    end; 

    // Listing all windows in TreeView

    procedure TForm2.Button1Click(Sender: TObject); 
    begin 
    TreeView1.Items.Clear; 
    EnumWindowsTree(TreeView1); 
    end; 

   //Tranfers all nodes of TreeView for a Memo (one by one)

    procedure TForm2.Button3Click(Sender: TObject); 
    var I,P,Cnt : Integer; 
        ParentNode, ChildNode: TTreeNode; 
    begin 
       P   := 65; 
       ParentNode  :=  TreeView1.Items[0]; 
       While ParentNode<>nil do 
       begin 
         if (ParentNode <> nil) then 
         begin 
              Memo1.Lines.Add(ParentNode.Text); 
             Cnt := 1; 
             ChildNode := ParentNode.GetFirstChild; 
             while (ChildNode <> nil) do 
             begin 
               Memo1.Lines.Add(ChildNode.Text); 

               if ChildNode.HasChildren then 
               begin 
                  ParentNode:= ChildNode.GetFirstChild; 
                  break; 
               end; 
               ChildNode := ChildNode.GetNextSibling; 
               Inc(Cnt); 
             end; 
         end; 
         if ChildNode=nil then 
         begin 
           if ParentNode.GetNextSibling<>nil then 
              ParentNode:=ParentNode.GetNextSibling 
           else 
           begin 
              while ParentNode.GetNextSibling=nil do 
              begin 
                 if ParentNode.Parent<>nil then ParentNode:=ParentNode.Parent else break; 
              end; 
              if ParentNode<>nil then ParentNode:=ParentNode.GetNextSibling; 
           end; 
         end; 
         Inc(P); 
       end; 
    end; 

最好使用内置的TreeView内容存储方法:

// Tranfers all nodes of TreeView for a Memo (one by one)
var
  MS: TMemoryStream;
begin
  MS := TMemoryStream.Create;
  try
    TreeView1.SaveToStream(MS);
    MS.Position := 0;
    Memo1.Lines.LoadFromStream(MS);
  finally
    Ms.Free;
  end;
end;

// Tranfers all nodes to TreeView from a Memo
var
  MS: TMemoryStream;
begin
  MS := TMemoryStream.Create;
  try
    Memo1.Lines.SaveToStream(MS);
    MS.Position := 0;
    TreeView1.LoadFromStream(MS);
  finally
    Ms.Free;
  end;
end;

请注意,未命名的 windows 会破坏正确恢复所需的格式,因此我稍微更改了字符串格式:'.[' 而不是 space.

function GetWindowInfo(hwnd: HWND): string;
begin
  Result := GetWindowTitle(hwnd) + '.[' + GetWindowClass(hwnd) + '] (' +
  { IntToStr } IntToHex(hwnd, 8) + ')';
end;