NSIS插件“nsScreenshot”不能在Windows NT 6.x的工作(NSIS Plug

2019-09-18 17:45发布

我补充说,3岁比原来的插件后发布的代码,但它仍然会返回错误...

代码是直线前进恕我直言......但我仍然最有可能错过一些方面...

请参见下面的代码:

{
        nsScreenshot NSIS Plugin
        (c) 2003: Leon Zandman (leon@wirwar.com)

        Re-compiled by: Linards Liepins (linards.liepins@gmail.com)
        Code by: http://www.delphitricks.com/source-code/forms/make_a_desktop_screenshot.html
        (e) 2012.
}
library nsScreenshot;

uses
  nsis in './nsis.pas',
  Windows,
  Jpeg,
  graphics,
  types,
  SysUtils;

const
  USER32 = 'user32.dll';

type
  HWND = type LongWord;
  {$EXTERNALSYM HWND}
  HDC = type LongWord;
  {$EXTERNALSYM HDC}
  BOOL = LongBool;
  {$EXTERNALSYM BOOL}

{$EXTERNALSYM GetDesktopWindow}
function GetDesktopWindow: HWND; stdcall; external USER32 name 'GetDesktopWindow';
{$EXTERNALSYM GetWindowDC}
function GetWindowDC(hWnd: HWND): HDC; stdcall; external USER32 name 'GetWindowDC';
{$EXTERNALSYM GetWindowRect}
function GetWindowRect(hWnd: HWND; var lpRect: TRect): BOOL; stdcall; external USER32 name 'GetWindowRect';
{$EXTERNALSYM ReleaseDC}
function ReleaseDC(hWnd: HWND; hDC: HDC): Integer; stdcall; external user32 name 'ReleaseDC';

function GetScreenshot(Filename: string; Hwnd: HWND; var Width: integer; var Height: integer): boolean; forward;
function ScreenShot(Bild: TBitMap; hWnd: HWND): boolean; forward;


function Grab_FullScreen(hwndParent: HWND; string_size: integer; variables: PChar; stacktop: pointer):integer; cdecl;
var
  buf: array[0..1024] of char;
  W,H: integer;
begin
  Result := 0;
  // set up global variables
  Init(hwndParent,string_size,variables,stacktop);

  // Get filename to save to
  PopString;//(@buf);

  // Get a full-screen screenshot
  if GetScreenShot(buf,GetDesktopWindow,W,H) then begin
    // Everything went just fine...

    // Push image dimensions onto stack
    PushString(PChar(IntToStr(H)));
    PushString(PChar(IntToStr(W)));

    // Push result onto stack
    PushString(PChar('ok'));
    Result := 1;
  end else begin
    // Something went wrong...
    PushString(PChar('error'));
  end;
end;

function Grab(hwndParent: HWND; string_size: integer; variables: PChar; stacktop: pointer):integer; cdecl;
var
  buf: array[0..1024] of char;
  grabWnd: HWND;
  Filename: string;
  W,H: integer;
begin
  Result := 0;
  // set up global variables
  Init(hwndParent,string_size,variables,stacktop);

  try
    // Get filename to save to
    PopString;//(@buwf);
    Filename := buf;

    // Get window handle of window to grab
    PopString;//(@buf);
    grabWnd := StrToInt(buf);
  except
    PushString(PChar('error'));
    exit;
  end;

  // Get screenshot of parent windows (NSIS)
  if GetScreenShot(Filename,grabWnd,W,H) then begin
    // Everything went just fine...

    // Push image dimensions onto stack
    PushString(PChar(IntToStr(H)));
    PushString(PChar(IntToStr(W)));

    // Push result onto stack
    PushString(PChar('ok'));
    Result := 1;
  end else begin
    // Something went wrong...
    PushString(PChar('error'));
  end;
end;

function GetScreenshot(Filename: string; Hwnd: HWND; var Width: integer; var Height: integer): boolean;
var
  bmp: TBitmap;
begin
  Result := false;

  // Get screenshot
  bmp := TBitmap.Create;
  try
    try
      if ScreenShot(bmp,Hwnd) then begin
        Width  := bmp.Width;
        Height := bmp.Height;
        bmp.SaveToFile(Filename);
        Result := true;
      end;
    except
      // Catch exception and do nothing (function return value remains 'false')
    end;
  finally
    bmp.Free;
  end;
end;

function ScreenShot(Bild: TBitMap; hWnd: HWND): boolean;
var
  c: TCanvas;
  r, t: TRect;
  h: THandle;
begin
  Result := false;

  c := TCanvas.Create;
  c.Handle := GetWindowDC(GetDesktopWindow);

  h := hWnd;
  if h <> 0 then begin
    GetWindowRect(h, t);
    try
      r := Rect(0, 0, t.Right - t.Left, t.Bottom - t.Top);
      Bild.Width  := t.Right - t.Left;
      Bild.Height := t.Bottom - t.Top;
      Bild.Canvas.CopyRect(r, c, t);
    finally
      ReleaseDC(0, c.Handle);
      c.Free;
    end;
    Result := true;
  end;
end;

function GetScreenToFile(FileName: string; Quality: Word; Percent: Word): boolean;
var
  Bmp: TBitmap;
  Jpg: TJpegImage;
begin
  Bmp := TBitmap.Create;
  Jpg := TJpegImage.Create;
  try
    Bmp.Width := GetDeviceCaps(GetDc(0), 8) * Percent div 100;
    Bmp.Height := GetDeviceCaps(GetDc(0), 10) * Percent div 100;
    SetStretchBltMode(Bmp.Canvas.Handle, HALFTONE);
    StretchBlt(Bmp.Canvas.Handle, 0, 0, Bmp.Width, Bmp.Height, GetDc(0), 0, 0, GetDeviceCaps(GetDc(0), 8), GetDeviceCaps(GetDc(0), 10), SRCCOPY);
    Jpg.Assign(Bmp);
    Jpg.CompressionQuality := Quality;
    Jpg.SaveToFile(FileName);
  finally
    Jpg.free;
    Bmp.free;
  end;
end;

function ScreenToFile(hwndParent: HWND; string_size: integer; variables: PChar; stacktop: pointer):integer; cdecl;
var
  buf: array[0..1024] of char;
  grabWnd: HWND;
  Filename: string;
  W,H: integer;
begin
  Result := 0;
  Init(hwndParent,string_size,variables,stacktop);
  try
    PopString;
    Filename := buf;
    PopString;
    grabWnd := StrToInt(buf);
  except
    PushString(PChar('error'));
    exit;
  end;
  if GetScreenToFile(Filename,W,H) then
  begin
    PushString(PChar('ok'));
    Result := 1;
  end else
  begin
    PushString(PChar('error'));
  end;
end;

  //ScreenToFile('SHOT.JPG', 50, 70);

exports Grab_FullScreen,
        Grab,
        ScreenToFile;

begin
end.

搜索ScreenToFile

感谢您的任何输入,. 这个插件是安装文档生成自动化至关重要。

Answer 1:

1. NSIS插件核心单元的问题:

1.1。 关于错误字符串:

从你自己的答案后arised您正在使用NSIS的ANSI版本。 既然你已经在德尔福XE,其中编译了库代码使用的stringCharPChar映射到Unicode字符串,你NSIS安装应用程序和库错误的数据之间传递。

1.2。 芯插件单元上的另一视图:

我检查你稍微修改插件核心单元NSIS.pas和有一些问题,可以防止你的插件才能正常工作。 然而,正如我已经看到了这个单位,首先什么来到我的脑海里,是来包装独立的过程和函数为一类。 这就是我所做的。

1.3。 该NSIS.pas V2.0:

既然你目前使用的只有3个功能从原来的核心单元your code我已经简化了类只使用这些(和一个额外的消息框中显示)。 因此,这里是修改后的插件核心单元的代码。 我不是对数据操作方面的专家,所以也许下面的代码可以简化,但它的作品至少在Delphi XE2和德尔福2009年,在我测试过它。 下面是代码:

unit NSIS;

interface

uses
  Windows, CommCtrl, SysUtils;

type
  PParamStack = ^TParamStack;
  TParamStack = record
    Next: PParamStack;
    Value: PAnsiChar;
  end;
  TNullsoftInstaller = class
  private
    FParent: HWND;
    FParamSize: Integer;
    FParameters: PAnsiChar;
    FStackTop: ^PParamStack;
  public
    procedure Initialize(Parent: HWND; ParamSize: Integer; Parameters: PAnsiChar;
      StackTop: Pointer);
    procedure PushString(const Value: string = '');
    function PopString: string;
    function MessageDialog(const Text, Caption: string; Buttons: UINT): Integer;
  end;

var
  NullsoftInstaller: TNullsoftInstaller;

implementation

procedure TNullsoftInstaller.Initialize(Parent: HWND; ParamSize: Integer;
  Parameters: PAnsiChar; StackTop: Pointer);
begin
  FParent := Parent;
  FParamSize := ParamSize;
  FParameters := Parameters;
  FStackTop := StackTop;
end;

procedure TNullsoftInstaller.PushString(const Value: string = '');
var
  CurrParam: PParamStack;
begin
  if Assigned(FStackTop) then
  begin
    CurrParam := PParamStack(GlobalAlloc(GPTR, SizeOf(TParamStack) + FParamSize));
    StrLCopy(@CurrParam.Value, PAnsiChar(AnsiString(Value)), FParamSize);
    CurrParam.Next := FStackTop^;
    FStackTop^ := CurrParam;
  end;
end;

function TNullsoftInstaller.PopString: string;
var
  CurrParam: PParamStack;
begin
  Result := '';
  if Assigned(FStackTop) then
  begin
    CurrParam := FStackTop^;
    Result := String(PAnsiChar(@CurrParam.Value));
    FStackTop^ := CurrParam.Next;
    GlobalFree(HGLOBAL(CurrParam));
  end;
end;

function TNullsoftInstaller.MessageDialog(const Text, Caption: string;
  Buttons: UINT): Integer;
begin
  Result := MessageBox(FParent, PChar(Text), PChar(Caption), Buttons);
end;

initialization
  NullsoftInstaller := TNullsoftInstaller.Create;
finalization
  if Assigned(NullsoftInstaller) then
    NullsoftInstaller.Free;

end.

1.4。 修改后的插件核心单元的使用方法:

正如你所看到的,还有的NullsoftInstaller声明的全局变量,它可以让你用,我已经包裹你已经使用之前的功能的类。 从这一变量的对象实例的使用简化与初始化,并且其中被创建并分配给该变量此目的例如当库加载,并且当该库被释放释放定稿部分。

因此,你需要在你的代码做的唯一的事情就是用这种NullsoftInstaller全局变量像这样:

uses
  NSIS;

function ScreenToFile(Parent: HWND; ParamSize: Integer; Parameters: PAnsiChar;
  StackTop: Pointer): Integer; cdecl;
var
  InputString: string;
begin
  Result := 0;

  // this is not necessary, if you keep the NullsoftInstaller object instance 
  // alive (and there's even no reason to free it, since this will be done in 
  // the finalization section when the library is unloaded), so the following
  // statement has no meaning when you won't free the NullsoftInstaller
  if not Assigned(NullsoftInstaller) then
    NullsoftInstaller := TNullsoftInstaller.Create;

  // this has the same meaning as the Init procedure in the original core unit
  NullsoftInstaller.Initialize(Parent, ParamSize, Parameters, StackTop);
  // this is the same as in the original, except that returns a native string
  InputString := NullsoftInstaller.PopString;
  NullsoftInstaller.MessageDialog(InputString, 'PopString Result', 0);
  // and finally the PushString method, this is also the same as original and
  // as well as the PopString supports native string for your Delphi version
  NullsoftInstaller.PushString('ok');
end;

2. Aero的合成窗口的屏幕截图

这里是我的截图程序的尝试, TakeScreenshot代码。 这需要一个额外的参数DropShadow ,启用Aero的组合物时,应采取截图,包括窗口阴影。 但是我无法找到一个方法如何做到这一点的不是把假窗后面所捕获的一个不同的方式。 它有一个很大的弱点; 有时会发生当捕获完成后没有完全显示假窗,所以需要围绕捕获窗口,而不是白色假窗当前桌面的截图(未显示)的后面。 因此,设置DropShadow为True现在只是处于实验阶段。

DropShadow是假的(无阴影截图)它工作正常。 我的猜测是,你是路过由于Unicode的德尔福与上述ANSI NSIS问题错误的参数。

library nsScreenshot;

uses
  Windows, SysUtils, Types, Graphics, DwmApi, Forms, JPEG, NSIS;

procedure CalcCloseCrop(Bitmap: TBitmap; const BackColor: TColor;
  out CropRect: TRect);
var
  X: Integer;
  Y: Integer;
  Color: TColor;
  Pixel: PRGBTriple;
  RowClean: Boolean;
  LastClean: Boolean;
begin
  LastClean := False;
  CropRect := Rect(Bitmap.Width, Bitmap.Height, 0, 0);
  for Y := 0 to Bitmap.Height-1 do
  begin
    RowClean := True;
    Pixel := Bitmap.ScanLine[Y];
    for X := 0 to Bitmap.Width - 1 do
    begin
      Color := RGB(Pixel.rgbtRed, Pixel.rgbtGreen, Pixel.rgbtBlue);
      if Color <> BackColor then
      begin
        RowClean := False;
        if X < CropRect.Left then
          CropRect.Left := X;
        if X + 1 > CropRect.Right then
          CropRect.Right := X + 1;
      end;
      Inc(Pixel);
    end;
    if not RowClean then
    begin
      if not LastClean then
      begin
        LastClean := True;
        CropRect.Top := Y;
      end;
      if Y + 1 > CropRect.Bottom then
        CropRect.Bottom := Y + 1;
    end;
  end;
  with CropRect do
  begin
    if (Right < Left) or (Right = Left) or (Bottom < Top) or 
      (Bottom = Top) then
    begin
      if Left = Bitmap.Width then
        Left := 0;
      if Top = Bitmap.Height then
        Top := 0;
      if Right = 0 then
        Right := Bitmap.Width;
      if Bottom = 0 then
        Bottom := Bitmap.Height;
    end;
  end;
end;

procedure TakeScreenshot(WindowHandle: HWND; const FileName: string;
  DropShadow: Boolean);
var
  R: TRect;
  Form: TForm;
  Bitmap: TBitmap;
  Target: TBitmap;
  DeviceContext: HDC;
  DesktopHandle: HWND;
  ExtendedFrame: Boolean;
const
  CAPTUREBLT = $40000000;
begin
  ExtendedFrame := False;
  if DwmCompositionEnabled then
  begin
    DwmGetWindowAttribute(WindowHandle, DWMWA_EXTENDED_FRAME_BOUNDS, @R,
      SizeOf(TRect));
    if DropShadow then
    begin
      ExtendedFrame := True;
      R.Left := R.Left - 30;
      R.Top := R.Top - 30;
      R.Right := R.Right + 30;
      R.Bottom := R.Bottom + 30;
    end;
  end
  else
    GetWindowRect(WindowHandle, R);

  SetForegroundWindow(WindowHandle);
  Bitmap := TBitmap.Create;
  try
    Bitmap.PixelFormat := pf24bit;
    Bitmap.SetSize(R.Right - R.Left, R.Bottom - R.Top);
    if ExtendedFrame then
    begin
      DesktopHandle := GetDesktopWindow;
      DeviceContext := GetDC(GetDesktopWindow);
      Form := TForm.Create(nil);
      try
        Form.Color := clWhite;
        Form.BorderStyle := bsNone;
        Form.AlphaBlend := True;
        Form.AlphaBlendValue := 0;
        ShowWindow(Form.Handle, SW_SHOWNOACTIVATE);
        SetWindowPos(Form.Handle, WindowHandle, R.Left, R.Top, 
          R.Right - R.Left, R.Bottom - R.Top, SWP_NOACTIVATE);
        Form.AlphaBlendValue := 255;
        BitBlt(Bitmap.Canvas.Handle, 0, 0, R.Right - R.Left, R.Bottom - R.Top,
          DeviceContext, R.Left, R.Top, SRCCOPY or CAPTUREBLT);
      finally
        Form.Free;
        ReleaseDC(DesktopHandle, DeviceContext);
      end;
      Target := TBitmap.Create;
      try
        CalcCloseCrop(Bitmap, clWhite, R);
        Target.SetSize(R.Right - R.Left, R.Bottom - R.Top);
        Target.Canvas.CopyRect(Rect(0, 0, R.Right - R.Left, R.Bottom - R.Top),
          Bitmap.Canvas, R);
        Target.SaveToFile(FileName);
      finally
        Target.Free;
      end;
    end
    else
    begin
      DeviceContext := GetWindowDC(WindowHandle);
      try
        BitBlt(Bitmap.Canvas.Handle, 0, 0, R.Right - R.Left, R.Bottom - R.Top,
          DeviceContext, 0, 0, SRCCOPY or CAPTUREBLT);
      finally
        ReleaseDC(WindowHandle, DeviceContext);
      end;
      Bitmap.SaveToFile(FileName);
    end;
  finally
    Bitmap.Free;
  end;
end;

function ScreenToFile(Parent: HWND; ParamSize: Integer; Params: PAnsiChar;
  StackTop: Pointer): Integer; cdecl;
var
  I: Integer;
  FileName: string;
  DropShadow: Boolean;
  Parameters: array[0..1] of string;
begin
  Result := 0;
  if not Assigned(NullsoftInstaller) then
    NullsoftInstaller := TNullsoftInstaller.Create;

  NullsoftInstaller.Initialize(Parent, ParamSize, Params, StackTop);

  for I := 0 to High(Parameters) do
    Parameters[I] := NullsoftInstaller.PopString;
  FileName := Parameters[1];
  if not DirectoryExists(ExtractFilePath(FileName)) or
    not TryStrToBool(Parameters[0], DropShadow) then
  begin
    NullsoftInstaller.PushString('error');
    NullsoftInstaller.PushString('Invalid parameters!');
    Exit;
  end;

  try
    TakeScreenshot(Parent, FileName, DropShadow);
    NullsoftInstaller.PushString('ok');
    Result := 1;
  except
    on E: Exception do
    begin
      NullsoftInstaller.PushString('error');
      NullsoftInstaller.PushString(E.Message);
      NullsoftInstaller.MessageDialog(E.Message, 'Error', 0);
    end;
  end;
end;

exports
  ScreenToFile;

begin

end.


Answer 2:

一些搜索后,我发现下面的代码从下面的SO问题的工作:

如何利用Delphi中活动窗口的截图?

在NSIS的inclusin所有其他选项BitBtl功能引起的死机,因为航空及其相关DWM雾probobly ...

此外,还有建议使用此功能。 不喷测试...

http://msdn.microsoft.com/en-us/library/dd162869.aspx

尽管如此,仍有一些问题:

  • 玻璃框架被绘制为透明的
  • 从NSIS文件名转换为一定程度损坏WideString的...
  • 文件可以仅仅通过对话背景色填充,如果更改页面(使用nsdialogs和MUI2)...


Answer 3:

GetDesktopWindow大概应该是GetDesktopWindow()但经常可以(也应该)使用NULL而不是GetDesktopWindow()。 此外,一个函数使用的GetDC和其他GetWindowDC ...



文章来源: NSIS Plugin “nsScreenshot” not working in Windows NT 6.x