备注行到TreeView



晚安,朋友们

我目前正在做一个项目,包括枚举可见窗口和它们的后代(也是可见的)。

我可以将TreeView的所有节点一个接一个地转移到Memo(文本格式),但现在我正试图做相反的事情(项目的必要性)。

有人可以帮助我在这里的StackOverflow?

这是所有的代码,列出的窗口在TreeView和之后转移到一个备忘录。

    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;

请注意,未命名的窗口中断格式需要正确恢复,所以我已经改变了字符串格式一点:'。['代替空格。

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

相关内容

  • 没有找到相关文章

最新更新