例如:我有一个TFrame(称为TPageFrame),它具有许多控件,例如TreeView将左,拆分器和主要客户区域对齐,包括编辑和RichEdit,如下图所示:
代码看起来像这样:
type
TPageFrame = class(TFrame)
Panel1: TPanel;
Splitter1: TSplitter;
Panel2: TPanel;
Panel3: TPanel;
Panel4: TPanel;
Edit1: TEdit;
RichEdit1: TRichEdit;
TreeView1: TTreeView;
private
{ Private declarations }
public
end;
在主窗体中,我有一个带有一些选项卡的RzTabControl。当我移到新选项卡时,将创建一个新的Frame(存储在Frame数组中),并将其父级设置为RzTabControl。
type
TForm1 = class(TForm)
RzTabControl1: TRzTabControl;
procedure RzTabControl1Change(Sender: TObject);
private
{ Private declarations }
FFrameArr: array[0..5] of TPageFrame;
public
{ Public declarations }
end;
procedure TForm1.RzTabControl1Change(Sender: TObject);
var
Index: Integer;
PageFrame: TPageFrame;
begin
Index := RzTabControl1.TabIndex;
Self.Caption := Index.ToString;
if FFrameArr[Index] = nil then
begin
PageFrame := TPageFrame.Create(Self);
PageFrame.Name := 'PageFrame' + Index.ToString;
PageFrame.Parent := RzTabControl1;
PageFrame.Align := alClient;
PageFrame.Visible := True;
FFrameArr[Index] := PageFrame;
end;
end;
问题:创建框架并设置其父级时,会出现很多“显示噪音”:
查看如何在2个位置上绘制两次编辑控件。 (通过视频演示会更容易...)
如何避免这种闪烁?
答案 0 :(得分:3)
@RaelB的代码存在一些问题,例如不正确地使用try / final,未处理本地创建的变量可能引起的任何异常等。
正确的(IMO)代码应为:
if not Assigned(FFrameArr[Index]) then begin
Screen.Cursor := crHourGlass;
try
// Defer updates
SendMessage(Handle, WM_SETREDRAW, WPARAM(False), 0);
try
PageFrame := TPageFrame.Create(Self);
try
PageFrame.Name := 'PageFrame' + Index.ToString;
PageFrame.Visible := False;
PageFrame.Parent := RzTabControl1;
PageFrame.Align := alClient;
PageFrame.Visible := True;
FFrameArr[Index] := PageFrame;
except
PageFrame.Free;
raise
end;
finally
// Make sure updates are re-enabled
SendMessage(Handle, WM_SETREDRAW, WPARAM(True), 0);
end;
PageFrame.Hide;
PageFrame.Show;
RzTabControl1.Invalidate;
finally
Screen.Cursor := crDefault;
end;
end;
答案 1 :(得分:-1)
感谢@HeartWare,基于How can I disable screen update which updating a lot of controls?
效果非常好:
if FFrameArr[Index] = nil then
begin
Screen.Cursor := crHourGlass;
// Defer updates
SendMessage(Handle, WM_SETREDRAW, WPARAM(False), 0);
try
PageFrame := TPageFrame.Create(Self);
PageFrame.Name := 'PageFrame' + Index.ToString;
PageFrame.Visible := False;
PageFrame.Parent := RzTabControl1;
PageFrame.Align := alClient;
PageFrame.Visible := True;
FFrameArr[Index] := PageFrame;
finally
// Make sure updates are re-enabled
SendMessage(Handle, WM_SETREDRAW, WPARAM(True), 0);
PageFrame.Hide;
PageFrame.Show;
RzTabControl1.Invalidate;
Screen.Cursor := crDefault;
end;
end;