How to catch the moment when the parent control has been resized?

前端 未结 5 445
-上瘾入骨i
-上瘾入骨i 2021-01-20 18:58

I have a visual component derived from TWinControl. I need to do some work in my component when its parent control has been resized. In general case, the \"Align\" property

5条回答
  •  栀梦
    栀梦 (楼主)
    2021-01-20 19:54

    I was looking for a solution to a similar problem. But in my case I cannot have such restrictions on alignment, and subclassing seemed overkill (the alignment thingie looks overkill too, now that I look at it)

    So I came up with the following idea:

    type
      TMyComponent = class(TControl)
      private
        FParentLastWidth: integer;
      ...
        procedure Invalidate; override;
      ...
      end;
    
    procedure TMyComponent.Invalidate;
    begin
      if (Parent <> nil) and (FParentLastWidth <> Parent.Width) then
      begin
        FParentLastWidth := Parent.Width;
        // do whatever when the parent resizes
      end;
      inherited;
    end;
    

    Add or replace the FParentLastWidth with whatever size you are tracking (I only needed reaction when the parent width changed. You can take it as an optimization so to not react to all kinds of changes which makes no difference for your component)

提交回复
热议问题