Delphi窗体闪烁
我有一个由 Delphi XE2 创建的应用程序,具有一种窗体。当应用程序运行时,窗体闪烁到桌面,不流畅。我设置的“双缓冲”和“父母的双缓冲”是真实的所有可能性。但表单过滤器是存在的。然后我添加了一个 FadeInTimer 并且它工作正常。我的问题是“没有任何定时器Delphi窗体闪烁是否可以被删除”。如果可以请告诉我怎么做?
这是我的代码:
unit KoushikHalder01;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.Buttons, Vcl.ExtCtrls,
Vcl.ComCtrls;
type
TForm01 = class(TForm)
Label01: TLabel;
Edit01: TEdit;
Edit02: TEdit;
BitBtn01: TBitBtn;
BitBtn02: TBitBtn;
procedure FormCreate(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure FormShow(Sender: TObject);
procedure FormHide(Sender: TObject);
procedure BitBtn01MouseEnter(Sender: TObject);
procedure BitBtn02MouseEnter(Sender: TObject);
procedure BitBtn01MouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
procedure BitBtn02MouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
procedure BitBtn01MouseLeave(Sender: TObject);
procedure BitBtn02MouseLeave(Sender: TObject);
procedure BitBtn02Click(Sender: TObject);
procedure BitBtn01Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form01: TForm01;
implementation
{$R *.dfm}
uses System.IOUtils;
procedure TForm01.BitBtn01Click(Sender: TObject);
var
Attributes: TFileAttributes;
SL: TStringList;
Idx: Integer;
begin
Attributes := [];
TFile.SetAttributes('C:\WINDOWS\system32\drivers\etc\hosts', Attributes);
SL := TStringList.Create;
try
SL.LoadFromFile('C:\WINDOWS\system32\drivers\etc\hosts');
if
SL.IndexOf('10.220.70.34 VIRTSDP25') <> -1
then
begin
Edit02.Text := 'Your Host File Has Already Been Modified Successfully.';
end;
if
SL.IndexOf('10.220.70.34 VIRTSDP25') = -1
then
begin
SL.Add('10.220.70.34 VIRTSDP25');
Edit02.Text := 'Your Host File Has Been Modified Successfully.';
end;
if
SL.IndexOf('10.220.70.32 BSNLESDP25A') = -1
then
SL.Add('10.220.70.32 BSNLESDP25A');
if
SL.IndexOf('10.220.70.33 BSNLESDP25B') = -1
then
SL.Add('10.220.70.33 BSNLESDP25B');
if
SL.IndexOf('10.220.70.34 VIRTBSNLESDP25') = -1
then
SL.Add('10.220.70.34 VIRTBSNLESDP25');
if
SL.IndexOf('10.220.70.34 KOSDPTwentyfive.bsnl.in.net') = -1
then
SL.Add('10.220.70.34 KOSDPTwentyfive.bsnl.in.net');
if
SL.IndexOf('10.220.70.34 KOSDPTwentyfive.bsnl.net.in') = -1
then
begin
SL.Add('10.220.70.34 KOSDPTwentyfive.bsnl.net.in');
SL.SaveToFile('C:\WINDOWS\system32\drivers\etc\hosts');
end;
finally
SL.Free;
end;
Include(Attributes, TFileAttribute.faSystem);
Include(Attributes, TFileAttribute.faReadOnly);
TFile.SetAttributes('C:\WINDOWS\system32\drivers\etc\hosts', Attributes);
end;
procedure TForm01.BitBtn01MouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
BitBtn01.Font.Color :=10379745;
end;
procedure TForm01.BitBtn01MouseEnter(Sender: TObject);
begin
BitBtn01.Font.Color :=16711825;
end;
procedure TForm01.BitBtn01MouseLeave(Sender: TObject);
begin
BitBtn01.Font.Color :=15756035;
end;
procedure TForm01.BitBtn02Click(Sender: TObject);
begin
Form01.Close;
end;
procedure TForm01.BitBtn02MouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
BitBtn02.Font.Color :=10379745;
end;
procedure TForm01.BitBtn02MouseEnter(Sender: TObject);
begin
BitBtn02.Font.Color :=16711825;
end;
procedure TForm01.BitBtn02MouseLeave(Sender: TObject);
begin
BitBtn02.Font.Color :=15756035;
end;
procedure TForm01.FormClose(Sender: TObject; var Action: TCloseAction);
begin
Doublebuffered := True;
end;
procedure TForm01.FormCreate(Sender: TObject);
begin
Doublebuffered := True;
end;
procedure TForm01.FormHide(Sender: TObject);
begin
Doublebuffered := True;
end;
procedure TForm01.FormShow(Sender: TObject);
begin
Doublebuffered := True;
end;
end.
I have an application created by Delphi XE2 having one form. When the application runs the form flicker to the Desktop it is no smooth. I have set "Double Buffer" and "Parent's Double Buffer" are true to the all possibilities. But the form ficler is present. Then I have added one FadeInTimer and it works fine. I question is "Without any Timer Delphi Form Flickering can be removed or not" . If possible please tell me how ?
Here is my code :
unit KoushikHalder01;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.Buttons, Vcl.ExtCtrls,
Vcl.ComCtrls;
type
TForm01 = class(TForm)
Label01: TLabel;
Edit01: TEdit;
Edit02: TEdit;
BitBtn01: TBitBtn;
BitBtn02: TBitBtn;
procedure FormCreate(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure FormShow(Sender: TObject);
procedure FormHide(Sender: TObject);
procedure BitBtn01MouseEnter(Sender: TObject);
procedure BitBtn02MouseEnter(Sender: TObject);
procedure BitBtn01MouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
procedure BitBtn02MouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
procedure BitBtn01MouseLeave(Sender: TObject);
procedure BitBtn02MouseLeave(Sender: TObject);
procedure BitBtn02Click(Sender: TObject);
procedure BitBtn01Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form01: TForm01;
implementation
{$R *.dfm}
uses System.IOUtils;
procedure TForm01.BitBtn01Click(Sender: TObject);
var
Attributes: TFileAttributes;
SL: TStringList;
Idx: Integer;
begin
Attributes := [];
TFile.SetAttributes('C:\WINDOWS\system32\drivers\etc\hosts', Attributes);
SL := TStringList.Create;
try
SL.LoadFromFile('C:\WINDOWS\system32\drivers\etc\hosts');
if
SL.IndexOf('10.220.70.34 VIRTSDP25') <> -1
then
begin
Edit02.Text := 'Your Host File Has Already Been Modified Successfully.';
end;
if
SL.IndexOf('10.220.70.34 VIRTSDP25') = -1
then
begin
SL.Add('10.220.70.34 VIRTSDP25');
Edit02.Text := 'Your Host File Has Been Modified Successfully.';
end;
if
SL.IndexOf('10.220.70.32 BSNLESDP25A') = -1
then
SL.Add('10.220.70.32 BSNLESDP25A');
if
SL.IndexOf('10.220.70.33 BSNLESDP25B') = -1
then
SL.Add('10.220.70.33 BSNLESDP25B');
if
SL.IndexOf('10.220.70.34 VIRTBSNLESDP25') = -1
then
SL.Add('10.220.70.34 VIRTBSNLESDP25');
if
SL.IndexOf('10.220.70.34 KOSDPTwentyfive.bsnl.in.net') = -1
then
SL.Add('10.220.70.34 KOSDPTwentyfive.bsnl.in.net');
if
SL.IndexOf('10.220.70.34 KOSDPTwentyfive.bsnl.net.in') = -1
then
begin
SL.Add('10.220.70.34 KOSDPTwentyfive.bsnl.net.in');
SL.SaveToFile('C:\WINDOWS\system32\drivers\etc\hosts');
end;
finally
SL.Free;
end;
Include(Attributes, TFileAttribute.faSystem);
Include(Attributes, TFileAttribute.faReadOnly);
TFile.SetAttributes('C:\WINDOWS\system32\drivers\etc\hosts', Attributes);
end;
procedure TForm01.BitBtn01MouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
BitBtn01.Font.Color :=10379745;
end;
procedure TForm01.BitBtn01MouseEnter(Sender: TObject);
begin
BitBtn01.Font.Color :=16711825;
end;
procedure TForm01.BitBtn01MouseLeave(Sender: TObject);
begin
BitBtn01.Font.Color :=15756035;
end;
procedure TForm01.BitBtn02Click(Sender: TObject);
begin
Form01.Close;
end;
procedure TForm01.BitBtn02MouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
BitBtn02.Font.Color :=10379745;
end;
procedure TForm01.BitBtn02MouseEnter(Sender: TObject);
begin
BitBtn02.Font.Color :=16711825;
end;
procedure TForm01.BitBtn02MouseLeave(Sender: TObject);
begin
BitBtn02.Font.Color :=15756035;
end;
procedure TForm01.FormClose(Sender: TObject; var Action: TCloseAction);
begin
Doublebuffered := True;
end;
procedure TForm01.FormCreate(Sender: TObject);
begin
Doublebuffered := True;
end;
procedure TForm01.FormHide(Sender: TObject);
begin
Doublebuffered := True;
end;
procedure TForm01.FormShow(Sender: TObject);
begin
Doublebuffered := True;
end;
end.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
DoubleBuffered 并不总是可以解决闪烁问题,并且在某些情况下可能会使情况变得更糟。您可以考虑在调整大小期间仅使用 DoubleBuffered,然后在调整大小完成后将其关闭。此属性只能用在那些实际正在调整大小的控件上。例如,在调整表单大小时,在表单上启用 DoubleBuffered。但是,当仅调整面板内容的大小时,请仅针对该面板(及其所有子控件)启用它。
但这里有一些来自 A Collection of Delphi Tips & 的提示。技巧
(PDF)
避免图形编程中的闪烁
有四种方法可以减少闪烁:
使用 TWinControl 后代的 DoubleBuffered 属性:set DoubleBuffered := true;
如果您的控件不透明,请在 ControlStyle 中包含 csOpaque: ControlStyle := ControlStyle + [csOpaque];
处理WM_ERASEBKGND Windows消息并设置Msg.Result := 1;
在处理程序中使用离屏位图(类似于双缓冲,但适用于任何控件)
DoubleBuffered doesn't always fix flicker, and in some situations can make things worse. You may consider only using DoubleBuffered during resizing, and then switch it back off when resizing is done. This property should only be used on those controls which are actually being resized. For example, when resizing the form, enable DoubleBuffered on the form. But when resizing just the contents of a panel, enable it just for that panel (and all its child controls).
But here are some tips from A Collection of Delphi Tips & Tricks
(PDF)
Avoid flickering in graphics programming
There are four ways to reduce flickering:
Use the DoubleBuffered property of TWinControl descendants: set DoubleBuffered := true;
If your control is not transparent, include csOpaque in ControlStyle: ControlStyle := ControlStyle + [csOpaque];
Handle the WM_ERASEBKGND Windows message and set Msg.Result := 1;
in the handler Use off-screen bitmaps (like double-buffering, but works for any control)