当MDIChild本身包含带有对齐的TPanel等容器时,如何重新绘制MDIChild表单:= alClient和ParentBackground:= False

时间:2011-03-21 20:12:15

标签: delphi repaint mdichild

综述

请参阅Andreas的知识渊博的评论!

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

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

注意:如果我在TForm8上调用Self.Repaint,例如在其Click事件中,可以触发TForm8的paintbox的paint事件。仅当我在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

2 个答案:

答案 0 :(得分:5)

我认为情况就是这样:

信不信由你,“正常”的行为是,如果你重新绘制一个表单(或其他一些容器),只有那个容器被重新绘制,而不是包含在其中的子容器。然而,随着视觉主题的出现,控件获得了半透明的部分,突然之间你需要在重绘父项时重新绘制子控件,这只是因为子项需要重新混合到新的背景中。

我的假设通过仔细检查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的建议手动重绘油漆盒。

答案 1 :(得分:1)

当您希望绘图框重新绘制时,您只需要调用pb1.Invalidate

或者我误解了你的问题?