【问题标题】:Handle of TComponent instanceTComponent 实例的句柄
【发布时间】:2012-11-07 06:55:49
【问题描述】:

我有一个TComponent 类的实例,需要通过 WinAPI 调用与相应的控件进行交互。

为此,我需要TComponent 实例所代表的控件句柄。

如何从TComponent 获得该句柄?

【问题讨论】:

  • 你应该先测试 If C is TWinControll then....

标签: delphi winapi delphi-2009


【解决方案1】:

尝试将您的 TComponent 实例转换为 TWinControl。

如果它返回有效句柄,则未经测试,但它返回的是合理的数字。

 procedure TForm1.Button1Click(Sender: TObject);
   var
     i: integer;
     mycontrol: TWinControl;
   begin
     Memo1.Lines.Clear;
     for i := 0 to ComponentCount - 1 do
       if Components[i] is TWinControl then
          begin
            mycontrol := TWinControl(Components[i]);
            Memo1.Lines.Add(IntToStr(mycontrol.Handle));
          end;
   end;

【讨论】:

  • 使用 Form1.ControlCount 和 Form1.Controls[i] 将过滤掉不需要的非可视 TComponent。您仍然需要 IS 测试并转换为 TWinControl。
  • @dthorpe 但是,例如,这不会在面板上为您提供一个按钮(因为该按钮将位于面板的 Controls 而不是表单的)。如果使用递归方法,它还会为您提供通常不应该接触的内部辅助控件。
  • @dthorpe 实际上Controls 是关于父/子关系,而Components 是关于所有权。这是完全不同的关系。 Controls 不是 Components 的子集,即使递归遍历 Controls 也不一定会给你相同的控件。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多