首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在非可视化的Delphi组件上访问设计位置?

如何在非可视化的Delphi组件上访问设计位置?
EN

Stack Overflow用户
提问于 2012-06-12 05:14:44
回答 3查看 1.1K关注 0票数 2

当在集成开发环境中设计表单时,非可视化组件(如TMainMenus、TDatamodules)可以自由放置和定位。该位置是持久化的,因此在重新加载表单时,这些组件会出现在正确的位置。

但是,TComponent没有Top或Left属性!

那么,我的代码如何访问非可视化组件的“设计位置”呢?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2012-06-12 05:22:51

这可以在运行时访问,但这是一种黑客行为。(主要是因为它是作为一种黑客实现的。)

Left和Top属性被设置为单词大小的值,这两个属性被打包到一个名为TComponent.FDesignInfo的长整型变量中。您可以使用DesignInfo属性获取它的值。看一看TComponent.DefineProperties,了解它是如何使用的。

票数 6
EN

Stack Overflow用户

发布于 2014-07-14 17:29:00

还有:

  • 如何将DesignInfo设置为像(-100,-100)这样的点?

目标:将图标放在可视区域之外,在设计时将其隐藏。

注意:例如,当创建直接从TComponent派生的简单视觉组件时,它非常有用,我想到了一个非常简单的标签(它总是与顶部对齐,总是left=0,顶部是自动计算的,诸如此类),它只将其标题属性存储到.dfm文件中;而且任何本地化程序都只能看到该标题属性。

解决方案是使用如下代码覆盖ReadState

代码语言:javascript
运行
复制
procedure TMyComponent.ReadState(Reader:TReader);
var
   NewDesignInfo:LongRec;
begin
     inherited ReadState(Reader);
     NewDesignInfo.Hi:=Word(-100); // Hide design-time icon (top position = -100)
     NewDesignInfo.Lo:=Word(-100); // Hide design-time icon (left position = -100)
     DesignInfo:=Longint(NewDesignInfo); // Set the design-icon position out of visual area
end;

希望能帮助别人!

票数 0
EN

Stack Overflow用户

发布于 2019-01-09 21:48:14

这对我很有效。来源: CnPack CnAlignSizeWizard.pas。

代码语言:javascript
运行
复制
procedure SetNonVisualPos(Form: TCustomForm; Component: TComponent; X, Y: Integer);
const
  NonvisualClassNamePattern = 'TContainer';
  csNonVisualSize = 28;
  csNonVisualCaptionSize = 14;
  csNonVisualCaptionV = 30;
var
  P: TSmallPoint;
  H1, H2: HWND;
  Offset: TPoint;

  function HWndIsNonvisualComponent(hWnd: hWnd): Boolean;
  var
    AClassName: array[0..256] of Char;
  begin
    AClassName[GetClassName(hWnd, @AClassName, SizeOf(AClassName) - 1)] := #0;
    Result := string(AClassName) = NonvisualClassNamePattern;
  end;

  procedure GetComponentContainerHandle(AForm: TCustomForm; L, T: Integer; var H1, H2: hWnd; var Offset: TPoint);
  var
    R1, R2: TRect;
    P: TPoint;
    ParentHandle: hWnd;
    AControl: TWinControl;
    I: Integer;
  begin
    ParentHandle := AForm.Handle;
    AControl := AForm;
    if AForm.ClassNameIs('TDataModuleForm') then // ÊÇ DataModule
    begin
      for I := 0 to AForm.ControlCount - 1 do
        if AForm.Controls[I].ClassNameIs('TComponentContainer')
        and (AForm.Controls[I] is TWinControl) then
        begin
          AControl := AForm.Controls[I] as TWinControl;
          ParentHandle := AControl.Handle;
          Break;
        end;
    end;
    H2 := 0;
    H1 := GetWindow(ParentHandle, GW_CHILD);
    H1 := GetWindow(H1, GW_HWNDLAST);
    while H1 <> 0 do
    begin
      if HWndIsNonvisualComponent(H1) and GetWindowRect(H1, R1) then
      begin

        P.x := R1.Left;
        P.y := R1.Top;
        P := AControl.ScreenToClient(P);

        if (P.x = L) and (P.y = T) and (R1.Right - R1.Left = csNonVisualSize)
        and (R1.Bottom - R1.Top = csNonVisualSize) then
        begin
          H2 := GetWindow(ParentHandle, GW_CHILD);
          H2 := GetWindow(H2, GW_HWNDLAST);
          while H2 <> 0 do
          begin
            if HWndIsNonvisualComponent(H2) and GetWindowRect(H2, R2) then
            begin
              if (R2.Top - R1.Top = csNonVisualCaptionV) and (Abs(R2.Left + R2.Right - R1.Left - R1.Right) <= 1)
              and (R2.Bottom - R2.Top = csNonVisualCaptionSize) then
              begin
                Offset.x := R2.Left - R1.Left;
                Offset.y := R2.Top - R1.Top;
                Break;
              end;
            end;
            H2 := GetWindow(H2, GW_HWNDPREV);
          end;
          Exit;
        end;
      end;
      H1 := GetWindow(H1, GW_HWNDPREV);
    end;
  end;

begin
  P := TSmallPoint(Component.DesignInfo);
  GetComponentContainerHandle(Form, P.x, P.y, H1, H2, Offset);
  Component.DesignInfo := Integer(PointToSmallPoint(Point(X, Y)));
  if H1 <> 0 then
    SetWindowPos(H1, 0, X, Y, 0, 0, SWP_NOSIZE or SWP_NOZORDER);
  if H2 <> 0 then
    SetWindowPos(H2, 0, X + Offset.x, Y + Offset.y, 0, 0, SWP_NOSIZE or SWP_NOZORDER);
end;

使用示例:

代码语言:javascript
运行
复制
SetNonVisualPos(TCustomForm(Designer.Root),MyComponent,10,10);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10987628

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档