总结:

请参阅Andreas的丰富见解!

==========================================

如下面的代码所示,TForm7是MDIForm表单,TForm8是MDIChild表单。 TForm8包含一个alClient对齐面板,其中还包含一个TPaintBox。如果TForm8的面板的ParentBackground设置为False,则无法从TForm7触发TForm8的绘画盒的绘画事件。我想知道为什么会发生这种情况,以及如何在不明确引用的情况下触发TForm8的paintbox的paint事件。任何建议表示赞赏!

注意:如果我在TForm8中调用Self.Repaint,例如在其Click事件中调用,则可以触发TForm8的绘画盒的绘画事件。仅当我在TForm8之外调用form8.repaint时,才能触发它。我想知道为什么会这样吗?

可能相关的SO页面:
How to repaint a parent form while a modal form is active?

包含MDIForm表单的单位。

    unit Unit7;

    interface

    uses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs;

    type
      TForm7 = class(TForm)
        procedure FormShow(Sender: TObject);
        procedure FormClick(Sender: TObject);

      end;

    var
      Form7: TForm7;

    implementation

    {$R *.dfm}

    uses
      Unit8;

    procedure TForm7.FormShow(Sender: TObject);
    begin
      TForm8.Create(Self);
    end;

    procedure TForm7.FormClick(Sender: TObject);
    begin
      TForm8(ActiveMDIChild).Repaint;
    end;

    end.


上述单位的Dfm。

    object Form7: TForm7
      Left = 0
      Top = 0
      Caption = 'Form7'
      ClientHeight = 379
      ClientWidth = 750
      Color = clBtnFace
      Font.Charset = DEFAULT_CHARSET
      Font.Color = clWindowText
      Font.Height = -11
      Font.Name = 'Tahoma'
      Font.Style = []
      FormStyle = fsMDIForm
      OldCreateOrder = False
      OnClick = FormClick
      OnShow = FormShow
      PixelsPerInch = 96
      TextHeight = 13
    end


包含MDIChild表单的单位。

    unit Unit8;

    interface

    uses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, ExtCtrls;

    type
      TForm8 = class(TForm)
        pb1: TPaintBox;
        pnl1: TPanel;
        procedure pb1Paint(Sender: TObject);
        procedure pb1Click(Sender: TObject);
      private
        fCounter: Integer;

      end;

    implementation

    {$R *.dfm}

    procedure TForm8.pb1Click(Sender: TObject);
    begin
      Self.Repaint;
    end;

    procedure TForm8.pb1Paint(Sender: TObject);
    begin
      Self.pb1.Canvas.TextOut(30, 30, IntToStr(Self.fCounter));
      Self.fCounter := Self.fCounter + 1;
    end;

    end.


上述单位的Dfm。

    object Form8: TForm8
      Left = 0
      Top = 0
      Caption = 'Form8'
      ClientHeight = 226
      ClientWidth = 233
      Color = clBtnFace
      Font.Charset = DEFAULT_CHARSET
      Font.Color = clWindowText
      Font.Height = -11
      Font.Name = 'Tahoma'
      Font.Style = []
      FormStyle = fsMDIChild
      OldCreateOrder = False
      Visible = True
      PixelsPerInch = 96
      TextHeight = 13
      object pnl1: TPanel
        Left = 0
        Top = 0
        Width = 233
        Height = 226
        Align = alClient
        ShowCaption = False
        TabOrder = 0
        object pb1: TPaintBox
          Left = 1
          Top = 1
          Width = 231
          Height = 224
          Align = alClient
          OnClick = pb1Click
          OnPaint = pb1Paint
          ExplicitLeft = 56
          ExplicitTop = -64
          ExplicitWidth = 105
          ExplicitHeight = 105
        end
      end
    end

最佳答案

我认为是这样的:

信不信由你,“常规”行为是,如果您重新绘制表单(或其他容器),则仅重新绘制该容器,而不重新绘制其中的子级。但是,随着视觉主题的出现,控件有了半透明的部分,突然之间,您需要在重绘父控件时重新绘制子控件,这仅仅是因为子控件需要重新融合到新的背景中。

我的假设很容易(相对)通过仔细检查VCL源代码来验证,例如

procedure TWinControl.CMInvalidate(var Message: TMessage);
begin
  { Removed irrelevant code to avoid copyvio issues. }
      InvalidateRect(WindowHandle, nil, not (csOpaque in ControlStyle));
      { Invalidate child windows which use the parentbackground when themed }
      if ThemeServices.ThemesEnabled then
        for I := 0 to ControlCount - 1 do
          if csParentBackground in Controls[I].ControlStyle then
            Controls[I].Invalidate;
  { Removed irrelevant code to avoid copyvio issues. }
end;


因此,当ParentBackground设置为false时,并且该面板的行为就像经典面板一样,当其父面板显示时,它不会重新绘制。另一方面,如果ParentBackgroundtrue,则它会连同其父对象一起重新绘制。

因此,确实没有问题。您只是希望得到意想不到的行为。

因此,您需要按照David的建议手动重新粉刷油漆盒。

关于delphi - 当MDIChild本身包含带有对齐的== alClient和ParentBackground:= False的容器(如TPanel)时,如何重绘MDIChild表单,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/5383061/

10-10 12:36