Delphi-PRAXiS
Seite 1 von 2  1 2      

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Object-Pascal / Delphi-Language (https://www.delphipraxis.net/32-object-pascal-delphi-language/)
-   -   Delphi How elimitnate flicker of Form on resizing? (https://www.delphipraxis.net/195141-how-elimitnate-flicker-form-resizing.html)

flashcoder 6. Feb 2018 00:26

Delphi-Version: 10 Seattle

How elimitnate flicker of Form on resizing?
 
Liste der Anhänge anzeigen (Anzahl: 1)
I have this code below where is possible see desktop behind a full screen Form and have a trouble of flicker every time that Form is resized on these lines:

Delphi-Quellcode:
procedure TForm1.TakeScreenShot;
begin
  Width := 0;
  Height := 0;
  DoSnapShot := True;
  Width := ScreenRect.Width;
  Height := ScreenRect.Height;
end;
How prevent this flicker?

Thanks in advance

Full code:

Delphi-Quellcode:
type
  TForm1 = class(TForm)
    CAPTURAR: TButton;
    SAIR: TButton;
    procedure FormCreate(Sender: TObject);
    procedure CAPTURARClick(Sender: TObject);
    procedure SAIRClick(Sender: TObject);
  private
    DesktopBMP: TBitmap;
    DoSnapShot: boolean;
    ScreenRect: TRect;
    procedure TakeScreenShot;
    procedure WMEraseBkgnd(var Message: TWMEraseBkgnd); message WM_ERASEBKGND;
  public
    { Public declarations }
  protected
    procedure Paint; override;
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.CAPTURARClick(Sender: TObject);
begin
  TakeScreenShot;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  Left := 0;
  Top := 0;
  Width := Screen.Width;
  Height := Screen.Height - 10;

  ScreenRect := Rect(Left, Top, Width, Height);

  DesktopBMP := TBitmap.Create;
  DesktopBMP.SetSize(Width, Height);
end;

procedure TForm1.Paint;
begin
  inherited;
  // Canvas.Draw(0, 0, DesktopBMP);
  DesktopBMP.SaveToFile('c:\screen.bmp');
end;

procedure TForm1.SAIRClick(Sender: TObject);
begin
  Application.Terminate;
end;

procedure TForm1.TakeScreenShot;
begin
  Width := 0;
  Height := 0;
  DoSnapShot := True;
  Width := ScreenRect.Width;
  Height := ScreenRect.Height;
end;

procedure TForm1.WMEraseBkgnd(var Message: TWMEraseBkgnd);
var
  DesktopDC: HDC;
  DesktopHwnd: Hwnd;
  DesktopCanvas: TCanvas;
begin
  if DoSnapShot then
  begin
    DoSnapShot := False;
    DesktopHwnd := GetDesktopWindow;
    DesktopDC := GetDC(DesktopHwnd);
    try
      DesktopCanvas := TCanvas.Create;
      DesktopCanvas.Handle := DesktopDC;
      DesktopBMP.Canvas.CopyRect(ScreenRect, DesktopCanvas, ScreenRect);
    finally
      DesktopCanvas.Free;
      ReleaseDc(DesktopHwnd, DesktopDC);
    end;
  end;
  Message.Result := 1;
  inherited;
end;
PS: BorderStyle property is bsNone

Zacherl 6. Feb 2018 13:55

AW: How elimitnate flicker of Form on resizing?
 
Resizing the form will call your screenshot function multiple times within a very short time periode. The
Delphi-Quellcode:
CopyRect
/
Delphi-Quellcode:
BitBlt
screenshot method is not recommended on newer Windows versions, as this code is already very slow itself (this will copy the complete desktop contents from the video memory to the CPU memory every time). You might be able to reduce flickering by using some kind of double-buffering, but I would recommend you to use the Desktop Duplication API introduced in Windows 8.

flashcoder 6. Feb 2018 17:09

AW: How elimitnate flicker of Form on resizing?
 
Zitat:

Zitat von Zacherl
You might be able to reduce flickering by using some kind of double-buffering.

Delphi-Quellcode:
DoubleBuffered := True;
not works.

EWeiss 6. Feb 2018 17:31

AW: How elimitnate flicker of Form on resizing?
 
Paint in Paint and ignore WMEraseBkgnd btw. result 1 and Exit.

greets

flashcoder 6. Feb 2018 17:49

AW: How elimitnate flicker of Form on resizing?
 
Zitat:

Zitat von EWeiss (Beitrag 1393235)
Paint in Paint and ignore WMEraseBkgnd btw. result 1 and Exit.

greets

Also without success, could show how you made, please?

EWeiss 6. Feb 2018 18:24

AW: How elimitnate flicker of Form on resizing?
 
using my Doublebuffer.
Delphi-Quellcode:
function TForm1.DoubleBuffer(DC: HDC; width, height: Integer; Action: TAction): HDC;
begin

  if Action = CreateBuffer then
  begin
    FDBufferhDCTemp := CreateCompatibleDC(DC);
    FDBufferhBMTemp := CreateCompatibleBitmap(DC, width, height);
    FDBufferhBMPrev := SelectObject(FDBufferhDCTemp, FDBufferhBMTemp);
    FDBufferXx := width;
    FDBufferYy := height;
    FDBufferUseDC := DC;
    Result := FDBufferhDCTemp;
  end
  else
  begin
    // Zeichne das Resultat ins Target Window
    BitBlt(FDBufferUseDC, width, height, FDBufferXx, FDBufferYy, FDBufferhDCTemp, 0, 0, SRCCOPY);
    // Befreie die system resourcen
    SelectObject(FDBufferhDCTemp, FDBufferhBMPrev);
    DeleteObject(FDBufferhBMTemp);
    DeleteDC(FDBufferhDCTemp);
    Result := 0;
  end;
end;
and load as example
how do use it. see PaintBox1Paint

i am not use Doublebuffer from the Form it self.

greets

flashcoder 6. Feb 2018 18:40

AW: How elimitnate flicker of Form on resizing?
 
Ok, but this way, Form will appear in screenshot, right?

EWeiss 6. Feb 2018 18:44

AW: How elimitnate flicker of Form on resizing?
 
Zitat:

Zitat von flashcoder (Beitrag 1393239)
Ok, but this way, Form will appear in screenshot, right?

why? read the example.
and then test it.

i am use D2010 so can't not use your example.. sorry

greets

flashcoder 6. Feb 2018 18:56

AW: How elimitnate flicker of Form on resizing?
 
Zitat:

Zitat von EWeiss (Beitrag 1393240)
Zitat:

Zitat von flashcoder (Beitrag 1393239)
Ok, but this way, Form will appear in screenshot, right?

why? read the example.
and then test it.

i am use D2010 so can't not use your example.. sorry

greets

All functions used in my example are compatible with any delphi version from 2010 until 10 Berlin

flashcoder 7. Feb 2018 22:14

AW: How elimitnate flicker of Form on resizing?
 
Zitat:

Zitat von EWeiss (Beitrag 1393238)
using my Doublebuffer.
Delphi-Quellcode:
function TForm1.DoubleBuffer(DC: HDC; width, height: Integer; Action: TAction): HDC;
begin

  if Action = CreateBuffer then
  begin
    FDBufferhDCTemp := CreateCompatibleDC(DC);
    FDBufferhBMTemp := CreateCompatibleBitmap(DC, width, height);
    FDBufferhBMPrev := SelectObject(FDBufferhDCTemp, FDBufferhBMTemp);
    FDBufferXx := width;
    FDBufferYy := height;
    FDBufferUseDC := DC;
    Result := FDBufferhDCTemp;
  end
  else
  begin
    // Zeichne das Resultat ins Target Window
    BitBlt(FDBufferUseDC, width, height, FDBufferXx, FDBufferYy, FDBufferhDCTemp, 0, 0, SRCCOPY);
    // Befreie die system resourcen
    SelectObject(FDBufferhDCTemp, FDBufferhBMPrev);
    DeleteObject(FDBufferhBMTemp);
    DeleteDC(FDBufferhDCTemp);
    Result := 0;
  end;
end;
and load as example
how do use it. see PaintBox1Paint

i am not use Doublebuffer from the Form it self.

greets



I not understood where use DC in my code

Delphi-Quellcode:
var
 DC: HDC;
 rc: TRect;
begin

rc := Form1.ClientRect;
DC := DoubleBuffer(Form1.Canvas.Handle, rc.Right, rc.Bottom, CreateBuffer);

// Where use this DC?

end;
and also want know if these parameters passed to this custom DoubleBuffer function are of my Form in my situation?


Alle Zeitangaben in WEZ +1. Es ist jetzt 08:29 Uhr.
Seite 1 von 2  1 2      

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz