Delphi-PRAXiS

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?

EWeiss 8. Feb 2018 00:11

AW: How elimitnate flicker of Form on resizing?
 
i can not see any resize Event in your example
after create the form in Fullscreen

so i not understand what your do.

greets

flashcoder 8. Feb 2018 00:22

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

Zitat von EWeiss (Beitrag 1393335)
i can not see any resize Event in your example
after create the form in Fullscreen

so i not understand what your do.

greets

The Form is resized inside of TakeScreenshot procedure.
Relative to my last answer, what component (in my case) i can apply custom DubleBuffer function?
Ex: the parameters received are of my Form? could give a example about where apply this function?

EWeiss 8. Feb 2018 00:24

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

Zitat von flashcoder (Beitrag 1393336)
Zitat:

Zitat von EWeiss (Beitrag 1393335)
i can not see any resize Event in your example
after create the form in Fullscreen

so i not understand what your do.

greets

The Form is resized inside of TakeScreenshot procedure.

yes and then?
i see a Fullscreen without drawing any and after can't resize the form.
not understand how your see the flicker.. nothing is draw to the Fullscreen Window

sorry for my bad english.

Zitat:

Ex: the parameters received are of my Form? could give a example about where apply this function?
how?
if nothing paint on the fullcreen and i can not resize it.
and you has a example see my Starwars Scrolltext, please compare

if understand your code correctly then you create a CompatibleDC here.

Delphi-Quellcode:
DesktopCanvas := TCanvas.Create;
that should be
Delphi-Quellcode:
DesktopCanvas := DoubleBuffer(Form1.Canvas.Handle, rc.Right, rc.Bottom, CreateBuffer);

now draw to DesktopCanvas
..
then use
Delphi-Quellcode:
DoubleBuffer(0, 0, 0, DestroyBuffer);

which bitblt your result to the Target window and free the ressources.


greets

flashcoder 8. Feb 2018 01:08

AW: How elimitnate flicker of Form on resizing?
 
Unfortunately i not understood your custom DoubleBuffer function.
Probably this can work to remove flicker when Form is resized in TakeScreenshot method, but i'm totally lost about how implement on my code :(

EWeiss 8. Feb 2018 02:45

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

Zitat von flashcoder (Beitrag 1393339)
Unfortunately i not understood your custom DoubleBuffer function.
Probably this can work to remove flicker when Form is resized in TakeScreenshot method, but i'm totally lost about how implement on my code :(

Delphi-Quellcode:
  if DoSnapShot then
  begin
    DoSnapShot := False;

    DesktopHwnd := GetDesktopWindow;
    DesktopDC := GetDC(DesktopHwnd);

    DesktopCanvas := DoubleBuffer(Canvas.Handle, ScreenRect.Right, ScreenRect.Bottom, CreateBuffer);
    BitBlt(DesktopCanvas, 0, 0, ScreenRect.Right, ScreenRect.Bottom, DesktopDC, 0, 0, SRCCOPY);
    DoubleBuffer(0, 0, 0, DestroyBuffer);

    ReleaseDc(DesktopHwnd, DesktopDC);
  end;
check this works by me..

EDIT:
Your problem is not the flickering but the setting of the height and width to 0
and then back to the old Position (Fullscreen)
Rethink your approach

using.. "Hide, Show" or "minimize, maximize" or create a Container for the Capture.
just a Suggestion.

greets

flashcoder 8. Feb 2018 15:15

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

Zitat von EWeiss (Beitrag 1393341)

EDIT:
Your problem is not the flickering but the setting of the height and width to 0
and then back to the old Position (Fullscreen)
Rethink your approach

using.. "Hide, Show" or "minimize, maximize" or create a Container for the Capture.
just a Suggestion.

greets

@EWeiss,

this idea of container seems more appropiated for me, but my form still will appear on screenshot?
I not want hide/show or minimoze/maximize, this not is good. Remember that background of Form must be erased before capture the screen.
This container could be a TImage or what?

EWeiss 8. Feb 2018 23:07

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

but my form still will appear on screenshot?
yes..

You change the size on every call that is wrong. (So you have to live with your current Situation)
You do not want to hide that windows, that's wrong. (so the form is still appear)

Unfortunately I can't help you anymore.. find your own way

greets

flashcoder 8. Feb 2018 23:54

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

Zitat von EWeiss (Beitrag 1393410)
Zitat:

but my form still will appear on screenshot?
yes..

You change the size on every call that is wrong. (So you have to live with your current Situation)
You do not want to hide that windows, that's wrong. (so the form is still appear)

Unfortunately I can't help you anymore.. find your own way

greets

Ok, thank you very much by all your answers :-D

EWeiss 9. Feb 2018 00:16

AW: How elimitnate flicker of Form on resizing?
 
that works..
with my own way so you can see i have give you all required Information.
bye.

Anhang gelöscht.

greets


Alle Zeitangaben in WEZ +1. Es ist jetzt 00:01 Uhr.

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