Search code examples
delphidelphi-10-seattle

How "crop" a area behind of a screen locker Form on Windows 8/10 without locker Form appear on final result?


I have a Remote Administration Tool where i have difficulties to "crop" a determinated area behind a screen locker Form on Windows 8/10 without locker Form appear on final result ( that in my case is a image file (.bmp) that will be showed to client later).

On Windows Vista/7 works fine. Then i want know why on Windows 8/10 instead of "cropped" area be the website (behind screen locker Form), this area is the locker Form?

Eg (note that all gray area is the screen locker Form):

enter image description here

Here is settings of screen locker Form and code like is generated a "cropped" area on client side, respectively:

object Form2: TForm2
  Left = 648
  Top = 176
  AlphaBlend = True
  BorderStyle = bsNone
  Caption = 'Form2'
  ClientHeight = 507
  ClientWidth = 687
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'MS Sans Serif'
  Font.Style = []
  FormStyle = fsStayOnTop
  OldCreateOrder = False
  Position = poDesigned
  OnClose = FormClose
  OnCreate = FormCreate
  OnShow = FormShow
  DesignSize = (
    687
    507)
  PixelsPerInch = 96
  TextHeight = 13
  object Image1: TImage
    Left = 0
    Top = 0
    Width = 105
    Height = 105
    AutoSize = True
  end

procedure TForm1.CS1Read(Sender: TObject; Socket: TCustomWinSocket);
var
  StrCommand: string;
  X1, X2, Y1, Y2: Integer;
  Bmp: TBitmap;
  DeskTopDC: HDc;
  DeskTopCanvas: TCanvas;
  R: TRect;
  List : TStringList;

begin
  StrCommand := Socket.ReceiveText;

   if Pos('¬', StrCommand) > 0 then // Receiving dimensions of area (in this case of some website) for save to file.
  begin

    List := TStringList.Create;
    Bmp := TBitmap.Create;

    try

      ExtractStrings(['¬'], [], PChar(StrCommand), List);

      X1 := StrToIntDef(List[0], 0) - Form2.Left - 2; // Form2 is the Form that copper whole desktop (locker screen)
      Y1 := StrToIntDef(List[1], 0) - Form2.Top - 2;
      X2 := StrToIntDef(List[2], 0) - Form2.Left - 2;
      Y2 := StrToIntDef(List[3], 0) - Form2.Top - 2;

      R := Rect(X1, Y1, X2, Y2);

      Bmp.SetSize(R.Width, R.Height);

         DeskTopDC := GetWindowDC(GetDesktopWindow); // Windows Vista/7

      if ( Pos('Windows 8', GetSOComputer) > 0 ) or
         ( Pos('Windows 10', GetSOComputer) > 0 ) then

      if BrowserHandle > 0 then
         DeskTopDC := GetWindowDC(BrowserHandle); // If is Win8/10, then device context will be window of navigator (ex: Google Chrome)
                                                  // this was necessary to that dimensions of area here be equals like was received, already
                                                  // that on server side, i see only browser window ( a way of exclude locker screen (Form2)
                                                  // of screenshot)

      DeskTopCanvas := TCanvas.Create;
      DeskTopCanvas.handle := DeskTopDC;

      Bmp.Canvas.CopyRect(Rect(0, 0, Bmp.Width, Bmp.Height), DeskTopCanvas, R);

      Bmp.SaveToFile(GetSpecialFolder(CSIDL_LOCAL_APPDATA, True) + 'area.bmp'); // Saving area created to AppData\Local for show to client later 

      ReleaseDC(GetDeskTopWindow, DeskTopDC);

    finally
      List.Free;
      Bmp.Free;
    end;
  end;

end;

Solution

  • I discovered that is a trouble generated by Aero composition that not is possible disable on Windows8/8.1/10, already on Windows Vista/7 is possible (and the code above always works fine).

    Then the solution found by me was create a "hole" on Form2 (locker Form) using:

    1. CreateRectRgn
    2. CombineRgn
    3. SetWindowRgn

    1. Create area with dimensions received.

    2. Save to file.

    3. Close hole of locker Form.

    so, GetWindowDC not get the screen locker form.