綜述:如何重繪一個MDIChild窗體時,將MDIChild本身含有的容器,如TPanel有排列:= alClient和ParentBackground:=假
請參閱安地列斯懂行的意見!
==========================================
如下面的代碼所示,TForm7是MDIForm格式,TForm8是MDIChild格式。 TForm8包含一個alClient對齊面板,它還包含一個TPaintBox。如果TForm8的面板的ParentBackground設置爲False,我不能從TForm7觸發TForm8的paintbox的繪畫事件。我想知道爲什麼會發生這種情況,以及如何觸發TForm8的paintbox的繪畫事件,而不會明確地引用它。任何建議表示讚賞!
注:如果我叫
Self.Repaint
withint TForm8,例如其Click事件中,可以觸發TForm8的顏料盒的Paint事件。只有當我在TForm8之外呼叫
form8.repaint
時才能觸發它。我想知道爲什麼會發生這種情況?
可能相關的SO網頁:包含mdi窗體形式
How to repaint a parent form while a modal form is active?
單位。
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
一邊。你爲什麼叫'Repaint'?通常情況下,您調用Invalidate,Invalidate調用InvalidateRect,InvalidateRect反過來發布WM_PAINT消息。只有在應用程序閒置後纔會抽取消息隊列,纔會處理此消息。這是一種更有效的做事方式,因爲它最大限度地減少了實際繪製的次數。 – 2011-03-21 20:55:32