在Delphi IDE中获取对象的引用

时间:2015-10-16 18:07:52

标签: delphi delphi-xe4

这是我对此问答的回答:

Can I change the display format for strings in the watch list?

事实证明,在D7和XE3之间的某个时刻,IDE的Watch Window的实现从使用TListView变为TVirtualStringTree。

虽然我通过忽略VST并从剪贴板获取监视值来发布我的答案更新,该更新适用于XE4,但我仍然希望能够从VST获取监视值,如果可以的话。我想我知道该怎么做 一旦我引用了VST,但问题在于我尝试获取VST失败了。

以下是我在自定义程序包中使用的代码的MCVE。希望它的作用是不言自明的。问题是块中的代码

  if WatchWindow.Components[i] is TVirtualStringTree then begin
    [...]
  end;

从不执行,DESPITE类名" TVirtualStringTree"出现在Memo1中。显然,具有该类名的组件未通过" is"测试。我猜测原因是编译到IDE中的TVirtualTreeView是一个与我使用的版本不同的版本,v.5.3.0,这是我能找到的最接近XE4的前身。

所以,我的问题是,这是可能的解释,我能做些什么吗?我怀疑是否有人可以通过帽子来繁荣用于XE4的TVirtualStringTree版本,这可能会解决我的问题。

type
  TOtaMenuForm = class(TForm)
    Memo1: TMemo;
    procedure FormCreate(Sender: TObject);
  private
    WatchWindow : TForm;
    VST : TVirtualStringTree;
  end;

procedure TOtaMenuForm.FormCreate(Sender: TObject);
var
  i : Integer;
  S : String;
begin

  WatchWindow := Nil;
  VST := Nil;

  // Iterate the IDE's forms to find the Watch Window
  for i := 0 to Screen.FormCount - 1 do begin
    S := Screen.Forms[i].Name;
    if CompareText(S, 'WatchWindow') = 0 then begin
      WatchWindow := Screen.Forms[i];
      Break;
    end;
  end;

  Assert(WatchWindow <> Nil);

  if WatchWindow <> Nil then begin
    Memo1.Lines.Add('Looking for VST');
    for i := 0 to WatchWindow.ComponentCount - 1 do begin
      Memo1.Lines.Add(IntToStr(i) + ':' + WatchWindow.Components[i].ClassName);
      if WatchWindow.Components[i] is TVirtualStringTree then begin
         VST := TVirtualStringTree(WatchWindow.Components[i]);
         Memo1.Lines.Add('found VST');
         Break;
      end;
    end;
    if VST = Nil then
      Memo1.Lines.Add('VST not found');
  end;
end;
Btw,我意识到依赖于IDE的实现细节的解决方案可能很脆弱,但这只是为了娱乐(我喜欢从一个组件中获取字符串数据以避免存储的挑战有)。

1 个答案:

答案 0 :(得分:0)

可能您可以尝试通过RTTI方法仅使用嵌入到IDE TVirtualStringTree实现中的已发布属性来执行您想要的操作吗?